Question

Write a java program implementing the Linked list. It should be on an small office who...

Write a java program implementing the Linked list.

It should be on an small office who has 5 employees. The program ask the user for ID, First name, Last name and what field the work in(eg: accounting, programmer, HR etc). Each employee (with all the information of that paticular employee) should be placed in one node in the program. The program should repeat and ask the user for all 5 employees information. Also when you display the Linked list it should be sorted by increasing order of the Employee ID. Employee Id should be the primary key . The program should also be able to delete an employee by their ID number. It should have an menu to ask the user for the following options:

  1. To add an new employee
  2. To delete an employee
  3. To search an employee
  4. To display list of employee

Whole program is based on linked list and should be sorted.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

class ListNode{
   int ID;
   String fname;
   String lname;
   String fwork;
   ListNode next;
  
   public ListNode(int id,String name,String lname,String work){
       this.ID=id;
       this.fname=name;
       this.lname=lname;
       this.fwork=work;
   }
}

public class Employee {
   ListNode head;
   ListNode sorted;
  
   void push(int id,String name,String lname,String work){
       ListNode newnode=new ListNode(id,name,lname,work);
       newnode.next=head;
       head=newnode;
   }
  
   //..........utility function to sort the list............
void insertionSort(ListNode headref){
   sorted=null;
   ListNode current=headref;
  
   //pick node 1 by 1 and create a soted list ....
   while(current!=null){
       ListNode next=current.next;
       sortedInsert(current);
       current=next;
   }
   //assign the sorted list to orignal head node
   head=sorted;
}   
  
void sortedInsert(ListNode newnode){
   if(sorted==null||sorted.ID>=newnode.ID){
       newnode.next=sorted;
       sorted=newnode;
   }else{
       ListNode current=sorted;
       while (current.next != null && current.next.ID < newnode.ID)
{
current = current.next;
}
newnode.next = current.next;
current.next = newnode;
   }
}
  
//..................utility ends.......................
  
   void printlist(ListNode head)
{
       ListNode temp=head;
      
       if(temp==null){
           System.out.println("List is empty");
       }
while (temp != null)
{
System.out.println("Name:"+temp.fname +" "+temp.lname+"ID:"+temp.ID+" Work:"+temp.fwork);
temp = temp.next;
}
}
  
   void delete(int id){
       //variable to hold node to be deleted and previous node..
       ListNode temp=head,prec=null;
      
       //if node to be delted is first node itself
       if(temp!=null && temp.ID==id){
           head=temp.next;
           return;
       }
      
       //traverse through the list to find node to be deleted
       while(temp!=null && temp.ID!=id){
           prec=temp;
           temp=temp.next;
       }
      
       if(temp==null){
       System.out.println("Employee is not present in the list..");
           return;
       }
       prec.next=temp.next;
      
      
   }

  
  
public static void main(String[] args){
  
   Employee list=new Employee();
   int temp_NumOfEmployeesToEnter=5;
  
   while(true){
   System.out.println("1.To add an new employee\n"+
           "2.To delete an employee\n"+
           "3.To search an employee\n"+
           "4.To display list of employee\n");
   Scanner sc=new Scanner(System.in);
   int temp=sc.nextInt();
   switch(temp){
   case 1:
       System.out.println("Enter ID:");
       int id=sc.nextInt();
       System.out.println("Enter first name:");
       Scanner scc=new Scanner(System.in);
       String fname=scc.nextLine();
       System.out.println("Enter last name:");
       String lname=scc.nextLine();
       System.out.println("Enter workProfile:(eg: accounting, programmer, HR etc)");
       String work=scc.nextLine();
      
       list.push(id, fname, lname, work);
       temp_NumOfEmployeesToEnter--;
       if(temp_NumOfEmployeesToEnter<=0){
           continue;
       }else{
           System.out.println("optional-Enter information for "+temp_NumOfEmployeesToEnter+ " more employees");
       }
  
       break;
  
   case 2:
       System.out.println("Enter the employee ID to be deleted");
   id=sc.nextInt();
       list.delete(id);
   break;
  
   case 3:
       System.out.println("Enter the employee ID to be searched");
       int x=sc.nextInt();
      
       ListNode current =list.head;
   boolean temp_var=true;
   //traverse list to find the employee...
       while(current!=null){
           if(current.ID==x){
               System.out.println("Name:"+current.fname +" "+current.lname+"ID:"+current.ID+" Work:"+current.fwork);
           temp_var=false;
           }
           current=current.next;
       }
       if(temp_var){
       System.out.println("Employee with ID "+x+" does not exits");
       }
   break;
  
   case 4:
       list.insertionSort(list.head); //sorting list and printing..
       list.printlist(list.head);
       break;
   default:
       System.out.println("Pleas enter valid response");
   }
   }
  
   }

}

