2014-10-28 90 views
1

我第一次使用指針。我有一個程序,它將數字插入鏈表中,打印列表並從列表中刪除特定數字。除了當我嘗試刪除最後插入的號碼時,它工作。如何刪除添加到C++鏈接列表中的最後一個元素

Node.h

#ifndef Node_h 
#define Node_h 

#include <iostream> 
using namespace std; 

class Node 
{ 
public: 
    int data; 
    Node *next; 

public: 
Node(); 
}; 

#endif 

Node.cpp

#include "Node.h" 

Node::Node() 
{ 
} 

LinkedList.h

#ifndef LinkedList_h 
#define LinkedList_h 

#include "Node.h" 

class LinkedList 
{ 
    private: 
    Node *pL; 

public: 
    LinkedList(); 
    void insert(int nr1); 
    void deleteNr(int nr1); 
    void printL(); 
}; 

#endif 

LinkedList.cpp //該程序創建一個 「鏈接列表」的數字

#include "LinkedList.h" 

LinkedList::LinkedList() 
{ 
    pL = NULL; 
} 

void LinkedList::insert(int nr1) 
{ 
    Node *p = new Node; 
    p->data = nr1; 
    p->next = pL; 
    pL = p; 
} 

void LinkedList::deleteNr(int nr1) 
{ 
    Node *p = pL; 
    Node *p2 = pL; 
    while (p != NULL & p->data != nr1) 
    { 
     p2 = p; 
     p = p->next; 
    } 

    if (p != NULL) 
    { 
     p2->next = p->next; 
     delete p; 
    } 
} 

void LinkedList::printL() 
{ 
    Node *p = pL; 

    while (p != NULL) 
    { 
     cout << p->data << "-> "; 
     p = p->next; 
    } 
} 

的main.cpp

#include "LinkedList.h" 

int menu(); 

//////// main ///////// 
int main() 
{ 
    int choice1, nr1; 
    LinkedList lk1; 

    choice1 = menu(); 

    while (choice1 <= 3) 
    { 
     if (choice1 == 1) 
     { 
      cout << "Enter number." << endl; 
      cin >> nr1; 
      lk1.insert(nr1); 
     } 

     else if (choice1 == 2) 
     { 
      cout << "Enter number." << endl; 
      cin >> nr1; 
      lk1.deleteNr(nr1); 
     } 

     else if (choice1 == 3) 
     { 
      lk1.printL(); 
      cout << endl << endl; 
     } 

     else if (choice1 == 4) 
     { 
      cout << "Exit the program." << endl; 
      system("pause"); 
      exit(1); 
     } 

     choice1 = menu(); 
    } // end while loop 
} 

int menu() 
{ 
    int choice1; 

    cout << "1. Insert a number into the linked-list." << endl; 
    cout << "2. Delete a number from the linked-list." << endl; 
    cout << "3. Print the linked-list." << endl; 
    cout << "4. Exit the program." << endl; 
    cout << "Enter choice." << endl; 
    cin >> choice1; 

    return choice1; 
} 
+2

家庭作業?當您嘗試刪除最後插入的項目時會發生什麼? – Alex 2014-10-28 18:01:26

+0

是的,這是一項家庭作業。當我嘗試刪除最後插入的項目時,它會崩潰。 – user3254558 2014-10-28 18:21:05

+0

請注意,在你的刪除函數中,你需要一個'&',它需要'&&'。他們做不同的事情。 – Daniel 2014-10-28 18:25:41

回答

0

您需要添加代碼來處理給定的輸入對應於列表中的第一項的情況。

void LinkedList::deleteNr(int nr1) 
{ 
    Node *p = pL; 

    if (p != NULL && p->data == nr1) 
    { 
     pL = p->next; 
     delete p; 
     return; 
    } 

    Node *p2 = pL; 
    while (p != NULL && p->data != nr1) 
    { 
     p2 = p; 
     p = p->next; 
    } 

    if (p != NULL) 
    { 
     p2->next = p->next; 
     delete p; 
    } 
} 
+0

這工作完美。謝謝!你能簡單地解釋我做錯了什麼,你糾正了什麼?我很困惑。 – user3254558 2014-10-28 18:39:50

+0

檢查您的&運營商。此代碼不正確。 – Daniel 2014-10-28 18:39:58

+0

@丹尼爾,它是固定的。 – 2014-10-28 18:56:26

1

你的問題是,通常情況下,P2背後是一覽P一個節點,但如果第一個節點被刪除,那麼第一個while循環中刪除功能有0迭代和p2和p是相同的。頭部被刪除,但pL未更新。它只是指向未分配的內存。這可能會使它看起來像節點未被刪除,或者它可能導致分段錯誤和崩潰。無論哪種方式,這是錯誤的行爲。您需要確保檢查要刪除的節點是第一個節點並更新pL的情況。

嘗試這樣的事情

void LinkedList::deleteNr(int nr1) 
{ 
    Node *p = pL; 
    Node *p2 = pL; 
    if(p != NULL && nr1 == p->data) 
    { 
     pL = p->next; 
     delete p; 
     return; 
    } 

    while (p != NULL && p->data != nr1) 
    { 
     p2 = p; 
     p = p->next; 
    } 

    if (p != NULL) 
    { 
     p2->next = p->next; 
     delete p; 
    } 
} 

如果您希望能夠刪除nr1所有實例鏈接列表,你需要添加另一個循環:

void LinkedList::deleteNr(int nr1) 
{ 
    Node *p = pL; 
    while(p != NULL && nr1 == p->data) 
    { 
     pL = p->next; 
     delete p; 
     p = pL; 
    } 
    Node *p2 = pL; 

    while (p != NULL) 
    { 
     p2 = p; 
     p = p->next; 
     if(nr1 == p->data) 
     { 
      p2->next = p->next; 
      delete p; 
     } 
    } 
} 
+0

這工作,並不起作用。是的,您可以刪除最後輸入的號碼,因爲它會按照與輸入相反的順序刪除列表中的號碼,但不能刪除特定的號碼。 – user3254558 2014-10-28 18:30:07

+0

@ user3254558我不明白你的意思? – Daniel 2014-10-28 18:35:19

+0

@ user3254558這將刪除鏈接列表中編號爲nr1的所有實例。 – Daniel 2014-10-28 18:41:48

-1
void LinkedList::deleteLast() 
{ 

Node *p = pL; 

if(p == NULL) 
    return; 
else if(p->next == NULL) { 
    p = NULL; 
} 
else { 
    while (p->next->next != NULL) 
    { 
    p = p->next; 
    } 

    p->next = NULL; 
} 
} 
+1

我不認爲這是OP正在尋找的答案。問題的標題有點誤導。另外,這個代碼是用什麼語言編寫的?它看起來像是Java和C++的組合。 – Daniel 2014-10-28 18:15:46

+0

這是C++語言。這是一個全新的功能,專門刪除最後一個元素並在主函數中調用? – user3254558 2014-10-28 18:30:28

+0

@ user3254558它使用Java的小寫null。也不刪除節點,這也是類似於Java的節點。 – Daniel 2014-10-28 18:38:16

相關問題