CS301P Assignment 01 fall 2021 (Data Structure)

Problem Statement

A company wants to hire new employees in its recruitment drive. You are hired as a programmer to develop a C++ program using Linked List data structure for the recruitment process. Your program should meet the following requirements.

Requirements:

  • Program should be built with the help of Linked list. (If array is used then marks will be deducted).
  • Output console menu must be appeared to do following operations:
    • The program will store the data of all candidates in Linked List.
    • The Candidate id, name, interview marks will be entered as input on the console.
    • If interview marks are greater than or equal to 50 then set the candidate status as pass else declare the candidate status as fail.
    • Save each candidate’s record into Linked List.
    • The program should be able to print total number of candidates.
    • The program should display the data of pass and fail candidates separately.
    • The program should be able to update the data of a candidate. Candidate ID will be passed as an input and data against that ID should be updated.

Solution Guidelines:

  1. Before developing this assignment understand and practice the following topics:
  2. Nodes
  3. List
  4. Linked List
  5. Linked List Methods
  6.  
  7. Use Linked List data structure to save information of candidates. Each candidate will represent a node which will contain candidate id, name, interview marks and status(pass/fail).
  • Your solution should use these classes
  1. Candidate (Node) Class: To save information of each candidate
    1. Linked_List Class: To save different candidates

Sample Output

See the attached Gif file (output.gif) to see the sample output.

Solution of CS301P Assignment 02 fall 2021

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

class Candidate
{
    private:
       string  name;
       int  marks,id;
	   string status;
	   Candidate* nextCandidate;        
    public:   
        Candidate(){
            marks      = 0;            
            name       = "";
            status     ="";
            nextCandidate= NULL;
        } 
        void setName(string s){
             name = s;
        };
        string getName(){
             return name;
        }; 
        
        int getmarks() { 
             return   marks; 
        };
        
        void setmarks(int s) { 
             marks = s; 
        };
        
        void setStatus(string s){
             status = s;
        };
        
        string getStatus(){
             return status;
        }; 
        
        int getId() { 
             return id; 
        };
        
        void setId(int i) { 
             id = i; 
        };

        void setNext(Candidate*nextCandidate) { 
             this->nextCandidate  =   nextCandidate; 
         };
         
         Candidate* getNext() { 
             return   nextCandidate; 
         };  	       
};


class Linked_List 
{
private:
		Candidate * head;
        Candidate * current;
		    	    
    public:
		Linked_List() {            
            head   =   new Candidate();
            head->setNext(NULL);
            current   =   NULL;          
        } 		
		
		void add() {
		    Candidate *newCandidate = new Candidate();
            int marks=0,id=0;
            string name = "";
            
            cout<<"\nEnter the ID of Candidate: ";
			cin>>id;
			
            cout<<"\nEnter the name of Candidate: ";
			cin>>name;
			
			cout<<"\nEnter the marks of Candidate: ";
			cin>>marks;
			
			newCandidate->setId(id);
			newCandidate->setName(name);
			newCandidate->setmarks(marks);
			
			if(marks>=50){
				newCandidate->setStatus("pass");
			}
			else{
				newCandidate->setStatus("fail");
			}
        
            if(head->getNext() == NULL){
           		newCandidate->setNext(NULL);
               	head->setNext(newCandidate);               	
               	current = newCandidate;
		    }
		    
		    else{
		    	Candidate *temp = head;
		    	while(temp->getNext() != NULL){
		    		temp = temp->getNext();
				}
				current = temp;
				newCandidate->setNext(current->getNext());
               	current->setNext( newCandidate );               	
               	current = newCandidate;
		   }         
        }
		
		void getInfo(string type) { 
		if (current  !=  NULL && current->getStatus() == type){
            	cout<<"Candidate ID: "<<current->getId()<<endl;
				cout<<"Candidate Name: "<<current->getName()<<endl;
	            cout<<"Interview Marks: "<<current->getmarks()<<endl;				 
				cout<<"Candidate Status: "<<current->getStatus()<<endl<<endl;				
			}
        }
		
        bool next() {
            if (current  ==  NULL){
                return  false;
            }  
            current  =  current->getNext();            
        }	
        
        void updateCandidate(Linked_List list,int id){
        Candidate *temp =list.head;
        while(temp->getNext() != NULL){
        	temp=temp->getNext();
        	if(temp->getId() == id){
        		string name,status;
        		int marks;
				cout<<"\nEnter the name of Candidate: ";
				cin>>name;
				cout<<"\nEnter the marks of Candidate: ";
				cin>>marks;
				temp->setName(name);
				temp->setmarks(marks);
				if(marks>=50){
				temp->setStatus("pass");
				}
				else{
				temp->setStatus("fail");
				}
				cout<<"Record Updated"<<endl;
				break;
				}
				}
		}
		 
       friend void showCandidatesInfo(Linked_List list,string resultType){
            Candidate* currentCandidate  =  list.current;
            list.current  =  list.head;
            for(int i = 1; list.next(); i++){			
				list.getInfo(resultType);          
			}
            list.current  =  currentCandidate;
        }        
};	

 int main() {
 	Linked_List list;
	int noCandidates = 0, marks = 0;
	int n=0,id;
        while (n<6) {
		        cout << "\n Enter Your Choice \n";
		        cout << "1: To add candidate data \n";
		        cout << "2: Total number of candidates \n";
		        cout << "3: Display data of passed candidates \n";
                cout << "4: Display of failed candidates \n";
                cout << "5: Update Candidate Data\n";
				cout << "Press other number to close the program\n";
				cin>>n;
				
				switch (n) {
					
				case 1:
				   cout<<"Enter number of candidates you want to add or see result ";
				   cin>>noCandidates;
				   for(int i = 0; i < noCandidates ; i++){
				          list.add();
				         // n++;
					}
				 break;
				 case 2:
				 cout<<"Total number of candidates are "<<noCandidates<<endl;
		         break;
				 
				 case 3:
				 showCandidatesInfo(list,"pass");
				 break;			 
				 
				 case 4:
				 showCandidatesInfo(list,"fail");
				 break;
					 
					 
			     case 5:
				 cout<<"Enter Candidate ID:"<<endl;
				 cin>>id;
				 list.updateCandidate(list,id);
				 break;				 
				 
				 default :
				 break;
				}
				} 
    return 0;
}
0 0 votes
Article Rating
0
Would love your thoughts, please comment.x
()
x