1. Write down a node structure for this case:
A teacher wants to manage each student’s master list with a computer
program. A list structure holds the following data; studID, studName,
Course, and Yr_level.
2. Create a program that will accept elements from the user, based on the created
node structure above. The number of elements depends on the no. of items
given by the user. Be able to display all the elements.
#include<iostream>
#include<string>
#include<list>
using namespace std;
struct Student
{
double studID;
string studName;
string course;
int Yr_level;
struct Student* next;
};
Student* head=NULL;
void Menu()
{
cout<<"--------MENU--------------\n"
<<"Choose something from the following\n"
<<" [A]dd new students\n"
<<" [V]iew all students\n"
<<" [Q]uit program\n";
}
void AddStudent(int num)
{
for(int i=1;i<=num;i++)
{ struct Student* temp=new Student;
cout<<"Please, enter the student`s "<<i<<" ID\n";
cin>>temp->studID;
cout<<"Please, enter the student`s "<<i<<" Name\n";
cin>>temp->studName;
cout<<"Please, enter the student`s "<<i<<" Course\n";
cin.ignore(256,'\n');
getline(cin,temp->course,'\n');
cout<<"Please, enter the student`s "<<i<<" Yr.level\n";
cin>>temp->Yr_level;
temp->next=NULL;
if(head==NULL)
head=temp;//temp is the first element of list
else
{
//temp is not the first element of list
//Find the last element of list
Student* p=head;
while(p->next!=NULL)p=p->next;
p->next=temp;//enter a new temp element in the end of list
}
}
}
void ViewListOfStud()
{
struct Student* temp=head;
if(temp==NULL)cout<<"List of students is empty\n";
else
{
cout<<"Full list of students:\n";
cout<<"ID"<<"\tName"<<"\tCourse"<<"\tYr.level\n";
while(temp!=NULL)
{
cout<<temp->studID
<<"\t"<<temp->studName
<<"\t"<<temp->course
<<"\t"<<temp->Yr_level<<"\n";
temp=temp->next;
}
}
}
int main()
{
char ch;
while(ch!='Q')
{
Menu();
cout<<"Please, enter a command:";
if(cin>>ch)
switch(ch)
{
case 'A':
{
int num;//number of elements
cout<<"Please, enter a number of students to add: ";
cin>>num;
AddStudent(num);
break;
}
case 'V':
ViewListOfStud();
break;
case 'Q':continue;
default:
cout<<"Incorrect choice, retry, please\n";
}
}
return 0;
}
Comments
Leave a comment