简体   繁体   English

C++ 需要有关删除节点的帮助 function

[英]C++ Need help on Remove Node function

I have stressed my head out the last few days to figure out how to get this remove() function to work.在过去的几天里,我一直在强调如何让这个 remove() function 工作。 I'm still a student and data structure is no joke.我还是个学生,数据结构可不是闹着玩的。 I really need help on how to get this function to remove a specific number on the list from user input.我真的需要有关如何获取此 function 以从用户输入中删除列表中的特定号码的帮助。 Doesn't matter what I try, it still could not work right.不管我尝试什么,它仍然无法正常工作。

For example, the list is: [1, 2, 3] I want to delete number 2 on the list.例如,列表是:[1, 2, 3] 我要删除列表中的数字 2。 I want the remove() function to traverse thur the list, if it found number 2, then delete number 2.我希望 remove() function 遍历列表,如果找到数字 2,则删除数字 2。

    class SortedNumberList {
public:
   Node* head;
   Node* tail;

   SortedNumberList() {
      head = nullptr;
      tail = nullptr;
   }

   void Insert(double number) {
       Node* newNode = new Node(number);
       if (head == nullptr) {
           head = newNode;
           tail = newNode;
       }
       else {
           tail->SetNext(newNode);
           tail = newNode;
           
       }
   }

   // Removes the node with the specified number value from the list. Returns
   // true if the node is found and removed, false otherwise.
   bool Remove(double number) {
       Node* temp = head;     
       if (temp == nullptr) {
           return false;
       }
       if (head->GetData() == number) {
           head = head->GetNext();
           return true;
       }
       else{
           while (temp != nullptr) {
           Node* curNode = temp;
           Node* preNode = nullptr;
           preNode = curNode->GetPrevious();
           temp = temp->GetNext();
               if (curNode->GetData() == number) {
                   preNode = curNode->GetNext();
                   return true;
               }
               delete curNode;
           }
       }
       delete temp;
   }
};

class Node {
protected:
   double data;
   Node* next;
   Node* previous;

public:
   Node(double initialData) {
      data = initialData;
      next = nullptr;
      previous = nullptr;
   }

   Node(double initialData, Node* nextNode, Node* previousNode) {
      data = initialData;
      next = nextNode;
      previous = previousNode;
   }

Edit: I'm able to solve my own issue, thank you everyone.编辑:我能够解决我自己的问题,谢谢大家。

bool Remove(double number) {
       // Your code here (remove placeholder line below)
       Node* temp = head;  //Make a temporary node point to head.
       
       if (temp == nullptr || head == nullptr) {  //if user don't provide input, return false.
           return false;
       }
       if (head->GetData() == number) {  //If number need to delete is at head.
           head = head->GetNext();
           return true;
       }
       else {
           while (temp != nullptr) {  //Travese temp node throught out a list.
               Node* curNode = temp->GetNext();  //Make a current node point at temp next.
               Node* preNode = temp;
               Node* sucNode = curNode->GetNext();
               if(curNode->GetData() == number) {  //Delete a node if number is found on the list
                   if (curNode->GetNext() == nullptr) {  //Delete at tail.
                       preNode->SetNext(nullptr);
                       tail = preNode;
                       delete curNode;
                       return true;
                   }
                   if (curNode->GetNext() != nullptr) {
                       preNode->SetNext(sucNode);
                       sucNode->SetPrevious(preNode);
                       delete curNode;
                       return true;
                   }
               }
               temp = temp->GetNext();
           }
       }
       return false;
   }
};

You should make Node a friend class of SortedNumberList or define former inside the later class which simplifies the code somewhat.您应该使 Node 成为 SortedNumberList 的朋友 class 或在后面的 class 中定义前者,这会稍微简化代码。 It's personal preference but it leads to less unnecessary boilerplate code (getters and setters).这是个人喜好,但它会减少不必要的样板代码(getter 和 setter)。

In a double linked list you do not need to keep track of the last as you do need on single linked lists because you have both pointers available.在双向链表中,您不需要像在单链表中那样跟踪最后一个指针,因为两个指针都可用。

The quest is just a matter of iterating to find the value, taking care to cut it early when we pass the mark since it is a sorted list.任务只是迭代查找值的问题,因为它是一个排序列表,所以在我们通过标记时注意尽早将其删除。

Then delete the object and update the link pointers.然后删除 object 并更新链接指针。

   bool Remove(double number) {
       // Loop through the entire list
       Node* temp = head;     
       while ( temp != nullptr) { 

           // There is no point looking forward if the list is sorted
           if (temp->data > number ) return false;

           // Compare to find
           if (temp->data == number) {

               // Get the pointers so we can delete the object
               Node* prev = temp->previous;
               Node* next = temp->next;

               // Delete object
               delete temp;

               // Update previous pointers
               if ( prev==nullptr ) {
                   head = next;
               } else {
                   prev->next = next;
               }

               // Update next pointers
               if ( next==nullptr ) {
                   tail = prev;
               } else {
                   next->previous = prev;
               }   

               // Indicate success
               return true;
           }
       }

       // We iterated to the end and did not find it
       return false;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM