Reputation: 85
My linked lists program works, but it wont show all of the list
here is my code When the user enters a name and contribution it stores it in the list. When i print out the list only the last name that the user entered showed. I think its in my AddToList function is the 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'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;
}
}
Upvotes: 3
Views: 604
Reputation: 881333
Think about it: head
is a pointer to the first item in your list, and the first thing you do in AddToList()
is:
head = new PersonRec();
What do you think is going to happen to the current list when you overwrite head
in this manner?
Don't change head
until you're ready. The basic pseudo-code would 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.
That's if you wanted the new node at the start of the list. If you want it at the end, it's a little more complicated since you either have to:
But the detail remains the same: don't destroy your head pointer until such time as you have the current list accessible by some other means.
I should mention that, based on your commented out code in AddToList()
, you appear to be very close. Setting head
should be done in the case where isEmpty()
is true. But you appear to have commented that out and moved the head = new ...
bit to outside/before the if
statement. Not sure exactly what happened there with your thought processes.
It looks like what you tries to do was along the lines of:
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
That last line is also important, and your code doesn't seem to do it in all cases (ie, other than when you're creating the initial node in the list).
Making that a little less language-agnostic would give you something like (untested):
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;
}
Upvotes: 4