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 extends LLNode
{
private DLLNode back;
public DLLNode(T info)
{
super(info);
back = null;
}
public void setBack(DLLNode back)
// Sets back link of this DLLNode.
{
this.back = back;
}
public DLLNode getBack()
// Returns back link of this DLLNode.
{
return back;
}
}
LLNODE.java
public class LLNode
{
private LLNode 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 link)
// Sets link of this LLNode.
{
this.link = link;
}
public LLNode 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 implements ListInterface
{
protected int numElements; // number of elements in this list
protected LLNode currentPos; // current position for iteration
// set by find method
protected boolean found; // true if element found, else false
protected LLNode location; // node containing element, if found
protected LLNode previous; // node preceeding location
protected LLNode 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 newNode = new LLNode(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 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
{
private DLLNode 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 nextLink)
// Sets the next link of this DLLNode.
{
this.nextLink = nextLink;
}
public DLLNode getNextLink()
// Returns next link of this DLLNode.
{
return nextLink;
}
public void setPrevLink(DLLNode prevLink)
// Sets the next link of this DLLNode.
{
this.prevLink = prevLink;
}
public DLLNode getPrevLink()
// Returns previous link of this DLLNode.
{
return prevLink;
}
}
RefUnsortedList.java
public class RefUnsortedList //implements ListInterface
{
protected int numElements; // number of elements in this list
protected DLLNode currentPos; // current position for iteration
protected DLLNode currentBackPos; //current back positiion
// set by find method
protected boolean found; // true if element found, else false
protected DLLNode location; // node containing element, if found
protected DLLNode previous; // node preceeding location
protected DLLNode list; // first node on the list
protected DLLNode 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 newNode = new DLLNode(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 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 list= new RefUnsortedList();
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 DL.pdf

  • 1.
    Design, implement, test(InJava ) 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 extends LLNode { private DLLNode back; public DLLNode(T info) { super(info); back = null; } public void setBack(DLLNode back) // Sets back link of this DLLNode. { this.back = back; } public DLLNode getBack() // Returns back link of this DLLNode. { return back; } } LLNODE.java public class LLNode { private LLNode link; private T info;
  • 2.
    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 link) // Sets link of this LLNode. { this.link = link; } public LLNode 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 implements ListInterface
  • 3.
    { protected int numElements;// number of elements in this list protected LLNode currentPos; // current position for iteration // set by find method protected boolean found; // true if element found, else false protected LLNode location; // node containing element, if found protected LLNode previous; // node preceeding location protected LLNode 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 newNode = new LLNode(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;
  • 4.
    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; }
  • 5.
    public T get(Telement) // 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 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
  • 6.
    // of thecurrent 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 { private DLLNode 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;
  • 7.
    } public void setNextLink(DLLNodenextLink) // Sets the next link of this DLLNode. { this.nextLink = nextLink; } public DLLNode getNextLink() // Returns next link of this DLLNode. { return nextLink; } public void setPrevLink(DLLNode prevLink) // Sets the next link of this DLLNode. { this.prevLink = prevLink; } public DLLNode getPrevLink() // Returns previous link of this DLLNode. { return prevLink; } } RefUnsortedList.java public class RefUnsortedList //implements ListInterface { protected int numElements; // number of elements in this list protected DLLNode currentPos; // current position for iteration protected DLLNode currentBackPos; //current back positiion // set by find method protected boolean found; // true if element found, else false protected DLLNode location; // node containing element, if found protected DLLNode previous; // node preceeding location protected DLLNode list; // first node on the list protected DLLNode last; // last node on the list public RefUnsortedList() {
  • 8.
    numElements = 0; list= null; last = null; currentPos = null; currentBackPos = null; } public void add(T element) // Adds element to this list. { DLLNode newNode = new DLLNode(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 {
  • 9.
    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;
  • 10.
    } public T get(Telement) // 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 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; }
  • 11.
    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
  • 12.
    public class Driver{ public static void main(String args[]) { RefUnsortedList list= new RefUnsortedList(); 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