In: Computer Science
implementing linked list using c++
I Have Atteched screenshots of output of program
#include<bits/stdc++.h>
using namespace std;
struct Employee
{
int employeeID;
string name;
string designation;
string department;
Employee *next;
Employee(int EmployeeID,string Name,string Designation,string Department){
employeeID=EmployeeID;
name=Name;
designation=Designation;
department=Department;
next=NULL;
}
};
//It will return the pointer of new employee
Employee *AddEmployee(unordered_map<string,int>&department_count,unordered_map<int,bool>&cheking_id){
string name,designation,department;
int employeeID;
bool br=true;
while(br){
cout<<"Enter EmploeeID: ";
cin>>employeeID;
if(cheking_id[employeeID]==true)
{
cout<<"Employee Id is Already Prsent,Please enter Other Id\n";
}else{
cheking_id[employeeID]=true;
br=false;
}
}
cout<<"Enter Name: ";
cin>>name;
cout<<"Enter Department: ";
cin>>department;
cout<<"Enter designation : ";
cin>>designation;
Employee *nw=new Employee(employeeID,name,designation,department);
//It is used to count department type
department_count[department]++;
return nw;
}
//deleting the emplyee
Employee* deleteEmployee(Employee *first, unordered_map<string,int>&department_count,unordered_map<int,bool>&cheking_id){
int ID;
cout<<"Enter Employee Id:";
cin>>ID;
Employee *temp=first,*prev=NULL;
//maintaining prev so we could get parent pointer of deleting Node
while(temp!=NULL&&temp->employeeID!=ID){
prev=temp;
temp=temp->next;
}
//if temp is NULL that means that employeeId is not present
if(temp!=NULL){
//we are deleting the employee so we need to delete
//employee department we counted and employeeid
department_count[temp->department]--;
cheking_id[temp->employeeID]=false;
//Handling case where deleting node is first node
if(temp!=first){
prev->next=temp->next;
cout<<"Successfully deleted record of Employee "<<temp->name<<endl;
}else{
first=first->next;
}
delete temp;
}else
{
cout<<"Entered ID Employee is not listed in Company\n";
}
return first;
}
void displayAll(Employee *first){
Employee *temp=first;
if(first==NULL)
{
cout<<"We Don't have any emploee details\n";
return;
}
while(temp!=NULL){
cout<<"Employee ID: "<<temp->employeeID<<endl;
cout<<"Employee name: "<<temp->name<<endl;
cout<<"Employee Designation: "<<temp->designation<<endl;
cout<<"Emplyee department : "<<temp->department<<endl;
temp=temp->next;
cout<<endl;
}
}
void displayEmployeeDetails(Employee *first){
Employee *temp=first;
if(first==NULL)
{
cout<<"We Don't have any emploee details\n";
return;
}
bool found=false;
int id;
cout<<"Enter EmployeeID:";
cin>>id;
//traversing linked list to get add details of employess
while(temp!=NULL){
if(id==temp->employeeID)
{
cout<<"===Employee Details===\n";
cout<<"Employee ID: "<<temp->employeeID<<endl;
cout<<"Employee name: "<<temp->name<<endl;
cout<<"Employee Designation: "<<temp->designation<<endl;
cout<<"Emplyee department : "<<temp->department<<endl;
cout<<endl;
found=true;
break;
}
temp=temp->next;
}
if(!found)
cout<<"Entered EmployeeID Employee is not found\n";
}
void displayDepartmentCount(unordered_map<string,int>&department_count){
if(department_count.size()==0)
{
cout<<"NO Emploee Details Present\n";
return;
}
//print the department and its count
for(auto department:department_count){
cout<<department.first<<":"<<department.second<<endl;
}
}
int main(){
// freopen("input.txt","r",stdin);
int choice=-1;
unordered_map<string,int>department_count;
unordered_map<int,bool>cheking_id;
Employee *first=NULL,*last=NULL;
while(1){
cout<<"\n1.Add Employee\n2.Delete Emploee\n3.Display All Emploee Details\n4.Display Perticular Employee Detail\n5.Display count of Each department\n6.Exit\nEnter choice:\n";
cin>>choice;
switch (choice)
{
case 1:{
/* code */
Employee *nw=AddEmployee(department_count,cheking_id);
if(first==NULL)
first=last=nw;
else
{
last->next=nw;
last=nw;
}
break;
}
case 2:
first=deleteEmployee(first,department_count,cheking_id);
break;
case 3:
displayAll(first);
break;
case 4:
displayEmployeeDetails(first);
break;
case 5:
displayDepartmentCount(department_count);
break;
case 6:
return 0;
break;
default:
cout<<"Enter Valid choice ";
break;
}
}
}