Question

In: Computer Science

Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...

Use Java programming to implement the following:

Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates.

  • Default constructor
    Create an empty list i.e., head is null.
  • boolean insert(int data)
    Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false.
  • boolean delete(int data)
    Delete the node that contains the given data from the list. If the deletion is successful, the function returns true; otherwise, returns false.
  • int find(int data)
    Search and return the index to the node that contains the data. Note that the index starts from 0. If it is not found, return -1.
  • int count()
    Count and return the number of nodes in the list
  • String toString()
    Return the string representation of this List where all data in the list are enclosed in square brackets [ ].

Then write the program that accepts a sequence of commands and print out the number of successful insertions, the number of successful deletions, the number of successful searches (through the “find” command), the number of items remaining in the list after executing all commands and the final contents of the list.

There are 3 commands which are “insert”, “delete” and “find”.

Input

Line 1: The number of transactions m on the list, where 1  m 200.

Line 2 to m+1: A string command (“insert”, “delete”, “find”) followed by an integer n (separated by a space).

  • • “insert” means insert n into the list
  • • “delete” means delete n from the list
  • • “find” means find n in the list

Output

Line 1: Display 4 integers (each number is separated by a space) which are:

  • the number of successful insertions,
  • the number of successful deletions,
  • the number of items found (from the command “find”), and
  • the number of items remaining in the list

Line 2: The final contents of the list

Sample Input

Sample Output

3

insert 1

delete 5

find 2

1 0 0 1

[ 1 ]

8

find 10

insert 3

insert 2

insert 1

delete 4

delete 3

insert 1

find 2

3 1 1 2

[ 2 1 ]

Solutions

Expert Solution

Hello, I am writing the code in java with comments as you mentioned please go through it and if you have any doubt, feel free to ask in comment and if you like this answer, It will be helpful for me by your upvote.

Thank you and enjoy your answer,
import java.util.Scanner;
import java.lang.String;
class LinkedList 
{ 
  Node head; // head of list 

  /* Linked list Node*/
  class Node 
  { 
    int data; 
    Node next; 
    Node(int d) {data = d; next = null; } 
  } 

  /* inserts a new node at the end. This method is 
  defined inside LinkedList class shown above */
  public boolean insert(int new_data) 
  { 
    Node new_node = new Node(new_data); 

    /* If the Linked List is empty, then make the 
      new node as head */
    if (head == null) 
    { 
      head = new Node(new_data); 
      return true; 
    } 

    if(find(new_data)){
      return false;
    }

    /* This new node is going to be the last node, so 
      make next of it as null */
    new_node.next = null; 

    /* Else traverse till the last node */
    Node last = head; 
    while (last.next != null) 
      last = last.next; 

    /* Change the next of last node */
    last.next = new_node; 
    return true; 
  } 

  //Checks whether the value x is present in linked list 
  public boolean find(int x) 
  { 
      Node current = head;    //Initialize current 
      while (current != null) 
      { 
          if (current.data == x) 
              return true;    //data found 
          current = current.next; 
      } 
      return false;    //data not found 
  } 

  /* for delete the node with given value */
  public boolean delete(int key)
    {
        // Store head node
        Node temp = head, prev = null;
 
        // If head node itself holds the key to be deleted
        if (temp != null && temp.data == key)
        {
            head = temp.next; // Changed head
            return true;
        }
 
        // Search for the key to be deleted, keep track of the
        // previous node as we need to change temp.next
        while (temp != null && temp.data != key)
        {
            prev = temp;
            temp = temp.next;
        }    
 
        // If key was not present in linked list
        if (temp == null) return false;
 
        // Unlink the node from linked list
        prev.next = temp.next;
        return true;
    }

  /* This function prints contents of linked list starting from 
    the given node */
  public void printList() 
  { 
    Node tnode = head;
    System.out.print("[ ");
    while (tnode != null) 
    { 
      System.out.print(tnode.data+" "); 
      tnode = tnode.next; 
    }
    System.out.print("]");
  } 

  /* method to count the number of node in list */

  public int count()
  {
    int count = 0;
    Node tnode = head;
    // while
    while (tnode != null) 
    { 
      tnode = tnode.next; 
      count = count + 1;
    }
    return count;
  }

  /* Driver program to test above functions. */
  public static void main(String[] args) 
  { 
    /* Start with the empty list */
    LinkedList mylist = new LinkedList(); 

    Scanner myObj = new Scanner(System.in);
    /* taking the number from user*/
    int m = myObj.nextInt();
    int insert = 0, find = 0, delete = 0;
    for(int i = 0 ; i< m; i++)
    {
      
      Scanner myObj1 = new Scanner(System.in);
      // taking the input from user
      String input = myObj1.nextLine();

      String[] strList = input.split(" ");

      int value = Integer.parseInt(strList[1]);

      if(strList[0].equals("insert")){
        if(mylist.insert(value)){
          insert = insert + 1;
        }
      }
      if(strList[0].equals("find")){
        if(mylist.find(value)){
          find = find + 1;
        }
      }
      if(strList[0].equals("delete")){
        if(mylist.delete(value)){
          delete = delete + 1;
        }
      }
    } 

    System.out.println(insert + " " + delete + " " + find + " "+ mylist.count());
 
    mylist.printList(); 
  } 
} 

// Output:

Thank you, Have a nice day.


Related Solutions

Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java Create a queue class to store integers and implement following methods: 1- void enqueue(int num): This method will add an integer to the queue (end of the queue). 2- int dequeue(): This method will return the first item in the queue (First In First Out). 3- void display(): This method will display all items in the queue (First item will be displayed first). 4- Boolean isEmpty(): This method will check the queue and if it is empty,...
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept...
JAVA PROGRAMMING Implement a class Purse. A purse contains a collection of coins. For simplicity, we...
JAVA PROGRAMMING Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList<String>. Supply a method void addCoin(String coinName). Add a method toString to the Purse class that prints the coins in the purse in the format Purse[Quarter,Dime,Nickel,Dime]. Write a method reverse that reverses the sequence of coins in a purse. Implement a TestPurse class with a main method in it. It will use the toString method to...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you must write has comments describing how it should behave. You may not add any new fields to the LinkedIntSet class and you may only add new code inside the bodies of the 5 methods you are asked to write. You may also write new private helper methods. However, you may not change any method headers, you may not change any code outside of the...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's...
This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's and JSP 3rd Edition. I need help modifying some code. I will post the code I was told to open that needs to be modified below. In this exercise, you'll enhance the Future Value application to store the amount and interest rate in the user's session. That way, the user can experiment with different numbers of years to see the value of his or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT