My work with linked lists works, but it does not display the whole list

Working with my linked lists works, but it does not display the whole list

here is my code When a user enters a name and contribution, he saves it in a list. When I print the list, only the last name entered by the user is displayed. I think its in my AddToList function is a problem Thanks

#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 name: ";
        cin.getline(head->aName, 20);
        //cin>>head->aName;
        cout<<"Enter the person 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;
    }

}
+3
source share
1 answer

Think about it: head- this is a pointer to the first element in your list, and the first thing you do in AddToList():

head = new PersonRec();

What do you think will happen to the current list when overwriting headthis way?

Do not change headuntil you are ready. The main pseudo-code will be:

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.

, node . , , :

  • , node, node;
  • node, .

: , , - .


, , AddToList(), , , . head , isEmpty() - true. , , 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

, , , (.. , node ).


- ():

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 name: ";
    cin.getline (p->aName, 20);

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

    p->link = NULL;
}
+4

All Articles