【问题标题】:Remove an element from a linked list in C++从 C++ 中的链表中删除一个元素
【发布时间】:2014-10-25 07:10:57
【问题描述】:

我现在正在尝试学习 C++,因为我将不得不参加一门课程,而且我来自 Java。我目前正在阅读“Jumping into C++”一书并完成练习。在阅读了关于链表的部分后,它告诉我创建自己的链表并拥有一个删除元素的方法(在练习中使用指针)。

到目前为止,我已经能够向我的链表添加值,并显示我的链表。在执行我的删除元素方法后,让我的程序明确告诉我它已删除特定内存地址处的值,我再次显示列表以发现我的值仍然以某种方式出现在据称已被删除的内存地址处。

这是我的 removeElement 方法:

// remove an element from the linked list
void removeElement(int remValue) {
    // to remove an element, we go through the list, find the value given
    // if we find it, stop
    // to remove, disconnect the link
    // relink the two values now (ie. value 1->2->3->NULL, 2 is removed, 1->3->NULL )
    LinkedList* current = head;
    LinkedList* next = current;
    while(current != NULL) {
        if(current->value == remValue) { // if match
            break; // break out of while
        }
        else {
            cout << "Value " << current->value << " does not match " << remValue << ".\n";
            next = current; // save in case
            current = current->pNextValue; // go to next value
        }
    } // end while
    if(current == NULL) { // if we reached end of list
        cout << "Can't remove value: no match found.\n"; // no match, cant remove
    } else { // found match
        cout << "Deleting: " << current << "\n";
        delete current;
        current = next->pNextValue; // current is updated
    }
}

这是我的链表的全部代码(包括一些测试以查看内容的去向):

http://pastebin.com/cHZr4cBa

我意识到我的大部分代码对于一个现实的链表来说效率不高,也不正常,我只是想弄清楚指针以及如何在最基本的链表中使用它们。

【问题讨论】:

  • 附带说明,在现代 C++ 中,您并不真正需要 delete(在 C++14 中也不是真正需要 new),因此您可能想找到更现代的 C++学习资料...

标签: c++ list pointers linked-list


【解决方案1】:

您实际上并没有取消链接您删除的节点。

您需要跟踪上一个节点,并使其next指针指向当前节点next节点。还要考虑当要删除的节点是第一个节点时的特殊情况。

