Question: Single Linked List (Java assignment ) Today's you will write a program with three classes, the first class will be the driver, second will be

Single Linked List (Java assignment )

Today's you will write a program with three classes, the first class will be the driver, second will be the LinkedList class, and the third will be the Node class that will be holding the data.

The first class, Driver class will just create an instance of the Linked List class and then control the flow of the program.

The Second class with be the Node class, this is where you store each individual Student's name which is your data, it will have:

Two private variables (no need for a new class, just put the name in the Node class):

A String to hold the name of the student

Node next

A setNext() method that will allow you to set the reference of the instance variable next.

A getNext() method that will return the Item that next is referencing.

A toString() method to print out the name of the student

A getName() to return the name for comparison

All names will be initialized in constructor and you don't need methods to change them, just retrieve them for printing or searching.

The second class will be the class that manages the linked list that is made up of the Node objects. The LinkedList Class will contain:

An instance field Node reference to the first Node object in the list. (starts null for empty list)

Then you will need methods for handling all the menu choices in the driver.

Note: After you get one of the print or delete methods working the traversal is the same for both of them.

All items will be added to the end of the list, not the front.

Clear the list is just setting the correct reference to null, very simple. all of the other methods I did on the board.

The third class is the Driver, just like my example, should contain:

A menu that calls a method that is an infinite do/while loop until the user gives -1 to exit.

Inside the loop you should print a menu and then wait for user input.

Methods for printing the menu:

What would you like to do? Press 1 to add an item to the back of the list Press 2 to print the list Press 3 to delete an item Press 4 to clear the whole list Press -1 to exit 

The input from the user should go into a switch statement that switches for each input:

case 1:

linkedlist.addItem(node);

. The menu method should consistently run until the user gives a -1.

You are to implement a linked list of the Node objects. Singular linked list. Assume new objects are always added to the back of the list.

Make sure you handle special cases such as adding an item to an empty list, deleting the first, etc. etc.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!