JAVA - doubly linked list superclass(help with building methods) The List Class
ID: 3774587 • Letter: J
Question
JAVA - doubly linked list superclass(help with building methods)
The 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.
---start---
----
above is context,
need help with below
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.
---exception class---
Explanation / Answer
/* Doesn't contain impelmetation of remove(Node deadNode), addAfter(), numNodes(), getNode(), String as Array()
As it has lots of subparts considering each function as a subpart of the question cannot be done fully in timeframe
*/
public void insert( String data ); // Adds a new node at the head of the list
{
Node newnode = new Node(data);
newnode.pPrev = head;
newnode.pNext = head.pNext;
head.pNext = newnode;
newnode.pNext.pPrev = newnode;
}
public void addTail( String data); //Adds a new node at the tail of the list.
//Puts the string data in the node;
{
Node newnode = new Node(data);
newnode.pNext = tail;
newnode.pPrev = tail.pPrev;
tail.pPrev = newnode;
newnode.pPrev.pNext = newnode;
}
public Node getHead( ) // Returns the head node
{
return head;
}
public Node getTail() //Returns the tail node
{
return tail;
}
public boolean isEmpty() {
return (head == null);
}
/* -Return the next node in the list after node */
/ * Throws LinkedListException if node is null or node is a deleted node */
public Node nextNode( Node node) throws LinkedListException
{
return node.pNext.pNext;
}
public void remove( int index) // Removes the node at the specified index;
{
Node currentNode = head;
for (int i = 0; i < index; i++) {
if (index < 0) {
System.out.println("The index is out of bounds");
return;
} else if (currentNode == null) {
System.out.println("The list is empty");
return;
} else if (i == index - 1) {
Node nextNode = currentNode.pNext;
Node prevNode = currentNode.pPrev;
prevNode.pNext = nextNode;
nextNode.pPrev = prevNode;
return;
}
currentNode = currentNode.pNext;
}
}
}
// Exception Class
public class LinkedListException extends Exception{
public LinkedListException(){
super();
}
public LinkedListException(String message){
super(message);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.