Problem Statement:

In the lesson you learned to save only one integer element as data in a node. Please note, it is not compulsory to have only one value in a node. There can be more than one and different types of elements as data of a node but pointer will remain one for singly linked list.

Write a C++ program to implement linked list data structure. In this problem every node should have two data elements called name and course of a student and a pointer to link the current node to the next node. Your program should prompt the user to enter name and course code of a student and save it into a Node (Student). User can enter the records of multiple students. So you need to create a linked list of students. One node of list will represent one student.

Instead of saving the name of course you should use code of course into Student node. This will help you save lots of memory. “short int” type variable can be used to save code of course which will take two bytes while name of course will take many bytes. This technique will help you save lots of memory.

Use the following course names and codes while entering the record of a student. (you can change courses)

Course NameCourse Code
Introduction to Computing1
Introduction to Programming2
Data Structures3
Object Oriented Programming4
IT Series5
C++6
VBA7
Math A Course8

After saving the required information of all students, you need to perform following operations operated through menu showing in sample output video.

  • Show the names of students enrolled in a specific or all courses (user will enter a choice of operation).
  • Show the count of students enrolled in a specific or all courses (user will enter a choice of operation).
  • Option to close the program by selection from menu as showing in sample output video.

Sample Output:

            For sample output watch the video file “A1 Sample Output.mp4” attached with this assignment file below.

#include <iostream>
#include <string>

using namespace std;

class Node{
	
	public: 
			string name;
			short int c_code;
			Node* next;	
			
};

class Student{
	
	private:
             Node* head;
             public:	
             	Student(){
             		
             		head = '\0';
				 }
				 
				 void insert_Student(Node**,Node*);
				 void newNode();
				 void print_List(int code);
				 
};

void Student::	insert_Student(Node** head_ref,Node* newNode){
	Node* current;
	if(*head_ref =='\0'){
		*head_ref = newNode;
	}
	else{
		current = *head_ref;
	
	while(current->next != '\0'){
		current = current->next;
	
	}
	
		newNode->next = current->next;
		current->next = newNode;
}
		
		cout<<"Student's information saved successfully."<<endl;
		
}			


void Student::	newNode(){
	short int c_code;
	string name; 
	cout<<"Enter the name of Student: ";
	cin>>name;
	cout<<endl;
	cout<<"1. Introduction to Computing "<<endl;
	cout<<"2. Introduction to Programming"<<endl;
	cout<<"3. Data Structure"<<endl;
	cout<<"4. Object Oriented Programming"<<endl;
	cout<<"5. IT Series "<<endl;
	cout<<"6. C++ "<<endl;
	cout<<"7. VBA "<<endl;
	cout<<"8. Math A Course "<<endl;
	cout<<"Enter the course code: ";
    cin>>c_code;
		
		Node* new_Node=new Node();
		new_Node->name = name;
		new_Node->c_code=c_code;
		new_Node->next='\0';
		insert_Student(&this->head,new_Node);
}	


void Student::print_List(int code){
	Node* temp =head;
	int count=0;
	if(temp=='\0')
	{
		cout<<endl<<"No record found "<<endl;
		
	}
	
	else{
		while(temp!=NULL){
			
			if(code==0){
				count++;
				cout<<"student name : "<<temp->name<<endl;
			}
			
				if(temp->c_code==code)
				{
				count++;
				cout<<"student name : "<<temp->name<<endl;
			
			}
				temp=temp->next;
			
		}
		
		cout<<"Enrollment count :"<<count<<endl ;
	}
}

int main (){
	
	
	Student *myList=new Student();
	
	bool check=true; bool check_1=true;
	char choice;int newChoice;
	do{
		myList->newNode();
		cout<<endl <<"Do you want to add another student? y/n:  ";
		cin>>choice;
		
		if(choice=='n' || choice=='N'){
			check=false;
		} 
	}while(check);
	
	
	do{
		cout<<endl;
		cout<<"0. Display all students"<<endl ;
		cout<<"1. Display all students enrolled in Introduction to Computing"<<endl ;
		cout<<"2. Display all students enrolled in Introduction to Programming"<<endl ;
		cout<<"3. Display all students enrolled in Data Structure"<<endl ;
		cout<<"4. Display all students enrolled in Object Oriented Programming"<<endl ;
		cout<<"5. Display all students enrolled in IT Series"<<endl ;
		cout<<"6. Display all students enrolled in C++"<<endl ;
		cout<<"7. Display all students enrolled in VBA"<<endl ;
		cout<<"8. Display all students enrolled in Math A Course"<<endl ;
		cout<<"9. Close the program "<<endl;
		cout<<endl;
		cout<<"Select an option for required operation: ";
		cin>>newChoice;
		
		               
		               switch(newChoice)
					   {
		               	
		               	case 0:
		               		cout<<endl;
		               		cout<<"Following students are enrolled in all courses. "<<endl;
		               		myList->print_List(newChoice);
		               		break;
		               		
               		 	case 1:
               		 		cout<<endl;
		               		cout<<"following student enroll in Introduction to Computing "<<endl;
		               		myList->print_List(newChoice);
		               		break;
		               	case 2:
		               		cout<<"following student enroll inIntroduction to Programming "<<endl;
		               		myList->print_List(newChoice);
		               		break;
		               	case 3:
		               		cout<<"following student enroll in dat structure "<<endl;
		               		myList->print_List(newChoice);
		               		break;
		               	case 4:
		               		cout<<"following student enroll in  Object Oriented Programming "<<endl;
		               		myList->print_List(newChoice);
		               		break;
		               	case 5:
		               		cout<<"following student enroll in  IT Series "<<endl;
		               		myList->print_List(newChoice);
		               		break;
		               	case 6:
		               		cout<<"following student enroll in C++ "<<endl;
		               		myList->print_List(newChoice);
		               		break;
		               	case 7:
		               		cout<<"following student enroll in VBA "<<endl;
		               		myList->print_List(newChoice);
		               		break;
		               	case 8:
		               		cout<<"following student enroll in Math A Course "<<endl;
		               		myList->print_List(newChoice);
		               		break;
		               	case 9:
		               		cout<<"Close program";
							   check_1=false;
							   exit(0);
							   break;
					   default :
							   	check_1=false;
							   	exit(0);
							   
					
					   }
	}while(check_1);
	
}

Leave a Reply

Your email address will not be published. Required fields are marked *