c++ - 显示多值链表

标签 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++;
            }
}
                        ///////////////////////////////////////////////

////指针

信息书::信息书() {

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;

程序只显示“记录号:”而不显示值。有什么想法吗?

最佳答案

我想过

current -> next = new node;

你应该添加这个:

current = current->next;

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

关于c++ - 显示多值链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22249879/

相关文章:

C:二叉树,内存分配

c++ - CGAL:延伸线段直到多边形边界

c++ - 突出显示 sql 中的搜索阶段

c++ - 具有 `const` 和非 -`const` 版本的成员函数是否符合重载条件?

C# 文化特定符号

c - 字符指针的整数警告

C增加结构值

c++ - 带 nfs 挂载的 QFileSystemWatcher

c# - 将结束字符与列表进行比较

unit-testing - 如何在 Rust 中测试结构的实例化?