So I am trying to wrap my head around link lists and the theory is there but the magic isn't (at least in my program)... my problem runs with the void traverseRecord() function. For some reason it automatically shows all the nodes.
Maybe I haven't totally grasped the concept of traversing, because I am thinking it will only show one at a time and I know my code isn't exactly right. Ideas anyone?
// studentGPAquery.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <list>
using namespace std;
struct student
{
string first_name;
string last_name;
double GPA;
student(){}
~student(){}
struct student *next;
};
student *head = NULL;
void mainMenu()
{
cout<<endl;
cout<<"++++++++++++++++++++++++++++++++++"<<endl;
cout<<" MAIN MENU "<<endl;
cout<<" Choose from the following: "<<endl;
cout<<"[N]umber of students in the class "<<endl;
cout<<"[A]dd a record "<<endl;
cout<<"[D]elete a record "<<endl;
cout<<"[V]iew all records "<<endl;
cout<<"[T]raverse all records "<<endl;
cout<<"[Q]uit program "<<endl;
cout<<"++++++++++++++++++++++++++++++++++"<<endl;
cout<<endl;
}
void addRecord()
{
cout<<endl;
cout<<"Add record:"<<endl;
cout<<endl;
struct student *temp, *alt;
temp = new student;
cout<<"What is the student's last name: ";
cin>>temp->last_name;
cout<<"What is the student's first name: ";
cin>>temp->first_name;
cout<<"What is student's GPA: ";
cin>>temp->GPA;
temp->next = NULL;
if(head == NULL)
head = temp;
else
{
alt = head;
while(alt->next != NULL)
{
alt = alt->next;
}
alt->next = temp;
}
}
void deleteRecord()
{
cout<<endl;
cout<<"Delete record:"<<endl;
cout<<endl;
student *del;
del = head;
del = del->next;
if(del->next == NULL)
cout<<"End of the list."<<endl;
else
del = del->next;
if(del == head)
cout<<"Beginning of list."<<endl;
else
{
student *prev;
prev = head;
while(prev->next != del)
{
prev = prev->next;
}
del = prev;
}
cout<<endl;
cout<<"Name to delete: ";
cin>>del->last_name;
if(del->next == NULL)
cout<<"Nothing follows."<<endl;
else
{
student *temp;
temp = del->next;
del->next = temp->next;
delete temp;
}
}
void viewRecord()
{
cout<<endl;
cout<<"All records:";
cout<<endl;
struct student *temp;
temp = head;
do
{
if(temp == NULL)
cout<<"Nothing follows."<<endl;
else
{
cout<<"Name: "<<temp->last_name<<", ";
cout<<temp->first_name<<endl;
cout<<"GPA: "<<temp->GPA<<endl;
cout<<endl;
temp = temp->next;
}
}
while(temp != NULL);
}
void traverseRecord()
{
struct student *temp;
temp = head;
if(temp != 0)
{
while(temp != 0)
{
cout<<"Name: "<<temp->last_name<<", ";
cout<<temp->first_name<<endl;
cout<<"GPA: "<<temp->GPA<<endl;
cout<<endl;
temp = temp->next;
}
}
while(temp != NULL)
{
cout<<"Name: "<<temp->last_name<<", ";
cout<<temp->first_name<<endl;
cout<<"GPA: "<<temp->GPA<<endl;
cout<<endl;
temp = temp->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char choice;
do{
mainMenu();
cout<<"Command?: ";
if(cin>>choice)
switch(choice)
{
case 'A':
addRecord();
break;
case 'D':
deleteRecord();
break;
case 'V':
viewRecord();
break;
case 'T':
traverseRecord();
break;
}
}
while(choice != 'Q');
cout<<endl;
cout<<"Terminating.."<<endl;
system("PAUSE");
return 0;
}
My other issue is sorting by last name, but I haven't reached that point yet in my code (one hurdle at a time)... but concept suggestions are welcome.
What I have lined up for sorting is:
left.last_name < right.last_name;
I'm still trying to figure out how to implement it.
~Climber Ty