【问题标题】:Failure to try to remove item not in LInkedList in C++尝试删除不在 C++ 链接列表中的项目失败
【发布时间】:2014-12-14 17:21:49
【问题描述】:

在一个简单的 LinkedList 类中,我试图删除一个对象,当该项目存在时它工作正常,但是当我尝试删除一个不存在的项目时,我的程序终止并说它刚刚停止工作......下面发布的是代码。有什么建议吗?

#include<iostream>
using namespace std;
class Node{
public:
    int data;
    Node* next;
    Node(){
        data=-1;
        next=NULL;
    }
    Node(int d){
        data=d;
        next=NULL;
    }
    Node(int d, Node* n){
        data=d;
        next=n;
    }
};
class LinkedList{
    public:
    Node* head;
    Node* dummy = new Node();
    LinkedList(){
        head=dummy;
    }
    LinkedList(Node* n){
        head=dummy;
        dummy->next=n;
    }
    void ins(Node* n){
        Node* current = head;
        while(current->next!=NULL&&current->next->data<=n->data){
            current=current->next;
        }
        n->next=current->next;
        current->next=n;
    }
    void print(){
        Node* current = head;
        while(current->next!=NULL){
            cout<<current->next->data<<endl;
            current=current->next;
        }
    }
    int peek(){
        if(head->next==NULL){
            cout<<"List is Empty"<<endl;
        }
        return head->next->data;
    }
    void rem(int toRemove){
        Node* current = head;
        while(current->next!=NULL&&current->next->data!=toRemove){
            current=current->next;
        }
        if(current->next->data==toRemove){
            current->next=current->next->next;
            cout<<"Removing Item"<<endl;
            return;
        }
        if(current->next->data!=toRemove){
            cout<<"No Item Found"<<endl;
            return;
        }
        if(current->next==NULL){
            cout<<"Not Removable since not there"<<endl;
            return;
        }
    }
};
int main(){
LinkedList* a = new LinkedList();
Node* n = new Node(5);
Node* nn = new Node(10);
Node* nnn = new Node(15);
Node* nnnn = new Node(12);
Node* nnnnn = new Node(7);
a->ins(n);
a->ins(nn);
a->ins(nnn);
a->ins(nnnn);
a->ins(nnnnn);
a->print();
a->rem(5);
a->print();
a->rem(13);
a->print();
return 0;
}

感谢任何帮助。谢谢,

【问题讨论】:

  • 我的建议是你在调试器中运行。
  • 我还建议您再次查看您的逻辑,包括循环和之后的检查。你认为当循环运行到结束时(当第一个条件为真时)会发生什么,因为找不到该项目?
  • 你为什么需要一个虚拟节点?
  • Dummy 可以减少插入和删除的情况

标签: c++ singly-linked-list


【解决方案1】:

在您的 rem() 函数中,您的 while 循环将您安全地带到一个不为空的节点,但在 while 循环之后,您不会检查 current->next 是否不为空。如果为 null,则在取消引用 current->next->data 时会崩溃。当我运行你的代码时发生了什么。

我建议循环直到你找到要删除的那个,而不是在你找不到它时循环——你可能永远找不到它。

【讨论】:

  • 我明白你的意思。非常感谢。
【解决方案2】:

在这个循环之后:

   while(current->next!=NULL&&current->next->data!=toRemove){
        current=current->next;

你可以有两种情况:

  • 您已找到您的数据:
  • current-&gt;next==NULL,因为您设法到达了列表的末尾

你执行的下一条语句是

    if(current->next->data==toRemove){

但是如果current-&gt;nextNULL,你尝试取消引用一个空指针并得到你的段错误!您必须首先检查您是否不在 NULL。

编辑:一旦你纠正了这个问题,我相信你还需要考虑其他问题:

  • 如果你想删除第一个元素怎么办?发生这种情况时,您不会预见到更新 head。无论如何,当您开始查看下一个数据时,您会错过它。 抱歉,我没有注意到虚拟节点;-)
  • 您的三个if 似乎是互斥的,因此您应该将它们与else 连接起来

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 2022-01-04
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    相关资源
    最近更新 更多