How to Write a C++ program to create and save students information into linked list.

Vu Old Solved Programming Based Assignments Codes are Available in this Section.
Post Reply
User avatar
ALI RAZA
★★★★★★★★★★★★★
★★★★★★★★★★★★★
Posts: 177
Joined: Fri Dec 22, 2017 8:10 am
Location: Lahore
Has thanked: 6 times
Been thanked: 7 times
Contact:
Status: Offline

How to Write a C++ program to create and save students information into linked list.

Post by ALI RAZA » Mon Oct 01, 2018 10:43 am

Write a C++ program to create and save students information into linked list.
Image

Your program should fulfill following requirements.
1) Your program will save the information of student into a student object.
2) Then create list of students which will save into a liked list. The structure of Student object and Students list is showing above in the diagram.
3) After making the list of students, give 3 options to user to perform some search operations from the list as given below.
I. Show all students from the list who are enrolled in the course (code entered by user).
II. Show all students from the list who got greater than or equal CGPA mentioned by user. Suppose user entered 3.4 then all students who got 3.4 or more CGPA should show on the screen.
III. Show all students from the list who got greater than or equal to Marks mentioned by user. Suppose user entered 65 then all students who got 65 or more Marks should show on the screen.
4) User should be able to repeat different type of searches without closing the program. You can see this functionality in Sample Output Demo video attached with assignment file.
5) For demonstration of required solution, watch attached “Sample Demo” video file.

ANSWER:

Code: Select all

#include <iostream>
#include <stdlib.h>
using namespace std;

/* The Student class */
class   Student 
{
    private:
       string name, course;       
       int  marks, counter;
	   float cgpa;       
       Student   * nextStudent;
       
    public:
        // constructor of Student class to initialize data member of class   
        Student(){
            counter = 0;
             name = "";
            course = "";
            marks = 0;
			cgpa = 0.0;           
            nextStudent = NULL;
        } 
          
        // destructor of Student class. place to release allocated memory
        ~Student(){
        
        }
            
        // Student class method to set name of Student
        void setName(string val){
             name = val;
        };
        
        //Student class method to get name of Student
        string getName(){
             return name;
        }; 
        
        // Student class method to set course code
        void setCourse(string val){
             course = val;
        };
        
        //Student class method to get course code
        string getCourse(){
             return course;
        }; 
        
        //Student class method to set the CGPA of Student
        void setCGPA(float val) { 
             cgpa = val; 
        };
		
		// Student class method to get the CGPA of Student
        float getCGPA() { 
             return   cgpa; 
        };
        
        
        //Student class method to set the Marks of Student
        void setMarks(int val) { 
             marks = val; 
        };
		// Student class method to get the Marks of Student
        int getMarks() { 
             return   marks; 
        };
                
         //Student class method to point current Student to next Student
         void setNext(Student *nextStudent) { 
             this->nextStudent   =   nextStudent; 
         };
         
         // Student class method to get memory address where pointer is pointing
         Student *getNext() { 
             return   nextStudent; 
         };        
};


/* The List class */
class   List {
    
    private:
        int      size;
        Student *   headStudent;
        Student *   currentStudent;
        Student *   lastCurrentStudent;
    
    public:
        // constructor of list class to initialize data members of class
        List() {
            size   =   0;
            headStudent   =   new Student();
            headStudent->setNext(NULL);
            currentStudent   =   NULL;
            lastCurrentStudent  =   NULL;            
        }
        
        // destructor of list class. place to release allocated memory
        ~List(){
        
        }        
        
        // list class method to add Students into list
        void   add () {
            Student *newStudent = new Student();
            int count = 0, loc_marks = 0;
            string loc_name = "", loc_course = "";
			float loc_cgpa = 0.0;
            
            cout<<"\nEnter the name of Student: ";
            cin>>loc_name;
            newStudent->setName(loc_name);
            
            cout<<"Enter the course code: ";
            cin>>loc_course;
            newStudent->setCourse(loc_course);           
            
            cout<<"Enter the Marks of course: ";
            cin>>loc_marks;
            newStudent->setMarks(loc_marks);  
			
			 cout<<"Enter the CGPA of Student: ";
            cin>>loc_cgpa;
            newStudent->setCGPA(loc_cgpa);      
                 
           
            if( currentStudent != NULL) {
               newStudent->setNext(currentStudent->getNext());
               currentStudent->setNext( newStudent );
               lastCurrentStudent   =   currentStudent;
               currentStudent   =   newStudent;
            } else {
               newStudent->setNext(NULL);
               headStudent->setNext(newStudent);
               lastCurrentStudent   =   headStudent;
               currentStudent   =   newStudent;
            }
            size ++;
        };        

