Build a Linked List class Data Node head null the start of
Build a Linked List class
Data
Node head = null; //the start of the linked list
Node tail = null; // The tail of the linked list.
-Use no other instance variables here: No int size etc.
-You probably will implement this as an inner class
Node
An inner class of the LList class. This is the class that contains the data, pNext, pPrev etc.
Required methods in the Node class:
String getData(); Get the data from a node.
Required methods in the LList class
public void insert( String data );
- Adds a new node at the head of the list, puts the String data in the node
public void addTail( String data);
- Adds a new node at the tail of the list. Puts the string data in the node;
public Node getHead( );
- Returns the head node
public Node getTail()
- Returns the tail node
public Node nextNode( Node node)
- Return the next node in the list after node
- Throws LinkedListException if node is null or node is a deleted node
public void remove( int index)
- Removes the node at the specified index;
public void remove( Node deadNode)
- Removes deadNode from the list
- Throws LinkedListException if node is null or node is a deleted node
public void addAfter( String data, Node priorNode)
- Adds a new node after the priorNode
- Throws LinkedListException If prioorNode is deleted or null
public int numNodes()
- Return a count of the number of nodes in the list
public Node getNode( int index)
- Returns the node at the specified index. Returns NULL if no node
public String[] as Array()
- Returns an array of strings where each element is one of the Data elements in the node ( IE the string value in the node) The elements in the array should be in the same order as the nodes in the tree. Element[0] should be the head node.
NOTE – be careful on adding and removing nodes. There are three cases you have to account for:
Adding / removing the first node
Adding / removing the last node
Adding / removing a node in the middle.
Solution
If we compile and run the above program, it will produce the following result
Output

