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

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...
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.
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.
List department name, employee id, and employee name for all employees in department name order. Repeat...
List department name, employee id, and employee name for all employees in department name order. Repeat for department #10 only. List the course ID, course name, section, instructor name, day, time, and room for all course sections. List the course ID, course name, section, student ID, and student name for CRN 1003. Display the list in ascending order of student last and first names. DROP TABLE registration; DROP TABLE sections; DROP TABLE courses; DROP TABLE students; DROP TABLE instructors; CREATE...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
Change program linkedListClass to the linked list for student items (a student item contains id, name...
Change program linkedListClass to the linked list for student items (a student item contains id, name and score, where the id is used as key) and test it in the main method. You can define a student item using a class of student. given the following codes; import java.util.*; public class linkedListClass {    public Node header;    public linkedListClass()    {        header = null;    }    public final Node Search(int key)    {        Node...
In this problem, you will write a program that reverses a linked list. Your program should...
In this problem, you will write a program that reverses a linked list. Your program should take as input a space-separated list of integers representing the original list, and output a space-separated list of integers representing the reversed list. Your algorithm must have a worst-case run- time in O(n) and a worst-case space complexity of O(1) beyond the input. For example, if our input is: 5 7 1 2 3 then we should print: 3 2 1 7 5 Please...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT