Design implement testIn Java a doubly linked list ADT using

Design, implement, test(In Java ) a doubly linked list ADT, using DLLNode objects as the nodes. In addition to our standard list operations, your class should provide for backward iteration through the list. To support this operation, it should export a resetBack method and get a getPrevious method.To facilitate this, you may want to include an instance variable last that always references the last element on the list.

DLLNODE.java

public class DLLNode<T> extends LLNode<T>
{
   private DLLNode<T> back;
  
   public DLLNode(T info)
   {
       super(info);
       back = null;
   }

   public void setBack(DLLNode<T> back)
   // Sets back link of this DLLNode.
   {
       this.back = back;
   }

   public DLLNode<T> getBack()
   // Returns back link of this DLLNode.
   {
       return back;
   }
}

LLNODE.java


public class LLNode<T>
{
private LLNode<T> link;
private T info;
  
public LLNode(T info)
{
this.info = info;
link = null;
}

public void setInfo(T info)
// Sets info of this LLNode.
{
this.info = info;
}

public T getInfo()
// Returns info of this LLONode.
{
return info;
}

public void setLink(LLNode<T> link)
// Sets link of this LLNode.
{
this.link = link;
}

public LLNode<T> getLink()
// Returns link of this LLNode.
{
return link;
}
}

This list can be an unsorted list. My professor suggested to reference off the RefUnsortedList ADT and modifying it to be a doubly linked list.you need to create resetBack and getPrevious methods which means you will need another variable to keep track of where you are in the backward iteration. It is not correct to use currentPos to keep track of the backward iteration, so make another variable (reference) for this, maybe call it currentBackPos.

RefUnsortedList.java

public class RefUnsortedList<T> implements ListInterface<T>
{

protected int numElements; // number of elements in this list
protected LLNode<T> currentPos; // current position for iteration

// set by find method
protected boolean found; // true if element found, else false
protected LLNode<T> location; // node containing element, if found
protected LLNode<T> previous; // node preceeding location

protected LLNode<T> list; // first node on the list

public RefUnsortedList()
{
numElements = 0;
list = null;
currentPos = null;
}

public void add(T element)
// Adds element to this list.
{
LLNode<T> newNode = new LLNode<T>(element);
newNode.setLink(list);
list = newNode;
numElements++;
}

protected void find(T target)
// Searches list for an occurence of an element e such that
// e.equals(target). If successful, sets instance variables
// found to true, location to node containing e, and previous
// to the node that links to location. If not successful, sets
// found to false.
{
location = list;
found = false;

while (location != null)
{
if (location.getInfo().equals(target)) // if they match
{
found = true;
return;
}
else
{
previous = location;
location = location.getLink();
}
}
}

public int size()
// Returns the number of elements on this list.
{
return numElements;
}

public boolean contains (T element)
// Returns true if this list contains an element e such that
// e.equals(element); otherwise, returns false.
{
find(element);
return found;
}

public boolean remove (T element)
// Removes an element e from this list such that e.equals(element)
// and returns true; if no such element exists, returns false.
{
find(element);
if (found)
{
if (list == location)   
list = list.getLink(); // remove first node
else
previous.setLink(location.getLink()); // remove node at location

numElements--;
}
return found;
}

public T get(T element)
// Returns an element e from this list such that e.equals(element);
// if no such element exists, returns null.
{
find(element);
if (found)
return location.getInfo();
else
return null;
}
  
public String toString()
// Returns a nicely formatted string that represents this list.
{
LLNode<T> currNode = list;
String listString = \"List:\ \";
while (currNode != null)
{
listString = listString + \" \" + currNode.getInfo() + \"\ \";
currNode = currNode.getLink();
}
return listString;
}

public void reset()
// Initializes current position for an iteration through this list,
// to the first element on this list.
{
currentPos = list;
}

public T getNext()
// Preconditions: The list is not empty
// The list has been reset
// The list has not been modified since most recent reset
//
// Returns the element at the current position on this list.
// If the current position is the last element, then it advances the value
// of the current position to the first element; otherwise, it advances
// the value of the current position to the next element.
{
T next = currentPos.getInfo();
if (currentPos.getLink() == null)
currentPos = list;
else
currentPos = currentPos.getLink();
return next;
}
}

Thank you in Advance!

Solution

SOURCE CODE:

DLLNode.java

public class DLLNode<T>
{
private DLLNode<T> nextLink,prevLink;
private T info;
  
public DLLNode(T info)
{
this.info = info;
nextLink = null;
}

public void setInfo(T info)
// Sets info of this DLLNode.
{
this.info = info;
}

public T getInfo()
// Returns info of this LLONode.
{
return info;
}

public void setNextLink(DLLNode<T> nextLink)
// Sets the next link of this DLLNode.
{
this.nextLink = nextLink;
}

public DLLNode<T> getNextLink()
// Returns next link of this DLLNode.
{
return nextLink;
}

public void setPrevLink(DLLNode<T> prevLink)
// Sets the next link of this DLLNode.
{
this.prevLink = prevLink;
}

public DLLNode<T> getPrevLink()
// Returns previous link of this DLLNode.
{
return prevLink;
}
}

RefUnsortedList.java

