Question

In: Computer Science

Write a program of doubly Circular linked list to maintain records of employees. Take employee ID,...

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.

Solutions

Expert Solution

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.


Related Solutions

A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
C++ Write a program to create a linked list which stores the details of employees(Employee number,...
C++ Write a program to create a linked list which stores the details of employees(Employee number, employee name, rate, hours worked). Create a menu to manage the emoployee data. MENU 1. ADD EMPLOYEE DETAILS 2. DELETE EMPLOYEE 3. SEARCH EMPLOYEE 4. PRINT EMPLOYEE PAYROLL 5. EXIT When the user selected option #4 the program should print the following pay report for each employee: EmpNo.     Name      Rate    Hours    Regular Pay      Overtime Pay     Gross Pay Any hours worked above 40 hours are...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID,...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID, name, designation and department using linked list and perform the following operations on the list. Add employee details based on department Remove employee details based on ID if found, otherwise display appropriate message Display employee details Count the number of employees in each department
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT