0% found this document useful (0 votes)
30 views11 pages

Mehran Dsa Assign 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views11 pages

Mehran Dsa Assign 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Name : Muhammad Mehran Amjad

Class: BScs-3B

Roll NO: su92-bscsm-s23-103

Assignment no 2
#include <iostream>

#include <string>

using namespace std;

class Employee {

public:

int id;

string name;

string dob;

string phone;

string address;

Employee(int empId, const string& empName, const string& empDob, const string& empPhone, const string& empAddress)

: id(empId), name(empName), dob(empDob), phone(empPhone), address(empAddress) {}

};

class Node {

public:

Employee data;

Node* next;

Node(Employee emp) : data(emp), next(nullptr) {}

};

class EmpLinkedList {

private:

Node* head;
public:

EmpLinkedList() : head(nullptr) {}

bool IsEmpty() {

return head == nullptr;

Node* Front() {

return head;

Node* Back() {

if (IsEmpty()) {

return nullptr;

Node* current = head;

while (current->next != nullptr) {

current = current->next;

return current;

void Print_List() {

Node* current = head;

while (current != nullptr) {

cout << "ID: " << current->data.id << ", Name: " << current->data.name << ", DOB: " << current->data.dob

<< ", Phone: " << current->data.phone << ", Address: " << current->data.address << endl;

current = current->next;

}
void AddFront(Employee emp) {

Node* newNode = new Node(emp);

newNode->next = head;

head = newNode;

void AddBack(Employee emp) {

Node* newNode = new Node(emp);

if (IsEmpty()) {

head = newNode;

return;

Node* lastNode = Back();

lastNode->next = newNode;

void DeleteFront() {

if (!IsEmpty()) {

Node* temp = head;

head = head->next;

delete temp;

void DeleteBack() {

if (!IsEmpty()) {

if (head->next == nullptr) {

delete head;

head = nullptr;

return;

}
Node* current = head;

while (current->next->next != nullptr) {

current = current->next;

delete current->next;

current->next = nullptr;

Node* Find(int empId) {

Node* current = head;

while (current != nullptr) {

if (current->data.id == empId) {

return current;

current = current->next;

return nullptr;

void Modify(int empId, const string& newName, const string& newDob, const string& newPhone, const string& newAddress)
{

Node* empNode = Find(empId);

if (empNode != nullptr) {

empNode->data.name = newName;

empNode->data.dob = newDob;

empNode->data.phone = newPhone;

empNode->data.address = newAddress;

}
void Add(Employee emp) {

Node* newNode = new Node(emp);

if (IsEmpty() || head->data.id > emp.id) {

newNode->next = head;

head = newNode;

return;

Node* current = head;

while (current->next != nullptr && current->next->data.id < emp.id) {

current = current->next;

newNode->next = current->next;

current->next = newNode;

void Delete(int empId) {

if (IsEmpty()) {

return;

if (head->data.id == empId) {

Node* temp = head;

head = head->next;

delete temp;

return;

Node* current = head;

while (current->next != nullptr && current->next->data.id != empId) {


current = current->next;

if (current->next == nullptr) {

return;

Node* temp = current->next;

current->next = current->next->next;

delete temp;

int Count() {

int count = 0;

Node* current = head;

while (current != nullptr) {

count++;

current = current->next;

return count;

};

void displayMenu(EmpLinkedList& empList) {

int choice;

do {

cout << "\n====== Employee Management System ======\n";

cout << "1. Display Front Employee's Info\n";

cout << "2. Display Back Employee's Info\n";

cout << "3. Print Employee's List\n";

cout << "4. Insert an Employee at Front\n";


cout << "5. Insert an Employee at Back\n";

cout << "6. Delete Employee at the Front\n";

cout << "7. Delete Employee at the Back\n";

cout << "8. Search for an Employee (based on Employee's ID)\n";

cout << "9. Modify an Employee data (Name, Date of Birth, Phone, address)\n";

cout << "10. Add a New Employee (order of ID)\n";

cout << "11. Delete an Employee\n";

cout << "12. Count Employees\n";

cout << "0. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1: {

Node* frontNode = empList.Front();

if (frontNode != nullptr) {

cout << "Front Employee's Info: ID=" << frontNode->data.id << ", Name=" << frontNode->data.name << endl;

} else {

cout << "Employee list is empty.\n";

break;

case 2: {

Node* backNode = empList.Back();

if (backNode != nullptr) {

cout << "Back Employee's Info: ID=" << backNode->data.id << ", Name=" << backNode->data.name << endl;

} else {

cout << "Employee list is empty.\n";

break;

case 3:

cout << "Employee's List:\n";


empList.Print_List();

break;

case 4: {

int id;

string name, dob, phone, address;

cout << "Enter Employee ID: ";

cin >> id;

cout << "Enter Employee Name: ";

cin >> name;

cout << "Enter Employee DOB: ";

cin >> dob;

cout << "Enter Employee Phone: ";

cin >> phone;

cout << "Enter Employee Address: ";

cin >> address;

empList.AddFront(Employee(id, name, dob, phone, address));

cout << "Employee added successfully at the front.\n";

break;

case 5: {

int id;

string name, dob, phone, address;

cout << "Enter Employee ID: ";

cin >> id;

cout << "Enter Employee Name: ";

cin >> name;

cout << "Enter Employee DOB: ";

cin >> dob;

cout << "Enter Employee Phone: ";

cin >> phone;

cout << "Enter Employee Address: ";

cin >> address;

empList.AddBack(Employee(id, name, dob, phone, address));


cout << "Employee added successfully at the back.\n";

break;

case 6:

empList.DeleteFront();

cout << "Employee deleted from the front.\n";

break;

case 7:

empList.DeleteBack();

cout << "Employee deleted from the back.\n";

break;

case 8: {

int id;

cout << "Enter Employee ID to search: ";

cin >> id;

Node* empNode = empList.Find(id);

if (empNode != nullptr) {

cout << "Employee found: ID=" << empNode->data.id << ", Name=" << empNode->data.name << endl;

} else {

cout << "Employee not found.\n";

break;

case 9: {

int id;

string name, dob, phone, address;

cout << "Enter Employee ID to modify: ";

cin >> id;

cout << "Enter new Employee Name: ";

cin >> name;

cout << "Enter new Employee DOB: ";

cin >> dob;

cout << "Enter new Employee Phone: ";


cin >> phone;

cout << "Enter new Employee Address: ";

cin >> address;

empList.Modify(id, name, dob, phone, address);

cout << "Employee data modified successfully.\n";

break;

case 10: {

int id;

string name, dob, phone, address;

cout << "Enter Employee ID to add: ";

cin >> id;

cout << "Enter Employee Name: ";

cin >> name;

cout << "Enter Employee DOB: ";

cin >> dob;

cout << "Enter Employee Phone: ";

cin >> phone;

cout << "Enter Employee Address: ";

cin >> address;

empList.Add(Employee(id, name, dob, phone, address));

cout << "Employee added successfully.\n";

break;

case 11: {

int id;

cout << "Enter Employee ID to delete: ";

cin >> id;

empList.Delete(id);

cout << "Employee deleted successfully.\n";

break;

case 12:
cout << "Number of Employees: " << empList.Count() << endl;

break;

case 0:

cout << "Exiting program.\n";

break;

default:

cout << "Invalid choice. Please try again.\n";

} while (choice != 0);

int main() {

EmpLinkedList employeeList;

displayMenu(employeeList);

return 0;

You might also like