2014-03-07 66 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