0% found this document useful (0 votes)
4 views

Practical 12 (DSA)

The document outlines a lab assignment for managing employee information, including adding, deleting, and displaying employee details using a linked list structure. It provides a C++ code implementation for a Company class that handles employee operations and a menu-driven interface for user interaction. The code includes functionalities for searching, editing, and displaying employee data, with appropriate messages for non-existent employees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Practical 12 (DSA)

The document outlines a lab assignment for managing employee information, including adding, deleting, and displaying employee details using a linked list structure. It provides a C++ code implementation for a Company class that handles employee operations and a menu-driven interface for user interaction. The code includes functionalities for searching, editing, and displaying employee data, with appropriate messages for non-existent employees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Assignment No.

12

Problem Statement: Company maintains employee information as employee ID, name,


designation and salary. Allow user to add, delete information of employee. Display
information of particular employee. If employee does not exist an appropriate message
is displayed. If it is, then the system displays the employee details. Use index sequential
file to maintain the data.

Code :
#include <iostream>
#include <cstring> // For strcpy class Node {
using namespace std; Emp data;
Node *next;
class Emp {
int eid; public:
string name; Node(Emp d) : data(d), next(nullptr) {}
float basic;
// Getter Methods
public: Emp getdata() { return data; }
// Default Constructor Node *getnext() { return next; }
Emp() {
eid = 0; // Setter Methods
name = "Not Given"; void setdata(Emp d) { data = d; }
basic = 0.0; void setnext(Node *n) { next = n; }
} }; // End of Node Class

// Parameterized Constructor class Company {


Emp(int d, const string &nm, float bs) { Node *start;
eid = d;
name = nm; public:
basic = bs; Company() { start = nullptr; }
}
// Add Employee
// Display Employee Details void addemp(Emp e) {
void display() { Node *temp = new Node(e);
cout << temp->setnext(start);
"\n*********************************** start = temp;
*"; }
cout << "\nEmployee Id : " << eid;
cout << "\nName : " << name; // Remove Employee by ID
cout << "\nSalary : " << basic; void removeempById(int id) {
cout << if (start == nullptr) {
"\n*********************************** cout << "\nNo employee data is here.";
*"; return;
} }

// Getter Methods Node *p = start;


int getid() { return eid; } if (id == p->getdata().getid()) {
string getname() { return name; } start = start->getnext();
float getbasic() { return basic; } p->getdata().display();
cout << "\nThis employee is
// Setter Methods deleted...";
void setid(int d) { this->eid = d; } delete p;
void setename(const string &nm) { this- return;
>name = nm; } }
void setbasic(float bs) { this->basic = bs; }
}; // End of Emp Class Node *prev = nullptr;
while (p != nullptr && p- void searchEmpById(int id) {
>getdata().getid() != id) { if (start == nullptr) {
prev = p; cout << "\nNot Found";
p = p->getnext(); return;
} }

if (p == nullptr) { Node *p = start;


cout << "\nEmployee not found."; while (p != nullptr) {
return; if (p->getdata().getid() == id) {
} cout << "\nEmployee Found ...";
p->getdata().display();
prev->setnext(p->getnext()); return;
p->getdata().display(); }
cout << "\nNow deleted this employee."; p = p->getnext();
delete p; }
} cout << "\nEmployee is not found.";
}
// Edit Employee by ID
void editEmp(int id) { // Display All Employees
if (start == nullptr) { void DisplayallEmp() {
cout << "\nNo employee is here."; if (start == nullptr) {
return; cout << "\nNo employee data here.\n";
} return;
}
Node *p = start;
while (p != nullptr) { Node *p = start;
if (p->getdata().getid() == id) { while (p != nullptr) {
Emp e = p->getdata(); p->getdata().display();
char ans; p = p->getnext();
string name; }
float sal; }
}; // End of Company Class
cout << "\nDo you want to change
name? (Y/N): "; int main() {
cin >> ans; int ch = 0;
if (ans == 'Y' || ans == 'y') { Company lt;
cout << "\nEnter New Name: ";
cin.ignore(); while (ch != 6) {
getline(cin, name); cout <<
e.setename(name); "\n\n*********************************
} ****";
cout << "\n\t1. Add Employee";
cout << "\nDo you want to change cout << "\n\t2. Display Employees";
Salary? (Y/N): "; cout << "\n\t3. Search By ID";
cin >> ans; cout << "\n\t4. Delete By ID";
if (ans == 'Y' || ans == 'y') { cout << "\n\t5. Edit Employee Data";
cout << "\nEnter New Salary: "; cout << "\n\t6. Exit";
cin >> sal; cout <<
e.setbasic(sal); "\n***********************************
} **\n";
cout << "\nEnter your choice: ";
p->setdata(e); cin >> ch;
return;
} switch (ch) {
p = p->getnext(); case 1: {
} int id;
cout << "\nRecord not found."; string name;
} float bs;

// Search Employee by ID cout << "\nEnter Employee ID: ";


cin >> id; cout << "\nEnter Employee ID to
cout << "\nEnter Employee Name: "; Delete: ";
cin.ignore(); cin >> id;
getline(cin, name); lt.removeempById(id);
cout << "\nEnter Employee Salary: break;
"; }
cin >> bs;
case 5: {
Emp e1(id, name, bs); int id;
lt.addemp(e1); cout << "\nEnter Employee ID for
break; Edit: ";
} cin >> id;
lt.editEmp(id);
case 2: break;
lt.DisplayallEmp(); }
break;
case 6:
case 3: { cout << "\nExiting the program!";
int id; break;
cout << "\nEnter Employee ID to
Search: "; default:
cin >> id; cout << "\nInvalid choice. Try
lt.searchEmpById(id); again.";
break; break;
} }
}
case 4: { return 0;
int id; }
Output:

*************************************
1. Add Employee
2. Display Employees
3. Search By ID
4. Delete By ID
5. Edit Employee Data
6. Exit
*************************************

Enter your choice: 1

Enter Employee ID: 23411

Enter Employee Name: C1

Enter Employee Salary: 20

*************************************
1. Add Employee
2. Display Employees
3. Search By ID
4. Delete By ID
5. Edit Employee Data
6. Exit
*************************************

Enter your choice: 2

************************************
Employee Id : 23411
Name : C1
Salary : 20

You might also like