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

时间:2012-05-10 02:39:33

标签: c++

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

这是我的代码 当用户输入名称和贡献时,它将其存储在列表中。当我打印出列表时,只显示用户输入的姓氏。我认为它在我的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;
    }

}

1 个答案:

答案 0 :(得分: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;
}