2016-04-20 100 views
2

我想寫一個反向打印功能作爲雙向鏈表的一部分。以下是我寫的相關功能:C++雙向鏈表反向打印

void PLAYER::AddNode(int addID, std::string addName){ 

nodePtr n = new node; //creates a new node pointer 
n->next = NULL;  //Make next null 
n->prev = NULL;  // this will set this to be the ending node 
n->ID = addID;  //These two lines pass the information into the node 
n->name = addName; // ID# and Name Information 

if(head != NULL){ // This checks to see if a list is set up. 
    curr = head; // Make this point to the head. 
    while(curr->next != NULL){ // Loops through until the NULL is found 
     curr = curr->next; 
    } 
    curr->next = n; //Make the currnet node point to N 
    n->prev = curr; //Make the previous node connect to curr 
    n->next = tail; // connect new node to the tail. 

} 
else{ 
    head = n; //If there is no list, this makes N the first node. 

} 

這裏是要使用的函數原型的類。

class PLAYER 
{ 
public: // Functions go inside PUBLIC 
    PLAYER(); 
    void AddNode(int addID, std::string addName); 
    void DeleteNode(int delPlayer); 
    void SortNode(); 
    void PrintList(); 
    void InsertHead(int AddID, std::string addName); 
    void PrintReverse(); 

private: //Variables go into here 
    typedef struct node{ 
      // ... 
      std::string name; 
      int ID; 
      node* next; 
      node* prev; 
    }* nodePtr; 


    nodePtr head, curr, temp, prev, test, tail; 


}; 

最後我試圖創建一個反向遍歷函數向後打印。

void PLAYER::PrintReverse() 
{ 
    curr = head; 
    while(curr->next != NULL) //Get to the end of the list 
    { 
     curr = curr->next; 
    } 
    while(curr->prev != NULL) //Work backward and print out the contents 
    { 
     std::cout << curr->ID << " " << curr->name << endl; 
     curr = curr->prev; 
    } 
} 

我想什麼做的是PrintReverse()函數中有它通過尾指針初始化,但我不能爲了弄清楚功能添加到PrintReverse()和ADDNODE()來有尾部指向的新節點。

這是我在這裏發佈的第一個問題,我希望我覆蓋了我的所有基地。感謝您的幫助,我可以找到。

編輯:

謝謝你的所有輸入。我重新學習數據結構,是的,這是我自己強加的作業,開始讓邏輯再次流動。

今晚我回家後我會做出改變。

+3

你的代碼有兩個問題:(i)你在哪裏初始化尾部? (ii)在AddNode中,爲什麼不直接使用tail,而不是迭代直到結束,(iii)在PrintReverse函數中,你的類中已經有了尾節點nodePtr,爲什麼不從那裏開始呢? –

+1

根據你有多少編程經驗,實現一個雙向鏈表可能有點棘手。爲什麼不使用STL的列表容器? –

+3

@AmmarHussain我認爲99%的鏈表問題是作業分配。 –

回答

1

需要考慮以下更改。

PrintReverse函數將不需要正向傳遞來獲得tail

void PLAYER::PrintReverse() 
{ 
    curr = tail; 
    while(curr != NULL) //Work backward and print out the contents 
    { 
     std::cout << curr->ID << " " << curr->name << endl; 
     curr = curr->prev; 
    } 
} 

AddNode函數中如何處理尾部存在問題。看到那裏的註釋包含[CHANGED][ADDED]行:

if(head != NULL){ // This checks to see if a list is set up. 
    curr = head; // Make this point to the head. 
    while(curr->next != NULL){ // Loops through until the NULL is found 
     curr = curr->next; 
    } 
    curr->next = n; //Make the currnet node point to N 
    n->prev = curr; //Make the previous node connect to curr 
    n->next = NULL; // [CHANGED]: we want the last node not to have a successor. 
} 
else{ 
    head = n; //If there is no list, this makes N the first node. 
} 

tail = n;  // [ADDED]: the last node added is the new tail. 

但是,一個簡單的解決方案是避免再次直傳,從尾部開始。

if(tail != NULL){ // This checks to see if a list is set up. 
    tail->next = n; //Make the old tail node point to N 
    n->prev = tail; 
    n->next = NULL; 
} 
else{ 
    head = n; //If there is no list, this makes N the first node. 
} 

tail = n;  // The last node added is the new tail. 
+1

在'PrintReverse()'中,你應該在循環之前檢查'tail'是否爲NULL,以防列表爲空。 –

+0

謝謝!編輯。 – qwertyman

+1

'PrintReverse()'也在跳過列表中的第一個節點。你應該使用'while(curr!= NULL)'而不是'while(curr-> prev!= NULL)' –