In: Computer Science
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.
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).
Output
Line 1: Display 4 integers (each number is separated by a space) which are:
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 ] |
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.