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,...
Outcomes: • Write a Java program that implements linked list algorithms can u also show thee...
Outcomes: • Write a Java program that implements linked list algorithms can u also show thee testing code -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- This is the starter code import java.util.NoSuchElementException; // Put your prologue comments here public class LinkedAlgorithms {       private class Node {        private String data;        private Node next;        private Node(String data) {            this.data = data;            this.next = null;        }    }    public Node head;   ...
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...
The Menu-driven Program: Write a menu-driven program to implement a roomsboard for a classroom reservations system....
The Menu-driven Program: Write a menu-driven program to implement a roomsboard for a classroom reservations system. The menu includes the following options: Add new room. The program will prompt the user to enter the roomID, courseID and time of the new reserved room, then will call the add() method from the RoomsBoard class. Remove all occurrences of a room. The program will prompt the user to input the room ID to be removed, then call the remove() method from the...
C++ 14.11 Lab # Map Write a menu driven program to perform following operations using a...
C++ 14.11 Lab # Map 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...
**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...
Write a menu driven program in Java (OOP) That contain class for Botique. Data members include...
Write a menu driven program in Java (OOP) That contain class for Botique. Data members include code ,colour , size ,Quantity Your class should contains app accessors and mutators method Your class should contain Parametric and non-parametric constructors (use constructor chaining) Your class should have input and display method.
Write a C++ program to run a menu driven program with the following choices: 1) Display...
Write a C++ program to run a menu driven program with the following choices: 1) Display the grades 2) Adjust grade 3) Display Average for each student 4) Display number of student with at least a B 5) Quit requirements: 1. Write a function called getValidGrade that allows a user to enter in an integer and loops until a valid number that is >= 0 and <= 100 is entered. It returns the valid value. 2. Write a function called...
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: ?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT