In: Computer Science
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID, name and salary as data of each employee. Search a particular record on ID and display the previous and next records as well. Whichever ID it give, it should display all the records because of being circular.
Code needed in Java.
I have implemented Doubly circular linkedlist with the following methods::
1> public void insertFirst(int empId, String empName, long empSalary) : This method add employee at the first position in the doubly circular linkedlist.
2> public void insertLast(int empId, String empName, long empSalary) : This method add employee at the last position in the doubly circular linkedlist.
3> public void displayList() : This method display each employee detail from the linkedlist.
4> public void searchEmployeeById(int empId) : This method search the employee with empId.
5> public void displaySearchedEmployeeDetail(Employee searchedEmployee) : This method display searched employee detail with its previous and next employee.
Here, DoublyCircularLinkedList class contain Employee class which represents as a node in the doubly circular linked list.
Program:-
// This class represents DoublyCircularLinkedList
class DoublyCircularLinkedList{
// point to the first Employee (firs node of linkedlist)
Employee start;
// This class represents Employee as a node of the linkedlist
class Employee{
// store employee id
int empId;
// store employee name;
String empName;
// store employee salary
long empSalary;
// store prev employee detail
Employee prev;
// store next employee detail
Employee next;
// constructor which create a node for he linkedlist
public Employee(int empId, String empName, long empSalary) {
this.empId = empId;
this.empName = empName;
this.empSalary = empSalary;
this.prev = null;
this.next = null;
}
}
// This method add the newEmployee at the first of linkedlist
public void insertFirst(int empId, String empName, long empSalary){
// create employee node for the linkedlist
Employee newEmployee = new Employee(empId, empName, empSalary);
// if the list is empty make newEmployee as a start node
if(start == null){
newEmployee.next = newEmployee;
newEmployee.prev = newEmployee;
start = newEmployee;
return;
}
// first get the last node using start
Employee lastEmployee = start.prev;
// now store start Employee to the next of newEmployee
newEmployee.next = start;
// point newEmployee to the prev of start employee
start.prev = newEmployee;
// point the last node to the prev of newEmployee
newEmployee.prev = lastEmployee;
// point newEmployee to the next of lastEmployee
lastEmployee.next = newEmployee;
// finally make the newEmployee as a start Employee
start = newEmployee;
}
// This method add the newEmployee at the last of linkedlist
public void insertLast(int empId, String empName, long empSalary){
// create employee node for the linkedlist
Employee newEmployee = new Employee(empId, empName, empSalary);
// if the list is empty make newEmployee as a start node
if(start == null){
start = newEmployee;
start.next = start;
start.prev = start;
return;
}
// point to the last node of linkedlist
Employee lastEmployee = start.prev;
// now store newEmployee to the next of lastEmployee
lastEmployee.next = newEmployee;
// now store last employee into the prev of newEmployee
newEmployee.prev = lastEmployee;
// point the start node from the newEmployee
newEmployee.next = start;
// and at the end, point newEmployee to the prev of start
start.prev = newEmployee;
}
// This method display the each employee detail
public void displayList(){
// if list is empty then return from the method
if(start == null){
System.out.println("Doubly Circular LinkedList is EMpty!");
return;
}
System.out.println("\n--------- Employee Details ---------\n");
// store start node
Employee curr = start;
// display each node detail
while(curr.next != start){
// display employee detail
System.out.println("Employee id : "+curr.empId);
System.out.println("Employee Name : "+curr.empName);
System.out.println("Employee Salary : "+curr.empSalary);
System.out.println("\n\t------||------");
System.out.println("\tNext Employee");
System.out.println("\t------||------\n");
// go too the next Employee
curr = curr.next;
}
// now display the last Employee detail
System.out.println("Employee id : "+curr.empId);
System.out.println("Employee Name : "+curr.empName);
System.out.println("Employee Salary : "+curr.empSalary);
}
// This method search Employee in the list by empId
public void searchEmployeeById(int empId){
System.out.println("\n ---- Search Employee with "+empId+" empId ----\n");
// if list is empty then return from the method
if(start == null){
System.out.println("Doubly Circular LinkedList is EMpty!");
return;
}
// store start node
Employee curr = start;
boolean flagFound = false;
// compare empId with each node
while(curr.next != start){
// compare emp Id
if(curr.empId == empId){
flagFound = true;
break;
}
// go too the next Employee
curr = curr.next;
}
// empId found
if(flagFound){
// displa searchedEmployee detail with the prev and next employee detail of searchedEmployee
displaySearchedEmployeeDetail(curr);
}else{
// now compare with last employee
if(curr.empId == empId){
// displa searchedEmployee detail with the prev and next employee detail of searchedEmployee
displaySearchedEmployeeDetail(curr);
}else{
System.out.println("Employee does not exist with "+empId+" empId");
}
}
}
// This method display the searched Employee deatail and also display the prev and next employee detail
public void displaySearchedEmployeeDetail(Employee searchedEmployee){
// display the searchedEmployee employee record
System.out.println("Searched Employee id : "+searchedEmployee.empId);
System.out.println("Searched Employee Name : "+searchedEmployee.empName);
System.out.println("Searched Employee Salary : "+searchedEmployee.empSalary);
System.out.println("\n\t----------||----------");
System.out.println("\t"+searchedEmployee.empName+"'s Previous Employee");
System.out.println("\t----------||----------\n");
// display the prev employee record
System.out.println("Employee id : "+searchedEmployee.prev.empId);
System.out.println("Employee Name : "+searchedEmployee.prev.empName);
System.out.println("Employee Salary : "+searchedEmployee.prev.empSalary);
System.out.println("\n\t----------||----------");
System.out.println("\t"+searchedEmployee.empName+"'s Next Employee");
System.out.println("\t----------||----------\n");
// display the next employee record
System.out.println("Employee id : "+searchedEmployee.next.empId);
System.out.println("Employee Name : "+searchedEmployee.next.empName);
System.out.println("Employee Salary : "+searchedEmployee.next.empSalary);
}
}
public class TestDoublyCircularLinkedList {
public static void main(String[] args) {
// create doubly circular linked lisr
DoublyCircularLinkedList dcll = new DoublyCircularLinkedList();
// now add the first employee detail
dcll.insertFirst(1, "Harshal", 100000);
// add the employee details
dcll.insertLast(2, "Sagar", 50000);
// add the employee details
dcll.insertLast(3, "Mohan", 10000);
// display all employee details
dcll.displayList();
// search employee by empId and display next and previous employee also
dcll.searchEmployeeById(1);
}
}
Output:-
I hope you will understand the above program.
Do you feel needful and useful then please upvote me.
Thank you.