删除链接列表节点,C​​ ++函数不起作用

时间:2018-12-28 08:11:06

标签: c++ linked-list

#include <iostream>
using namespace std;
class List {
public:
    struct node {
        int data;
        node *next;
    };
    node* head = NULL;
    node* tail = NULL;
    node* temp = NULL;
    node* prev = NULL;
public:
    void addNum(int num) {
        temp = new node;
        temp->data = num;
        temp->next = NULL;
        if (head == NULL) {
            head = temp;
            tail = temp;
        }
        else {
            tail->next = temp;
            tail = temp;
        }
    }
    void PrintList() {
        temp = head;
        while (temp != NULL) {
            cout << temp->data << endl;
            temp = temp->next;
        }
    }
    void DelNum(int num) {
        temp = head;
        while (temp != NULL) {
            if (temp->data == num) {
                prev->next = temp->next;
                free(temp);
            }
            temp = prev;
            temp = temp->next;
        }
    }
};

int main() {

    List list;
    list.addNum(1);
    list.addNum(2);
    list.addNum(3);
    list.addNum(4);
    list.addNum(5);
    list.addNum(6);

    list.DelNum(3);
    list.PrintList();
    return 0;
}

我的DelNum函数怎么了?当我运行程序时,什么都没有弹出。我输入多少数字都没关系。

2 个答案:

答案 0 :(得分:1)

正如女士指出的那样,问题出在分配temp = prev;的DelNum()函数中。在初始化时,您定义了node* prev = NULL;因此,将prev = NULL分配给temp时,当尝试像temp = temp->next;一样使用时会导致分段错误。

答案 1 :(得分:0)

DelNum函数中存在两个主要问题:

首先,在while循环中 ,您应该分配

prev = temp; 

第二秒,找到目标元素后,将其删除后必须跳出循环,这在代码中没有完成

下面是您的更正代码(也对DelNum函数中其他一些极端情况的更正):

#include <iostream>
using namespace std;
class List {
public:
    struct node {
        int data;
        node *next;
    };
    node* head = NULL;
    node* tail = NULL;
    node* temp = NULL;
    node* prev = NULL;
public:
    void addNum(int num) {
        temp = new node;
        temp->data = num;
        temp->next = NULL;
        if (head == NULL) {
            head = temp;
            tail = temp;
        }
        else {
            tail->next = temp;
            tail = temp;
        }

      cout<<num<<" is added \n";
    }
    void PrintList() {
        temp = head;
        while (temp != NULL) {
            cout << temp->data << endl;
            temp = temp->next;
        }
    }
        void DelNum(int num) {

        if(head==NULL)//empty
        {
            cout<<"empty linked list, can't be deleted\n";
            return;
        }
        if(head->next==NULL)//means only one element is left
        {
            if(head->data==num)
            {
                node * fordelete=head;
                head=NULL;
                cout<<num<<"is deleted\n";
                delete(fordelete);
            }
            else
            {
                cout<<"not found , can't be deleted\n";
            }
            return;

        }
       temp = head;  // when more than one element are there
       prev = temp;
        while (temp != NULL) {

            if (temp->data == num) {
                prev->next = temp->next;
                free(temp);

            cout<<num<<" is deleted\n";
                break;

            }
            prev= temp;
            temp = temp->next;
        }
        if(temp==NULL)
        {
            cout<<"not found, can't be deleted\n";
        }
    }
};

int main() {

    List list;
    list.addNum(1);
    list.addNum(2);
    list.addNum(3);
    list.addNum(4);
    list.addNum(5);
    list.addNum(6);

    list.PrintList();
    list.DelNum(3);
    list.DelNum(7);
    list.PrintList();
    return 0;
}

希望它能对您有所帮助。