2014-03-07 102 views
0

我是C++新手,目前正在嘗試關於鏈接列表,並且在我的程序中顯示多個值時遇到問題。我知道問題出在指針(DisplayAll函數)的某處,但我不知道如何解決它。顯示多個值鏈接列表

node* InfoBook::AddNode(nodePtr temp) 
{ 
    string firstname; 
    string lastname; 
    string phonenumber; 
    string dayofbirth; 
    string monthofbirth; 
    string yearofbirth; 
    string age; 
    string streetname; 
    string city; 
    string state; 
    string zipcode; 
    InfoBook ad; 

     if(head != NULL) 
     { 
      current = head; 
      while(current -> next != NULL) 
      { 
       current = current -> next; 
      } 
      current -> next = new node; 
      current -> firstname = temp -> firstname; 
      current -> lastname = temp -> lastname; 
        ////code here to add the other values//// 
      current -> zipcode = temp -> zipcode; 
      current -> next -> next = nullptr; 
      return current; 
      ad.userPromptStatement(); 
     } 
     else 
     { 
      head = new node; 
      head -> firstname = temp -> firstname; 
      head -> lastname = temp -> lastname; 
        ////code here to add the other values//// 
      head -> zipcode = temp -> zipcode; 
      head -> next = nullptr; 
      return current; 
     } 
} 

////////////////////////////////DisplayAll///////////////////////////////// 

void InfoBook::DisplayAll() 
{ 
    current = head; 
    int count = 1; 
    string firstname; 
    string lastname; 
    string phonenumber; 
    string dayofbirth; 
    string monthofbirth; 
    string yearofbirth; 
    string age; 
    string streetname; 
    string city; 
    string state; 
    string zipcode; 

     if(current == nullptr) 
     { 
      cout << "\n\n\No Record exists."; 
     } 
      while(current != NULL) 
      {  ////////I know the problem is somewhere between here//////// 
       cout << "Record # " << count << " : "; 
       cout << current -> firstname << endl; 
       cout << current -> lastname << endl; 
       cout << current -> phonenumber << endl; 
       cout << current -> dayofbirth << endl; 
       cout << current -> monthofbirth << endl; 
       cout << current -> yearofbirth << endl; 
       cout << current -> age << endl; 
       cout << current -> streetname << endl; 
       cout << current -> city << endl; 
       cout << current -> state << endl; 
       cout << current -> zipcode << endl; 
       cout <<"\n\n\n"; 
       current = current -> next; 
       count++; 
      } 
} 
         /////////////////////////////////////////////// 

////指針

InfoBook :: InfoBook() {

head = NULL; 
current = NULL; 
temp = NULL; 

}

////////

class InfoBook 
{ 
private: 
    nodePtr head; 
    nodePtr current; 
    nodePtr temp; 

public: 
    InfoBook(); 

    void userPromptStatement(); 
    node* AddNode(nodePtr); 
    void DisplayAll(); 

/////////////

typedef struct node 
{ 
    string firstname; 
    string lastname; 
    string phonenumber; 
    string dayofbirth; 
    string monthofbirth; 
    string yearofbirth; 
    string age; 
    string streetname; 
    string city; 
    string state; 
    string zipcode; 
    static int count; 
    node* next; 
} *nodePtr; 

該程序只顯示'記錄#:'但不是值。有任何想法嗎?

+0

http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Loghorn

回答

1

我想以後

current -> next = new node; 

你應該補充一點:

current = current->next; 

,因爲你必須分配給您已經頁頭,不是當前的節點。

+0

此外,你可以看看:http://www.thelearningpoint.net/computer-science/data-structures-singly-linked-list-with-c-program-source-code – nvg58

+0

這很可能是正確的,儘管這個列表中的最後一件事情'current - > next - > next = nullptr'應該有一個'下一步'以避免調用未定義的行爲。 – WhozCraig

+0

@ nvg58沒有工作,實際上是在AddNode的問題? – user3391677