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

80.2K

Verified Solution

Question

Programming

Use Java programming to implement thefollowing:

Implement the following methods in the UnorderedList class formanaging a singly linked list that cannot containduplicates.

  • 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. Ifthe 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 thedeletion 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 thelist are enclosed in square brackets [ ].

Then write the program that accepts a sequence of commands andprint out the number of successful insertions, the number ofsuccessful deletions, the number of successful searches (throughthe “find” command), the number of items remaining in the listafter executing all commands and the final contents of thelist.

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

Input

Line 1: The number of transactions mon 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 isseparated 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 ]

Answer & Explanation Solved by verified expert
4.0 Ratings (533 Votes)
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 upvoteThank you and enjoy your answerimport javautilScannerimport javalangStringclass LinkedList Node head head of list Linked list Node class Node int data Node next Nodeint d data d next null inserts a new node at the end This method is defined inside LinkedList class shown above public boolean insertint newdata Node    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students