        // list class method to get the information of Student
        void   getInfo() { 
            if (currentStudent  !=  NULL)             
	             cout<<currentStudent->getName()<<"\t";
				 cout<<currentStudent->getCourse()<<"\t";
				 cout<<currentStudent->getMarks()<<"\t";
				 cout<<currentStudent->getCGPA()<<"\t"; 
        };  
		
		// list class methods to search students
        float   searchStudentByCGPA() { 
            if (currentStudent  !=  NULL)             
	             return currentStudent->getCGPA();
        };   

        string   searchStudentByCourseCode() { 
            if (currentStudent  !=  NULL)             
	             return currentStudent->getCourse();
        };    

        int   searchStudentByMarks() { 
            if (currentStudent  !=  NULL)             
	             return currentStudent->getMarks();
        };   
               
        // list class method to move lastcurrentStudent and currentStudent to next Student
        bool   next() {
            if (currentStudent  ==  NULL){
                return  false;
            }
            
            lastCurrentStudent  =  currentStudent;
            currentStudent  =  currentStudent->getNext();
            
            if (currentStudent == NULL || size == 0) {
                return  false;
            } else {
                return  true;
            }
        };
        
        
        friend void searchStudent(List list){
            Student* savedCurrentStudent  =  list.currentStudent;
            list.currentStudent  =  list.headStudent;
            
            int choice = 0, found = 0;
            
			repeat:
            
			cout<<"\n\nWhat type of search you want to perform? Enter only 1, 2 or 3 for choice."; 
            cout<<"\n1. Search all students by course code\n";
            cout<<"2. Search all students by marks greater than equal to\n";
            cout<<"3. Search all students by CGPA greather than equal to\n";
            cout<<"Choice: ";
            cin>>choice;
            
            if(choice == 1){
            	string code = "";
       
            		
            	cout<<"Enter the course code: ";
            	cin>>code;            	
            	
            	for(int i = 1; list.next(); i++){  
            
					if(list.searchStudentByCourseCode() == code){
						cout << "\n Student " << ++found << ":   ";
						list.getInfo();                
					}   
				                
            	}
            	if (found == 0){
					cout<<"Did not find any student";
				} 
			}
			
			else if(choice == 2){
            	int marks = 0;
            		
            	cout<<"Enter the lower limit of Marks for search: ";
            	cin>>marks;            	
            	
            	for(int i = 1; list.next(); i++){  
            
					if(list.searchStudentByMarks() >= marks){
						cout << "\n Student " << ++found << ":   ";
						list.getInfo();                
					}   
				                
            	}
            	if (found == 0){
					cout<<"Did not find any student";
				} 
			}
			
			else if(choice == 3){
            	float cgpa = 0.0;         	
            		
            	cout<<"Enter the lower limit of CGPA for search: ";
            	cin>>cgpa;            	
            	
            	for(int i = 1; list.next(); i++){  
            
					if(list.searchStudentByCGPA() >= cgpa){
						cout << "\n Student " << ++found << ":   ";
						list.getInfo();                
					}   
				                
            	}
            	if (found == 0){
					cout<<"Did not find any student";
				} 
			}
					
			else{
				cout<<"Invalid choice. Please enter only 1, 2 or 3 for choice.";
				goto repeat;
			}
            
         
            
            
            list.currentStudent  =  savedCurrentStudent;
        };

        friend List addStudents();    

};

// friend method of list class used to add students into list 
// this method have access to all public / private data members and methods of list class 
List addStudents(){
	List   list;
	int val = 0, numStudents = 0;
	
	cout<<"How many number of students you want to enter into list? ";
    cin>>numStudents;
            
    for(int i=0; i<numStudents; i++){
    	list.add();
        cout<<"Student's information saved successfully.\n";
    }

    cout  <<  "\n List size = "  <<  list.size  <<'\n';         
    return  list;
};
    
main()
{
    List lst;
	lst  =  addStudents();

    char more = '\0';
    do{
    	searchStudent(lst);    	
    	cout<<"\n\nWant to perform another search? Enter y or n: ";
    	cin>>more;
	}while(more == 'y' || more == 'Y');   
    
    system("pause");
}



Post Reply

Last 10 Members Who Visited This Topic

User avatar bc170406547 (1), User avatar ALI RAZA (4), User avatar Princes_Noor (1)

  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 1 guest