Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Re-Implementing a doubly-Linked lists package list; abstract public class ListNode{ protected Object item; protected List myList; public boolean isValidNode(){ return myList != null; } //next()

Re-Implementing a doubly-Linked lists

package list;

abstract public class ListNode{

protected Object item;

protected List myList;

public boolean isValidNode(){

return myList != null; }

//next() returns the node following this node. If this node is invalid, throws an exception.

public abstract ListNode next() throws InvalidNodeException;

//prev() returns the node preceding this node

public abstract ListNode prev() throws InvalidNodeException;

//insertBefore() inserts a node holding this "item" immediately preceding this node. If this node is invalid, throws an exception.

public abstract void insertBefore(Object item) throws InvalidNodeException;

// insertAfter() inserts a node holding this "item" immediately following this node. If this node is invalid, throws an exception.

public abstract void insertAfter(Object item) throws InvalidNodeException;

//remove() removes this node from its list. If this node is invalid, throws an exception.

public abstract void remove() throws InvalidNodeException;

}

package list;

abstract public class List{

protected int size;

//insertFront() inserts a node holding this "item" at the front of this list.

public abstract void insertFront(Object item);

//insertBack() inserts a node holding this "item" at the back of this list.

public abstract void insertBack(Object item);

public abstract ListNode front();

public abstract ListNode back();

public abstract String toString();

}

package list;

public class InvalidNodeException extends Exception {

protected InvalidNodeException() { super(); }

protected InvalidNodeException(String s) { super(s); }

}

package list;

public class DListNode extends ListNode{

protected DListNode prev;

protected DListNode next;

DListNode(Object o, DList dl, DListNode p, DListNode n){

//Your code }

public ListNode next() throws InvalidNodeException{

if(!isValidNode()){throw new InvalidNodeException("next() is called on invalid node");}

;//Your code

}

public ListNode prev() throws InvalidNodeException{

if(!isValidNode()){throw new InvalidNodeException("prev() is called on invalid node");}

;//Your code

}

public void insertBefore(Object item) throws InvalidNodeException{

if(!isValidNode()){throw new InvalidNodeException("insertBefore() is called on invalid node");}

//Your code

}

public void insertAfter(Object item) throws InvalidNodeException{

if(!isValidNode()){throw new InvalidNodeException("insertAfter() is called on invalid node");}

//Your code

}

public void remove() throws InvalidNodeException{

if(!isValidNode()){throw new InvalidNodeException("remove() is called on invalid node");}

//Your code

prev = null; next = null; myList = null;

}

}

package list;

public class DList extends List{

protected DListNode head;

protected DListNode newNode(Object item, DList list, DListNode prev, DListNode next){

return new DListNode(item, list, prev, next);

}

public DList(){ //Your code

}

public void insertFront(Object item){ //Your code

}

public void insertBack(Object item){ //Your code

}

public ListNode front(){ return head.next; }

public ListNode back(){ return head.prev; }

public String toString(){

//Your code

}

}

1) finish coding the 6 methods defined in the DListNode class, and explain your design. DListNode(Object o, DList dl, DListNode p, DListNode n){ //Your code }

public ListNode next() throws InvalidNodeException{ if(!isValidNode()){ throw new InvalidNodeException("next() is called on invalid node");} ;//Your code }

public ListNode prev() throws InvalidNodeException{ if(!isValidNode()){ throw new InvalidNodeException("prev() is called on invalid node");} ;//Your code }

public void insertBefore(Object item) throws InvalidNodeException{ if(!isValidNode()){ throw new InvalidNodeException("insertBefore() is called on invalid node");} //Your code }

public void insertAfter(Object item) throws InvalidNodeException{ if(!isValidNode()){ throw new InvalidNodeException("insertAfter() is called on invalid node");} //Your code }

public void remove() throws InvalidNodeException{ if(!isValidNode()){ throw new InvalidNodeException("remove() is called on invalid node");} //Your code prev = null; next = null; myList = null; }

2) finish coding the 4 methods defined in the DList class, and explain your design. public DList(){ //Your code }

public void insertFront(Object item){ //Your code }

public void insertBack(Object item){ //Your code }

public String toString(){ //Your code }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions