Question

In: Computer Science

Java Write a menu driven program that implements the following linked list operations : INSERT (at...

Java

Write a menu driven program that implements the following linked list operations :

  • INSERT (at the beginning)
  • INSERT_ALPHA (in alphabetical order)
  • DELETE (Identify by contents, i.e. "John", not #3)
  • COUNT
  • CLEAR

Solutions

Expert Solution

import static java.lang.System.out;
import java.util.Scanner;

public class A10 {

    public static final Scanner CONSOLE = new Scanner (System.in);
  
    public static void main(String[] args) {
      
    int i = 1;
    int x = 1;
    int choice;
    int sizeL;
    
    
    out.println("Please enter the size of this list:");
    sizeL = CONSOLE.nextInt();
    
    while(x == 1) {
      if (i == 3 ) {
        i = 1;
        out.println("Please enter the size of this new list:");
        sizeL = CONSOLE.nextInt();
      }
    
    DoublyLinkList list  = new DoublyLinkList(sizeL);
    while(i == 1) {
    out.println("***DOUBLY LINKED LIST OPERATIONS:****");
    out.println(" MENU ");
    out.println("---------------------------------------");
    out.println(" 1.INSERT (at the beginning) ");
    out.println(" 2.INSERT_ALPHA (in alphabetical order) ");
    out.println(" 3.DELETE (Identify by contents, i.e. \"John\", not #3) ");
    out.println(" 4.COUNT ");
    out.println(" 5.CLEAR ");
    out.println(" 6.EXIT PROGRAM ");
    out.println("--------------------------------------");
    out.println("Enter your choice:\t");
    
    choice = CONSOLE.nextInt();
    
    switch(choice) {

    case 1:
    Scanner CONSOLE1 = new Scanner (System.in);
    out.println("Please enter a name to insert.");
    String name = CONSOLE1.nextLine();
    list.insert(name);
    break;
    
    case 2:
    Scanner CONSOLE2 = new Scanner (System.in);
    out.println("Please enter a name to insert.");
    String name1 = CONSOLE2.nextLine();
    list.insert_Alpha(name1);
    break;
    
    case 3:
    Scanner CONSOLE3 = new Scanner (System.in);
    out.println("Please enter a name to delete.");
    String name2 = CONSOLE3.nextLine();
    list.delete(name2);
    break;

    case 4:
    out.println("Your Count is " +list.count());
    break;

    case 5:
    out.println("The list has been cleared");
    list.clear();
    i = 3;
    break;
    
    case 6:
    out.println("Bye!");
    i++;
    x++;
    break;
    
    default:
    out.println("Wrong choice try again please.");
    break;
  }
}
}
}
    
    
    static class Node{
    public String name;
    private Node next;
    private Node previous;
    }

    static class DoublyLinkList{
    int count, size;
    Node head, tail;
    public DoublyLinkList(int size){
        this.size = size;
        count = 0;
        head = null;
        tail = null;
    }
    
    public int count(){
        return count;
    }
    
    public void clear(){
        head = null;
        tail = null;
        count = 0;
    }
    
    public boolean isFull(){
        if(count < size)
        return false;
        else
        return true;
    }

    public boolean isEmpty(){
        if(count == 0)
        return true;
        else
        return false;
    } 
    
     public void insert(String name) {
         if(isEmpty()){
             head = new Node();
             head.name = name;
             count++;
             tail = head;
         }else if (!isFull()){
            Node current =  new Node();
            current.name = name;
            head.previous = current;
            current.next = head;
            head = current;
            count++;
        }
      }

      public void delete(String Name) {  
         if (isEmpty()) {
             out.println("LinkList is empty");
            return; 
         }else{
            Node prev = null;
            Node temp = null;
            Node current = head; 
         while(current != null){
         if (current.name.equals(Name)){
            if(prev == null){
              if(current.next != null){
            temp = current.next;
            temp.previous = null;
            }
            head = temp;
            }else{
              if(current.next != null){
                 temp = current.next;
                 temp.previous = prev;
                 prev.next = temp;
                 head = prev;
              }
              else{
                head = prev;
              }
              
            }
            count--;
            return; 
            }else{
             prev = current;
             current = current.next;
         }
         }
         }
      }
      
      
       public void insert_Alpha(String Name){
       Node previous = null;
       Node current = head;
       Node newNode = new Node();
       if(!isFull()) {
         newNode.name = Name;
         while(current != null && Name.compareTo(current.name)>0){
         previous = current;
         current = current.next;
         }
         if(previous == null){
           newNode.previous = null;
           newNode.next = current;
           head = newNode;
           count++;
         }
         else{
          newNode.previous = previous;
          newNode.next = current;
          previous.next = newNode;
          head = previous;
          count++;
         }
       }
       }      
       
}
}


Related Solutions

PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
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 a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
Write a menu-driven program to handle the flow of widgets into and out of a warehouse....
Write a menu-driven program to handle the flow of widgets into and out of a warehouse.     The warehouse will have numerous deliveries of new widgets and orders for widgets     The widgets in a filled order are billed at a profit of 50 percent over their cost     Each delivery of new widgets may have a different cost associated with it     The accountants for the firm have instituted a last-in, first-out system for filling orders         the newest...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program should allow the user to select one of the options according to whether lengths, weights or volume are to be converted, read the volue to be converted and the units, and then call the appropriate function to carry out the conversion In unit out unit I C (inch to centimeter 1 in = 2.4 cm) F C (feet to centimeter 1 ft = 30.4...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Write a Java program (use JDBC to connect to the database) that implements the following function...
Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C...
Create a Linked List and conduct the following operations. Portion of the program is given. The...
Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add 100 to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT