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()非常に近いように見えることに言及する必要があります。が trueの場合に設定し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 に答える