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

Write a menu driven Java program which uses a method for each of the following operations:...
Write a menu driven Java program which uses a method for each of the following operations: (Note : The user should be allowed to repeat the operations as long as he wants to. Use appropriate number of parameters and return type for each method.) A. to find the sum of the following series (up to N terms). The program should    display the terms:              22 + 42 + 62… For example, if N=4, then the program should display the following...
C++ ^ ^ Write a menu driven program to perform following operations using a map container...
C++ ^ ^ Write a menu driven program to perform following operations using a map container that stores the information about USA Population (In Million). @@@Menu@@@ 1. Add Information 2. Display Information 3. Update Information 4. Erase Information 5. Clear Information For example, Suppose the map initially contains following information (Use emplace() function to store the information) 2010, 309.33 2011, 311.58 2012, 313.87 2015, 320.74 2016, 323.07 The program should produce the desired output when a valid input is provided,...
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: ?...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with class SLLNode listed as inner class. TestIntegerSLL.java that tests the SLL class by using a linked list of Integer. In SLL class add the following method:                                                                    public void moveToEnd (int i) It will move the element at the i -th position to the end of the list. You can assume i to be within the list, and that the first element has the...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT