Hello,
I'm new to this forum so I apologize for any mistakes I'm going to make, I'm still learning the ropes. I am writing a token ring simulation program and am using a circular doubly linked list in the process. I seem to be having issues with the add and remove methods however.
My TokenRingList class:
[highlight = java]
import support.DLLNode;
/**
* A specialized circular doubly linked list for a token ring simulation.
*/
public class TokenRingList implements TokenRingListInterface {
// The start of the token ring list:
private DLLNode<Workstation> list;
// The location used to search this list:
private DLLNode<Workstation> location;
// The size of this list:
private int size;
// clockwise/counterclockwise - true if clockwise, false if counterclockwise:
private boolean clockwise = true;
/**
* Creates a new TokenRingList object.
*/
public TokenRingList() {
list = null;
location = null;
}
/**
* Returns the size of the token ring.
*/
public int size() {
return size;
}
/**
* Adds a Workstation object to this token ring.
*/
public void add(Workstation element) {
DLLNode<Workstation> newNode = new DLLNode<Workstation>(element);
if(list==null)
{
list = newNode;
newNode.setForward(list);
}
else
{
newNode.setBack(location.getBack());
newNode.setForward(location);
location.getBack().setForward(newNode);
location.setBack(newNode);
}
}
/**
* Removes a Workstation object from this token ring.
*/
public boolean remove(Workstation element) {
DLLNode<Workstation> newNode = new DLLNode<Workstation>(element);
boolean test = false;
if (contains(element))
{
newNode.getBack().setForward(newNode.getForward()) ;
newNode.getForward().setBack(newNode.getBack());
test = true;
}
else
test = false;
size--;
return test;
}
/**
* Returns true if this token ring has the workstation; false otherwise.
*/
public boolean contains(Workstation element) {
// Is the list empty?
if (list == null)
return false;
// Find the element if it exists:
DLLNode<Workstation> t = list;
do {
if (element == t.getInfo())
return true;
t = t.getForward();
} while (t != list);
// Not found:
return false;
}
/**
* Returns the workstation if it is in this token ring; null otherwise.
*/
public Workstation get(Workstation element) {
// Is the list empty?
if (list == null)
return null;
// Find the element if it exists:
DLLNode<Workstation> t = list;
do {
if (element == t.getInfo())
return element;
t = t.getForward();
} while (t != list);
// Not found:
return null;
}
public void reset() {
location = list;
}
/**
* Returns the next workstation in the token ring.
*/
public Workstation getNext() {
if (clockwise) {
return getClockwise();
}
else {
return getCounterClockwise();
}
}
/**
* Returns the current workstation and advances location clockwise.
*/
private Workstation getClockwise() {
DLLNode<Workstation> t = location;
location = location.getForward();
return t.getInfo();
}
/**
* Returns the current workstation and advances location counterclockwise.
*/
private Workstation getCounterClockwise() {
DLLNode<Workstation> t = location;
location = location.getBack();
return t.getInfo();
}
/**
* Sets the token ring to retrieve workstations in a clockwise fashion.
*/
public void setClockwise() {
clockwise = true;
}
/**
* Sets the token ring to retrieve workstations in a counterclockwise fashion.
*/
public void setCounterClockwise() {
clockwise = false;
}
}
[/highlight]
this is the DLLNode class I'm using
[highlight = java]
public class DLLNode<T> {
private DLLNode<T> back;
private DLLNode<T> forward;
private T info;
public DLLNode(T info) {
this.info = info;
this.back = null;
this.forward = 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 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;
}
public void setForward(DLLNode<T> forward)
// Sets forward link of this DLLNode;
{
this.forward = forward;
}
public DLLNode<T> getForward()
// Returns forward link of this DLLNode.
{
return forward;
}
}
[/highlight]
Any advice or pointers would be great. Thank you.