显示多个值链表

时间:2014-03-07 12:17:02

标签: c++ string struct linked-list nodes

我是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;

程序仅显示'Record#:'但不显示值。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我认为

之后
current -> next = new node;

你应该加上这个:

current = current->next;

因为您必须分配给您已分配的节点,而不是当前节点。