In: Computer Science
Create a class of DSA with two pointer objects of Employee and Customer. These objects will represent the head pointer of corresponding linkedlists.
Add new data member functions searchCustomer & searchEmployee, to search for customers and employees separately in DSA.
There is a small confusion if your question, you had not specified in which language you need the code, so I had done in cpp, comment below, if you required in another languale, I will modify the answer based on that.
Code :
#include <iostream>
#include <string>
using namespace std;
class Employee
{
public:
string name;
int id;
Employee *next;
Employee(const string &newName, int a)
{
name = newName;
id = a;
next = 0;
}
void print() const
{
cout << "Name: " << name << ", id: " << id << endl;
}
~Employee()
{
cout << "Destructor of Employee" << endl;
}
};
Employee *head = NULL;
class Customer
{
public:
string name;
int id;
Customer *next;
Customer(const string &newName, int a)
{
name = newName;
id = a;
next = 0;
}
void print() const
{
cout << "Customer Name: " << name << ", Customer id: " << id << endl;
}
~Customer()
{
cout << "Destructor of Customer" << endl;
}
};
Customer *headOfCustomer = NULL;
bool InsertEmployee(const string &n, int a)
{
Employee *previous, *current, *newEmployee;
newEmployee = new Employee(n, a);
if (!newEmployee)
return false;
if (head)
{
previous = head;
current = head->next;
while (current)
{
previous = current;
current = current->next;
}
previous->next = newEmployee;
}
else
{
head = newEmployee;
}
return true;
}
bool InsertCustomer(const string &n, int a)
{
Customer *previous, *current, *newCustomer;
newCustomer = new Customer(n, a);
if (!newCustomer)
return false;
if (headOfCustomer)
{
previous = headOfCustomer;
current = headOfCustomer->next;
while (current)
{
previous = current;
current = current->next;
}
previous->next = newCustomer;
}
else
{
headOfCustomer = newCustomer;
}
return true;
}
void printEmployee()
{
Employee *tempPtr;
if (head)
{
tempPtr = head;
while (tempPtr)
{
tempPtr->print();
tempPtr = tempPtr->next;
}
}
else
{
cout << "The list is empty" << endl;
}
}
void printCustomer()
{
Customer *tempPtr;
if (headOfCustomer)
{
tempPtr = headOfCustomer;
while (tempPtr)
{
tempPtr->print();
tempPtr = tempPtr->next;
}
}
else
{
cout << "The list is empty" << endl;
}
}
void searchEmployee(int id)
{
Employee *tempPtr;
if (head)
{
tempPtr = head;
while (tempPtr)
{
if(tempPtr->id == id)
{
cout<<"The employee found";
return;
}
tempPtr = tempPtr->next;
}
cout<<"Employee not found in the linked list";
}
else
{
cout << "The list is empty" << endl;
}
}
void searchCustomer(int id)
{
Customer *tempPtr;
if (headOfCustomer)
{
tempPtr = headOfCustomer;
while (tempPtr)
{
if(tempPtr->id == id)
{
cout<<"The Customer found";
return;
}
tempPtr = tempPtr->next;
}
cout<<"Customer not found in the linked list";
}
else
{
cout << "The list is empty" << endl;
}
}
int main()
{
int option = 1;
string name;
int id;
// InsertEmployee("Employee 1", 100);
// InsertEmployee("Employee 2", 200);
// InsertEmployee("Employee 3", 300);
// printEmployee();
while(option != 0 )
{
cout<<"\n0. Exit";
cout<<"\n1. Add Employee";
cout<<"\n2. Seach Employee";
cout<<"\n3. Display all Employee";
cout<<"\n4. Add Customer";
cout<<"\n5. Search Customer";
cout<<"\n6. Display all Customer";
cout<<"\nEnter your option : ";
cin>>option;
switch(option)
{
case 1: // Add employee
cout<<"Enter employee name : ";
cin>>name;
cout<<"Enter employee ID : ";
cin>>id;
if(InsertEmployee(name,id))
{
cout<<"Successfully inserted";
}
break;
case 2: // Search employee
cout<<"Enter employee ID to search : ";
cin>>id;
searchEmployee(id);
break;
case 3: // Display all employee
printEmployee();
break;
case 4: // Add customer
cout<<"Enter customer name : ";
cin>>name;
cout<<"Enter customer ID : ";
cin>>id;
if(InsertCustomer(name,id))
{
cout<<"Successfully inserted";
}
break;
case 5: // Search customer
cout<<"Enter customer ID to search : ";
cin>>id;
searchCustomer(id);
break;
case 6: // Display all customer
printCustomer();
break;
}
}
return 0;
}
Screenshot of a part of output :