2012-10-07 214 views
0

從上次功能中刪除不能正常工作。它顯示該節點已被刪除,但當我顯示它時,它會陷入無限循環並顯示垃圾。無法弄清楚什麼是錯的!從鏈接列表中刪除節點

下面的代碼:

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; 
} 
+1

請儘量只張貼相關的代碼... –

+1

我建議你學習一些調試技巧。您可以使用調試器來遍歷代碼並查看變量的值,或者使用'cin'語句慷慨地輸出值。這將幫助您追蹤問題的出現位置。 –

+0

對不起,發佈第一次。當我粘貼正確縮進的代碼時,它刪除了所有的選項卡:(問題顯然是用delete_from_tail()函數。 – user1727119

回答

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"; 
} 
+0

謝謝dmakaitis。意義重大! :) – user1727119

+0

稍微調整了答案,正如Dietmar所說,利用維護Tail使事情變得簡單一些。 –

0

你肯定在刪除對象太多的Delete_At_Tail()(我認爲這是當你寫delete_from_tail()或「從去年刪除」你的意思是功能;當編程精度就是一切!)功能:你想擺脫一個對象,但你是三個對象,包括被刪除對象的前身。你應該只有delete你正在刪除的對象,沒有其他。除此之外,該功能看起來不錯。

順便說一句,既然你維持一個指向Tail的指針,你可以改進找到前輩的方法:你可以找到nextTail的節點。這避免了在尋找前任的循環中保持兩個變量最新的尷尬。

+0

我看到... ...感謝。請記住精確度!:) – user1727119