Open Kanika0503 opened 3 months ago
using namespace std;
struct node{ int emp_id; string name; int age; char grade; int salary; struct node next; }start=NULL,nn,temp,*loc;
void Insert(int id, string n, int a, char g,int s) { nn = new node; nn->emp_id = id; nn->name = n; nn->age = a; nn->grade = g; nn->salary = s;
if (start == NULL || id == 1) {
nn->next = start;
start = nn;
} else {
temp = start;
int count = 1;
while (temp->next != NULL && count < id - 1) {
temp = temp->next;
count++;
}
nn->next = temp->next;
temp->next = nn;
}
}
void Delete(int id) { if(start==NULL) { cout<<"No Records in the List to Delete"<<endl;
}
else
{
temp=start;
if(id==1)
{
start=temp->next;
delete temp;
cout<<"Record at "<<id<<" is deleted"<<endl;
return;
}
temp=start;
int count=1;
bool found=false;
while(temp->next!=NULL && count < id-1)
{
temp=temp->next;
count++;
}
if(count <id-1 || temp->next==NULL)
{
cout<<"Invalid Position"<<endl;
return;
}
loc=temp->next;
temp->next=loc->next;
delete loc;
found = true;
cout<<"Record at "<<id<<" is deleted"<<endl;
if(!found)
{
cout<<"No Record found at "<<id<<"to delete"<<endl;
}
}
}
void traverse(){ if(start==NULL) { cout<<"No Records are Present Currently"<<endl; }
else
{
temp=start;
int count=1;
while(temp!=NULL)
{
cout<<count<<"."<<temp->emp_id<<"\t"<<" "<<temp->name<<"\t"<<" "<<temp->age<<"\t"<<" "<<temp->grade<<"\t"<<" "<<temp->salary<<endl;
temp=temp->next;
count++;
}
}
} void display() { if(start==NULL) { cout<<"Records are Empty"<<endl; } else{ temp=start; int maxsalary=temp->salary; string empwithmaxsalary=temp->name;
while(temp!=NULL)
{
if(temp->salary>maxsalary)
{
maxsalary=temp->salary;
empwithmaxsalary=temp->name;
}
temp=temp->next;
}
cout<<"Employee With Highest Salary is: "<<empwithmaxsalary<<" with "<<maxsalary<<"\n"<<endl;
}
}
int main() { int id,ch,salary,age; string name; char grade,choice; do{ cout<<"WELCOME TO THE EMPLOYEE DATABASE"<<"\n"<<endl; cout<<"1.Press 1 to Insert a New Record"<<endl; cout<<"2.Press 2 to Delete an Existing Record"<<endl; cout<<"3.Press 3 to Display Employee with Highest Salary"<<endl; cout<<"4.Press 4 to Display all Records"<<endl; cin>>ch; switch(ch) { case 1: traverse(); cout<<"Enter the ID of the Employee be Inserted"<<endl; cin>>id; cout<<"Enter the Name of the Employee"<<endl; cin>>name; cout<<"Enter the Age of the Employee"<<endl; cin>>age; cout<<"Enter the Grade of the Employee"<<endl; cin>>grade; cout<<"Enter the Salary of the Employee"<<endl; cin>>salary; Insert(id,name,age,grade,salary); cout<<"The current Employee Records are: "<<endl; traverse(); break;
case 2: traverse(); cout<<"Enter the ID of the Employee be Deleted"<<endl; cin>>id; Delete(id); cout<<"The current Employee Records are: "<<endl; traverse(); break;
case 3: cout<<"The Employee with the Highest Salary is: "<<endl; display();
case 4: cout<<"The Records in the Employee Database are: "<<endl; traverse(); break;
default: cout<<"Invalid choice. Please Enter a Valid option."<<endl; break; } cout<<"Do you want to Insert or Delete more records?"<<endl; cout<<"Press (Y/N)"<<endl; cin>>choice; } while(choice=='Y' || choice=='y');
return 0;
}
You are tasked with creating a program to manage an employee database for an organization. Each employee record consists of the following information: Employee ID Name Age Grade Salary Next pointer (to the next employee record) Requirements: The program should allow the user to: Add a new employee record, Delete a record, Display the highest paid employee The program should use a linked list data structure to store and manage employee records.