3

我的链接列表程序有效,但它不会显示所有列表

这是我的代码当用户输入名称和贡献时,它会将其存储在列表中。当我打印出列表时,仅显示用户输入的姓氏。我认为它在我的 AddToList 函数中是问题谢谢

#include <string>

using namespace std;

struct PersonRec
{
    char aName[20];
    //string aName;
    int aBribe;
    PersonRec* link;
};


class PersonList
{

private:
    PersonRec *head;
    bool IsEmpty();


public:
    PersonList();
    ~PersonList();
    void AddToList();
    void ViewList();

};

#include <iostream>
#include <string>

using namespace std;

#include "personlist.h"

PersonList::PersonList()
{
    head = NULL;
}

PersonList::~PersonList()
{
    PersonRec *current, *temp;
    current = head;
    temp = head;
    while(current != NULL)
    {
        current = current->link;
        delete temp;
        temp = current;
    }
}


bool PersonList::IsEmpty()
{
    //PersonRec *p;

    if(head == NULL)//it has no nodes head will poin to NULL
    {
        return true;
    }

    else
    {
        //p = head;
        return false;
    }
}


void PersonList::AddToList()
{
    PersonRec *p;


    head = new PersonRec();   

    //if(head == NULL)
    //{
    if(!IsEmpty())
    {
        //head = new PersonRec();

        cout<<"Enter the person's name: ";
        cin.getline(head->aName, 20);
        //cin>>head->aName;
        cout<<"Enter the person's contribution: ";
        cin>>head->aBribe;
        //head->link = NULL;
        //}
    }    

    else
    {
        p = head;
        while(p->link != NULL)
            p = p->link;
        p->link = new PersonRec();
    }


}//end function

void PersonList::ViewList()
{
    PersonRec *p;
    p = head;

    if(IsEmpty())
    {
        cout<<"List is Empty "<<endl;
    }

    while(p != NULL)
    {
        cout<<p->aName<<" "<<"$"<<p->aBribe<<endl;
        p = p->link;
    }

}

#include <iostream>
#include "personlist.h"

using namespace std;

int displayMenu (void);
void processChoice(int, PersonList&);

int main()
{
    int num;

    PersonList myList;
    do 
    {
        num = displayMenu();
        if (num != 3)
            processChoice(num, myList);
    } while (num != 3);

    return 0;
}

int displayMenu(void)
{
    int choice;
    cout << "\nMenu\n";
    cout << "==============================\n\n";
    cout << "1. Add student to waiting list\n";
    cout << "2. View waiting list\n";
    cout << "3. Exit program\n\n";
    cout << "Please enter choice: ";
    cin >> choice;

    cin.ignore();
    return choice;
}

void processChoice(int choice, PersonList& p)
{
    switch(choice)
    {
    case 1: p.AddToList();
        break;
    case 2: p.ViewList();
        break;
    }

}
4

1 回答 1

4

想一想:head是指向列表中第一项的指针,而您要做的第一件事AddToList()是:

head = new PersonRec();

当您以这种方式覆盖时,您认为当前列表会发生什么?head

head在你准备好之前不要改变。基本的伪代码是:

newnode = new PersonRec;             # Don't overwrite head yet.
# Populate newnode with payload.
newnode-> next = head                # Put current list at end.
head = newnode                       # Now you can change it.

那就是如果您希望新节点位于列表的开头。如果你最后想要它,它会有点复杂,因为你要么必须:

  • 遍历列表寻找最后一个节点,以便您可以将新节点附加到它;或者
  • 保留指向最后一个节点的指针,以避免遍历。

但细节保持不变:在您可以通过其他方式访问当前列表之前,不要破坏您的头指针。


我应该提一下,根据您在 中注释掉的代码AddToList(),您似乎非常接近。在为真的head 情况下进行设置。isEmpty()但是您似乎已经对此发表了评论,并将该head = new ...位移到了声明之外/之前if。不确定你的思维过程到底发生了什么。

看起来您尝试做的事情是:

if isEmpty:
    head = new PersonRec;
    p = head
else:
    p = head
    while p->next != NULL:
        p = p->next
    p->next = new PersonRec;
    p = p->next

# Here, p is a pointer to the new node (head or otherwise)
# and the list is stable

p-> payload/link = whatever/null

最后一行也很重要,您的代码似乎并非在所有情况下都这样做(即,除了在列表中创建初始节点时)。


减少与语言无关的内容会给您带来类似(未经测试)的东西:

void PersonList::AddToList() {
    PersonRec *p;

    if(!IsEmpty()) {
        p = head = new PersonRec();
    } else {
        p = head;
        while (p->link != NULL)
            p = p->link;
        p->link = new PersonRec();
        p = p->link;
    }

    cout << "Enter the person's name: ";
    cin.getline (p->aName, 20);

    cout << "Enter the person's contribution: ";
    cin >> p->aBribe;

    p->link = NULL;
}
于 2012-05-10T02:49:28.380 回答