//-----------------------------------Screen shot-------------------------------------//

Add a comment
Know the answer?
Add Answer to:
Write a java program implementing the Linked list. It should be on an small office who...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Linked List in Java The data node should be modeled somewhat like this: class node{ int...

    Linked List in Java The data node should be modeled somewhat like this: class node{ int node iNum; node next; } Write a program that creates a linked list and loads it with the numbers 0 to 9. Start with an empty list and then use a "for loop" to fill it. Create a linked list class, a node class, etc. Routines like makeNode and findTail should be methods in the linked list class. Create a showList function to display...

  • JAVA please Java problem that has to be in doubly linked list. It is game problem...

    JAVA please Java problem that has to be in doubly linked list. It is game problem that I have to keep of the players' scores. this should have a Java blueprint class named player   It should have two fields, the name and score and include the constructors, toString() method, and getters and setters. Scores must be kept in ascending order (smallest at the front ) in a doubly linked list. It has menu as well: Load original data Print the...

  • write a java program implementing stacks using arrays. (not with linked list)

    write a java program implementing stacks using arrays. (not with linked list)

  • Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...

    Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...

  • Create a linked list of at least 15 different values that are prompted for and entered...

    Create a linked list of at least 15 different values that are prompted for and entered while the program is running. Appending a node attaches that node to the end of the list while inserting a node places it in order maintaining a sorted list. Create a menu driven program where you have the options to appended, insert, display, and delete a node. Display the list after first appending data showing it in no specific order, delete all nodes and...

  • team, create a Java program that adds or removes entries from a linked list based on...

    team, create a Java program that adds or removes entries from a linked list based on user input. You can use the index of the entry as the key to identify the record for addition or deletion. The program can be as simple as you like but must add an entry to the list based on the user input and also must delete the proper item in the list based on the index entered by the user Display your list...

  • Program is to be written In C++, The output should look like the screen shot. It...

    Program is to be written In C++, The output should look like the screen shot. It should allow the user to continue to ask the user to enter all employee ID's until done and then prompt the user to enter the hours and pay rate for each employee ID. Please help:( Can you please run the program to make sure the output is just like the screenshot please? It needs to have the output that is in the screenshot provided,...

  • please solve this question: Write a character Max-Heap Builder program in C++. The program should display...

    please solve this question: Write a character Max-Heap Builder program in C++. The program should display the menu below. Each item in the menu should be implemented in a function. a. Add a node. One node to be added to the max-heap. b. Delete a node. One node to be deleted from the max-heap. C. Search a node. Returns true if the node exists in the max-heap, otherwise it returns false. d. Print the tree. Prints the heap in level-order...

  • C++ Linked Lists You have been hired by Employees. Inc to write an employee management system....

    C++ Linked Lists You have been hired by Employees. Inc to write an employee management system. The following are your specifications: Write a program that uses the following linked lists: bullet empId: a linked list of seven long integers to hold employee identification numbers. The array should be initialized with the following numbers: 5658845 4520125 7895122 8777541 8451277 1302850 7580489 bullet hours: a linked list of seven integers to hold the number of hours worked by each employee bullet payRate:...

  • Write a MIPS assembly language program that uses dynamic memory allocation to create and manage a...

    Write a MIPS assembly language program that uses dynamic memory allocation to create and manage a linked list data structure. Gives the user the following options:                         1. To create and add the first node to a linked list.          2. To add a single node to the pre-existing linked list.             The list must already exist before a new node can be added.            The nodes should be maintained in ascending order based on the data value within the nodes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT