Question
/ Please complete the following java code???? //____________________________________________________________________________________________________- //__________________________________________________________________________________________________ public class LinkedList { private Node head = null; private Node tail = null; // Because
/ Please complete the following java code???? //____________________________________________________________________________________________________- //__________________________________________________________________________________________________ public class LinkedList { private Node head = null; private Node tail = null;
// Because LinkedList and Node use same element type , // I don't need to use another generic for internal Node Class (I used it in our class, but not now) public class Node { E data; Node next; //Node constructor public Node(E element) { data = element; next = null; } //Node toString method to print Node element public String toString() { return String.valueOf(this.data); } }
public void add(int index, E element) { /////////////////////////// // Write your code here! // /////////////////////////// // add an element(as a node) at given index } public void addFirst(E element) { /////////////////////////// // Write your code here! // /////////////////////////// // add an element(as a node) at the first of the list } public void addLast(E element) { /////////////////////////// // Write your code here! // /////////////////////////// // add an element(as a node) at the last of current list } public E getFirst() { /////////////////////////// // Write your code here! // /////////////////////////// // Return the element value of first node } public E getLast() { /////////////////////////// // Write your code here! // /////////////////////////// // Return the element value of first node } public void remove(int index) { /////////////////////////// // Write your code here! // /////////////////////////// // Remove an element(a node) at given index }
public E removeFirst() { /////////////////////////// // Write your code here! // /////////////////////////// // Return the element value of first node // Remove an element(a node) at given index } public E removeLast() { /////////////////////////// // Write your code here! // /////////////////////////// // Return the element value of first node // Remove an element(a node) at given index } public int size() { /////////////////////////// // Write your code here! // /////////////////////////// // Return how many elements in current list // Don't keep the size value, when size() method called, // retrieve whole list and count the number of element and return it } public String toString() { Node temp = head; String str = "["; while(temp.next != null) { str = str + temp.data + ", "; temp = temp.next; } str = str + temp.data; return str + "]"; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Hint:
Your test program will runs with sentinal loop with user input.
The main menu will be similar as follows.
-------Linked List Test Program------ 0 Exit Program 1 Add First Node 2 Add Last Node 3 Add at Index 4 Remove First Node 5 Remove Last Node 6 Remove at Index 7 Print List Size Select Option: user input here |
Except for options 0 and 7 above, when calling a method on the list make sure the method prints the list content before and after executing the called method, For example, the output of testing method removeLast() would be displayed like this
(assuming the list has 10, 20, 30, 40, 50):
Testing method removeLastNode() List content before removing last node is: 10 20 30 40 50 List content after removing last node is: 10 20 30 40 |
User choose remove option but there is no element in the List, you need to show the message like this
List is Empty now. |
Except for options 0 and 7 above, when calling a method on the list make sure the method prints the list content before and after executing the called method, For example, the output of testing method removeLast() would be displayed like this
(assuming the list has 10, 20, 30, 40, 50):
Testing method removeLastNode() List content before removing last node is: 10 20 30 40 50 List content after removing last node is: 10 20 30 40 |
|
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