从链表中删除节点

时间:2012-10-07 18:35:07

标签: c++ data-structures linked-list

从上一个功能删除无法正常工作。它显示该节点已被删除,但当我显示它时,它进入一个无限循环并显示垃圾。无法想出什么是错的!

以下是代码:

using namespace std;
class List
{
    struct NODE
    {
        int item;
        NODE *next;
    };
    NODE *Head,*Tail;
    public:
    List()
    {
        Head=NULL;
        Tail=NULL;
    }
    ~List()
    {
        while(Head->next!=NULL)
        {
            Delete_At_Head();
        }
        Delete_At_Head();
    }
    void Add_At_First(int);
    void Add_At_Last(int);
    void Delete_At_Head();
    void Delete_At_Tail();
    int Is_Empty();
    void display();
};
void List::Add_At_First(int data)
{
    NODE *temp;
    temp=new NODE;
    if(Head==NULL)
    {
        temp->item=data;
        temp->next=NULL;
        Head=temp;
        Tail=Head;
    }
    else
    {
        temp->item=data;
        temp->next=Head;
        Head=temp;
    }
    cout<<"Node added at first!\n";
}
void List::Add_At_Last(int data)
{
    NODE *temp;
    temp=new NODE;
    temp->item=data;
    temp->next=NULL;
    if(Head==NULL)
    {
        Head=temp;
        Tail=temp;
    }
    else
    {
        Tail->next=temp;
        Tail=temp;
    }
    cout<<"Node added at last!\n";
}
void List::Delete_At_Head()
{
    NODE *temp;
    temp=new NODE;
    temp->item=Head->item;
    temp->next=Head->next;
    delete Head;
    Head=temp->next;
    delete temp;
    cout<<"Node deleted from head!\n";
}
void List::Delete_At_Tail()//Problematic part
{
    NODE *temp,*prev;
    temp=new NODE;
    prev=new NODE;
    temp=Head;
    while(temp->next!=NULL)
    {
        prev=temp;
        temp=temp->next;
    }
    prev->next=NULL;
    delete temp;
    delete Tail;
    Tail=prev;
    delete prev;
    cout<<"Node deleted from tail!\n";
}
int List::Is_Empty()
{
    if(Head==NULL)
        return 1;
    else
    return 0;
}
void List::display()//does not display after delete from tail
{
    NODE *temp;
    temp=new NODE;
    temp->item=Head->item;
    temp->next=Head->next;
    do
    {
        cout<<temp->item<<"-->";
        temp=temp->next;
    }while(temp->next!=NULL);
    cout<<temp->item;
}
int main()
{
    List obj;
    int ch,data;
    do
    {
        cout<<"\n1.Display\n2.Add at first\n3.Add at last\n4.Delete at 
head\n5.Delete at tail\n6.Exit\nEnter your choice: ";
        cin>>ch;
        switch(ch)
        {
            case 1:
            {
                if(obj.Is_Empty())
                    cout<<"List is Empty!\n";
                else
                    obj.display();
                break;
            }
            case 2:
            {
                cout<<"Enter data: ";
                cin>>data;
                obj.Add_At_First(data);
                break;
            }
            case 3:
            {
                cout<<"Enter data: ";
                cin>>data;
                obj.Add_At_Last(data);
                break;
            }
            case 4:
            {
                if(obj.Is_Empty())
                    cout<<"List is Empty!\n";
                else
                    obj.Delete_At_Head();
                break;
            }
            case 5:
            {
                if(obj.Is_Empty())
                    cout<<"List is Empty!\n";
                else
                    obj.Delete_At_Tail();
                break;
            }
            case 6:
            {
                break;
            }
        }
    }while(ch!=6);
    return 0;
}

2 个答案:

答案 0 :(得分:0)

你肯定在Delete_At_Tail()中删除了太多的对象(我假设这是你写delete_from_tail()或“从最后删除”时的意思;编程精度就是一切!)函数:你想摆脱一个对象,但你是delete三个对象,包括被删除对象的前身。您应该只delete要删除的对象而不是其他对象。除此之外,该功能看起来还不错。

顺便说一下,由于您正在维护指向Tail的指针,因此您可以改进查找前一代的方法:您可以找到nextTail的节点。这避免了在循环中保持两个变量最新的笨拙需要,以寻找前一个。

答案 1 :(得分:0)

以下内容应正确删除列表中的最后一项:

void List::Delete_At_Tail()
{
    if(Head == NULL) return;

    // If only one item in the list, delete it and empty the list...
    if(Head->next == NULL) {
        delete Head;
        Head = NULL;
        Tail = NULL;
        return;
    }

    // Find the last item in the list
    NODE *temp = Head;
    while(temp->next!=Tail)
    {
        temp=temp->next;
    }

    delete Tail;
    temp->next=NULL;
    Tail=temp;
    cout<<"Node deleted from tail!\n";
}