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.
This my code and I have problem with print method and clear the whole list.
import java.util.Scanner;
//Class Node definition
class Node
{
//Instance variable to store value of type integer
protected String studentName;
//Node class object created to store its own reference
//Instance variable to point to a node
protected Node next;
//Default constructor to initialize
public Node()
{
//Object is initialized to null
next = null;
//studentName is initialized to null
studentName = "";
}//End of constructor
//Parameterized constructor to initialize with specific value
public Node(String name, Node n)
{
//value is assigned with name value
studentName = name;
//Points to new node
next = n;
}//End of constructor
//Method to set link to next Node
public void setNext(Node n)
{
next = n;
}//End of method
//Method to get link to next node
public Node getNext()
{
//Returns the current reference
return next;
}//End of method
//Method to get data from current Node
public String getName()
{
//Return the current node data
return studentName;
}//End of method
}//End of class Node
//Class linkedList definition
class linkedList
{
//Reference object to point to starting node
protected Node start;
//Reference object to point to last node
protected Node end ;
//To store the size of the list
public int size;
//Default constructor to initialize
public linkedList()
{
//Start and last object is initialized to null
start = null;
end = null;
//Size is initialized to zero
size = 0;
}//End of constructor
//Method to check if list is empty
public boolean isEmpty()
{
//Returns true if the list is empty
return start == null;
}//End of method
//Method to insert a node at the end
public void insertAtEnd(String name)
{
//Creates a new node
Node newNode = new Node(name, null);
//Increases the size by one
size++ ;
//Checks if start is null then it is the first node
if(start == null)
{
//Start is pointing to new node
start = newNode;
end = start;
}//End of if
else
{
//end pointing to new node
end.setNext(newNode);
//New node reference to end node
end = newNode;
}//End of else
}//End of method
//Method to delete a node at position
public void deleteAtPos(int pos)
{
//Checks if the specified position is one for starting node
if (pos == 1)
{
start = start.getNext();
//size is reduced by one
size--;
return ;
}//End of if
//Checks if the position is equal to size for last node
if (pos == size)
{
//Creates a reference to refer to start node
Node sNode = start;
//Creates a temporary reference to refer to start node
Node tNode = start;
//Loops till end of the list
while (sNode != end)
{
//Assigns start node reference to temporary node
tNode = sNode;
//Points to next node
sNode = sNode.getNext();
}//End of while loop
//Temporary node reference is assigned to end node
end = tNode;
//End points to null
end.setNext(null);
//Reduce the size by one
size --;
return;
}//End of if
//For other than first or last
//Creates a reference to start node
Node ptr = start;
//Decrease the position by one because it starts from zero
pos = pos - 1 ;
//Loops till end of list
for (int counter = 1; counter < size - 1; counter++)
{
//Checks if the counter value is equal to position specified
if (counter == pos)
{
//Creates a temporary reference refers to next node
Node tmpNode = ptr.getNext();
//Temporary node refers to next node
tmpNode = tmpNode.getNext();
ptr.setNext(tmpNode);
break;
}//End of if
ptr = ptr.getNext();
}//End of for loop
//Decrease the size by one
size-- ;
}//End of method
//Method to display elements of the list
public void print()
{
System.out.print(" Linked List Contents ");
//Checks if the size is zero
if (size == 0)
{
//Displays the error message empty
System.out.print("ERROR: Empty list ");
return;
}//End of if
//If only one node
else if (start.getNext() == null)
{
//Displays the node value
System.out.println(start.getName() );
return;
}//End of if
//Creates a temporary reference to refer to start node
Node tempNode = start;
//Display the value of the start node
System.out.print(start.getName()+ "->");
//Points to the next node
tempNode = start.getNext();
//Loops till not null
while (tempNode.getNext() != null)
{
//Display the value of the current node
System.out.print(tempNode.getName()+ "->");
//Points to the next node
tempNode = tempNode.getNext();
}//End of while loop
System.out.print(tempNode.getName()+ " ");
}//End of method
//Method to delete all nodes
void deleteAll()
{
//Start is initialized to null
start = null;
//Set the size to zero
size = 0;
}
}//End of classLinkedList
//Driver class LinkedListDriver definition
public class LinkedListDriver
{
//Displays menu
static void menu()
{
System.out.println(" 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");
System.out.println(" Enter your choice: ");
}//End of method
//main method definition
public static void main(String[] args)
{
//Scanner class object created
Scanner sc = new Scanner(System.in);
//Creating object of class linkedList
linkedList list = new linkedList();
//Loops till -1 is entered
do
{
//Displays menu
menu();
//Accepts user choice
int choice = sc.nextInt();
//Switch case begins
switch(choice)
{
case 1:
System.out.print(" Enter a name to insert: ");
list.insertAtEnd(sc.next());
break;
case 2:
list.print();
break;
case 3:
System.out.print(" Enter the position to delete: ");
list.deleteAtPos(sc.nextInt());
break;
case 4:
list.deleteAll();
break;
case -1:
System.exit(0);
default:
System.out.println("Invalid choice.");
}//End of switch - case
}while(true);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started