public class RefUnsortedList<T> //implements ListInterface<T>
{

protected int numElements; // number of elements in this list
protected DLLNode<T> currentPos; // current position for iteration
protected DLLNode<T> currentBackPos; //current back positiion
// set by find method
protected boolean found; // true if element found, else false
protected DLLNode<T> location; // node containing element, if found
protected DLLNode<T> previous; // node preceeding location

protected DLLNode<T> list; // first node on the list
protected DLLNode<T> last; // last node on the list
public RefUnsortedList()
{
numElements = 0;
list = null;
last = null;
currentPos = null;
currentBackPos = null;
}

public void add(T element)
// Adds element to this list.
{
DLLNode<T> newNode = new DLLNode<T>(element);

if(list==null)
{
list = newNode;
last = list;
}
else
{
list.setPrevLink(newNode);
newNode.setNextLink(list);
list = newNode;
}
numElements++;
}

protected void find(T target)
// Searches list for an occurence of an element e such that
// e.equals(target). If successful, sets instance variables
// found to true, location to node containing e, and previous
// to the node that links to location. If not successful, sets
// found to false.
{
location = list;
found = false;

while (location != null)
{
if (location.getInfo().equals(target)) // if they match
{
found = true;
return;
}
else
{
previous = location;
location = location.getNextLink();
}
}
}

public int size()
// Returns the number of elements on this list.
{
return numElements;
}

public boolean contains (T element)
// Returns true if this list contains an element e such that
// e.equals(element); otherwise, returns false.
{
find(element);
return found;
}

public boolean remove (T element)
// Removes an element e from this list such that e.equals(element)
// and returns true; if no such element exists, returns false.
{
find(element);
if (found)
{
if (list == location)   
list = list.getNextLink(); // remove first node
else
previous.setNextLink(location.getNextLink()); // remove node at location

numElements--;
}
return found;
}

public T get(T element)
// Returns an element e from this list such that e.equals(element);
// if no such element exists, returns null.
{
find(element);
if (found)
return location.getInfo();
else
return null;
}
  
public String toString()
// Returns a nicely formatted string that represents this list.
{
DLLNode<T> currNode = list;
String listString = \"List: \";
while (currNode != null)
{
listString = listString + \" \" + currNode.getInfo() + \" \";
currNode = currNode.getNextLink();
}
return listString;
}

public void reset()
// Initializes current position for an iteration through this list,
// to the first element on this list.
{
currentPos = list;
}

public void resetBack()
// Initializes current position for an iteration through this list,
// to the first element on this list.
{
currentBackPos = last;
}

public T getNext()
// Preconditions: The list is not empty
// The list has been reset
// The list has not been modified since most recent reset
//
// Returns the element at the current position on this list.
// If the current position is the last element, then it advances the value
// of the current position to the first element; otherwise, it advances
// the value of the current position to the next element.
{
T next = currentPos.getInfo();
if (currentPos.getNextLink() == null)
currentPos = list;
else
currentPos = currentPos.getNextLink();
return next;
}
public T getPrevious()
// Preconditions: The list is not empty
// The list has been reset
// The list has not been modified since most recent reset
//
// Returns the element at the current position on this list.
// If the current position is the first element, then it advances the value
// of the current position to the last element; otherwise, it advances
// the value of the current position to the next element.
{
T prev = currentBackPos.getInfo();
if (currentBackPos.getPrevLink() == list)
currentBackPos = last;
else
currentBackPos = currentBackPos.getPrevLink();
return prev;
}
}

Driver.java


public class Driver {
public static void main(String args[])
{
RefUnsortedList<Integer> list= new RefUnsortedList<Integer>();
for(int i=1;i<10;i++)
list.add(i);
System.out.println(list.toString());
list.reset();
list.resetBack();
System.out.println(\"\ Testing the getNext() function\");
System.out.print(\" \"+list.getNext().intValue());
System.out.print(\" \"+list.getNext().intValue());
System.out.print(\" \"+list.getNext().intValue());
System.out.println(\"\ Testing the getPrevious() function\");
System.out.print(\" \"+list.getPrevious().intValue());
System.out.print(\" \"+list.getPrevious().intValue());
System.out.print(\" \"+list.getPrevious().intValue());

}
}

OUTPUT:

List: 9 8 7 6 5 4 3 2 1

Testing the getNext() function
9 8 7
Testing the getPrevious() function
1 2 3

Design, implement, test(In Java ) a doubly linked list ADT, using DLLNode objects as the nodes. In addition to our standard list operations, your class should p
Design, implement, test(In Java ) a doubly linked list ADT, using DLLNode objects as the nodes. In addition to our standard list operations, your class should p
Design, implement, test(In Java ) a doubly linked list ADT, using DLLNode objects as the nodes. In addition to our standard list operations, your class should p
Design, implement, test(In Java ) a doubly linked list ADT, using DLLNode objects as the nodes. In addition to our standard list operations, your class should p
Design, implement, test(In Java ) a doubly linked list ADT, using DLLNode objects as the nodes. In addition to our standard list operations, your class should p
Design, implement, test(In Java ) a doubly linked list ADT, using DLLNode objects as the nodes. In addition to our standard list operations, your class should p
Design, implement, test(In Java ) a doubly linked list ADT, using DLLNode objects as the nodes. In addition to our standard list operations, your class should p
Design, implement, test(In Java ) a doubly linked list ADT, using DLLNode objects as the nodes. In addition to our standard list operations, your class should p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site