【讨论】:

    【解决方案2】:

    @Joachim Pileborg 是对的,你需要记录之前的节点,而不是next

    试试这个代码:

    // remove an element from the linked list
    void removeElement(int remValue) {
        LinkedList* prev = head; // empty header
        LinkedList* current = head->pNextValue; // the first valid node
        while(current != NULL) {
            if(current->value == remValue) { 
                break; 
            }
            else {
                cout << "Value " << current->value << " does not match " << remValue << ".\n";
                prev = current; 
                current = current->pNextValue; // go to next value
            }
        }
        if(current == NULL) { // if we reached end of list or the list is empty
            cout << "Can't remove value: no match found.\n"; 
        } else {
            cout << "Deleting: " << current << "\n";
            prev->pNextValue = current->pNextValue; // unlink the node you remove
            delete current; // delete the node
        }
    }
    

    【讨论】:

      【解决方案3】:

      您需要更新列表中的链接以删除您删除的节点。

      请注意,使用delete 运算符不会更改内存中的任何值。它只是告诉操作系统您不再使用内存中的该位置,它可以将其回收用于其他用途。

      【讨论】:

        【解决方案4】:
        #include <iostream>
        #include <cstdlib>
        
        using namespace std;
        
        class Node
        {
        public:
            Node* next;
            int data;
            Node();
            ~Node();
            void print();
        };
        
        class LinkedList
        {
        public:
            int length;
            Node* head;
        
            LinkedList();
            ~LinkedList();
            void add(int data);
            void remove(int data);
            Node* search(int data);
            void print();
            void size();
        };
        
        Node::Node(){
            //set default values;
        }
        
        Node::~Node(){
            cout << "NODE DELETED" <<endl;
        }
        
        void Node::print(){
            Node* node = this;
            if(node != NULL){
                cout << "===============================" << endl;
                cout << this->data << endl;
                cout << "===============================" << endl;
            }
        }
        
        LinkedList::LinkedList(){
            this->length = 0;
            this->head = NULL;
        }
        
        LinkedList::~LinkedList(){
            cout << "LIST DELETED" <<endl;
        }
        
        void LinkedList::add(int data){
            Node* node = new Node();
            node->data = data;
            node->next = this->head;
            this->head = node;
            this->length++;
        }
        
        void LinkedList::remove(int data){
            if(this->length == 0){
                cout << "The list is empty" << endl;
            }else if(this->head->data == data){
                Node* current = head;
                this->head = this->head->next;
                delete current;
                this->length--;
            }else{
                Node* previous = this->head;
                Node* current = head->next;
                while(current != NULL) {
                    if(current->data == data) {
                        break;
                    }
                    else {
                        previous = current;
                        current = current->next;
                    }
                }
                if(current == NULL) {
                    cout << "Can't remove value: no match found.\n";
                } else {
                    previous->next = current->next;
                    delete current;
                    this->length--;
                }
            }
        }
        
        Node* LinkedList::search(int data) {
            Node* head = this->head;
            while(head){
                if(head->data == data){
                    return head;
                }else{
                    head = head->next;
                }
            }
            cout << "No match found.\n";
            return NULL;
        }
        
        void LinkedList::print(){
            if(this->length == 0){
                cout << "The list is empty" << endl;
            }else{
                Node* head = this->head;
                int i = 1;
                cout << "===============================" << endl;
                while(head){
                    cout << i << ": " << head->data << endl;
                    head = head->next;
                    i++;
                }
                cout << "===============================" << endl;
            }
        }
        
        void LinkedList::size(){
            cout << "List Length: " << this->length << endl;
        }
        
        
        int main(int argc, char const *argv[])
        {
            LinkedList* list = new LinkedList();
        
            for (int i = 0; i < 5; ++i)
            {
                if(i == 3){
                    list->add(105);
                    list->add(106);
                }
                list->add(rand() % 100);
            }
        
            list->print();
            list->size();
        
            list->remove(105);
        
            list->print();
            list->size();
        
            Node* node = list->search(106);
            node->print();
        
            delete list;
            return 0;
        }
        

        【讨论】:

          【解决方案5】:
          // C++ program to delete a node in
          
          // singly linked list recursively
          
           
          
          #include <bits/stdc++.h>
          
          using namespace std; 
          
           
          
          struct node { 
          
              int info; 
          
              node* link = NULL; 
          
              node() {} 
          
              node(int a) 
          
                  : info(a) 
          
              { 
          
              } 
          
          };
          
           
          
          /*
          
          Deletes the node containing 'info' part as val and
          
          alter the head of the linked list (recursive method)
          
          */
          
          void deleteNode(node*& head, int val) 
          
          {
          
               
          
              // Check if list is empty or we 
          
              // reach at the end of the 
          
              // list. 
          
              if (head == NULL) { 
          
                  cout << "Element not present in the list\n"; 
          
                  return; 
          
              } 
          
              // If current node is the node to be deleted 
          
              if (head->info == val) { 
          
                  node* t = head; 
          
                  head = head->link; // If it's start of the node head 
          
                                     // node points to second node 
          
                  delete (t); // Else changes previous node's link to 
          
                              // current node's link 
          
                  return; 
          
              } 
          
              deleteNode(head->link, val); 
          
          }
          
           
          
          // Utility function to add a 
          
          // node in the linked list
          
          // Here we are passing head by 
          
          // reference thus no need to
          
          // return it to the main function
          
          void push(node*& head, int data) 
          
          {
          
              node* newNode = new node(data); 
          
              newNode->link = head; 
          
              head = newNode; 
          
          }
          
           
          
          // Utility function to print 
          
          // the linked list (recursive
          
          // method)
          
          void print(node* head) 
          
          {
          
               
          
              // cout<<endl gets implicitly 
          
              // typecasted to bool value 
          
              // 'true' 
          
              if (head == NULL and cout << endl) 
          
                  return; 
          
              cout << head->info << ' '; 
          
              print(head->link); 
          
          }
          
           
          
          int main() 
          
          {
          
              // Starting with an empty linked list 
          
              node* head = NULL;
          

          【讨论】:

          【解决方案6】:

          代码基于@Some programmer dude 的回答。

          
          void removeElement(int remValue) {
              LinkedList * current, next, previous;
              current = *&head;
              
              while(current != NULL){
          
                  // `next` node
                  next = current->next;
          
                  // if match
                  if(current->value == remValue){
          
                      // if `previous` and `next` has a node, link the `previous` node to `next` node
                      if(previous != NULL && next != NULL) previous->next = next; 
          
                      // if `current` is the last node, link `previous` node to `NULL`
                      else if(previous != NULL && next == NULL) previous->next = NULL;
          
                      // if `current` node is the first node, set the header node to `next` node
                      else if(previous == NULL && next != NULL) *&head = next;
                      
                      // if `current` node is the only item, empty the list
                      else *&head = NULL;
          
                      cout << "Deleting: " << current->value << "\n";
          
                      // delete node
                      delete current;
          
                      // exit the function
                      return;
                  
                  // if not match
                  } else cout << "Value " << current->value << " does not match " << remValue << ".\n";
                  
          
                  // Variables for next loop
                  previous = current;
                  current = current->next;
              }
          
              // No Match
              cout << "Can't remove value: no match found.\n";
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-04
            • 2021-03-26
            • 2020-02-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多