I am making a simple queue implementation with nodes that have instance methods setNext, getNext. The class I am writing them has fields Node First, and Node Last. My enqueue method must have 3 cases
(1) Add to end of list (normal queue implementation)
this.back.setNext(node to add);
this.back = (node to add);
(2) Add to empty list
this.front = this.back = (node to add);
(3) Add to front of list (and thus move all other elements back)
It seems really simple to me but I am having a tough time sorting out how to get all of the next values to match and not overwrite any nodes. Please let me know of an algorithm that could do this. Thanks!
Update: one implementation I have tried is using an array:
Node[] x = new Node[this.totalNodes+1];
Node current = this.front;
int i = 0;
while(current != null) {
x[i] = current;
current = current.getNext();
i++;
}
int j = 0;
Node current2 = this.front;
this.front = (node to add)
while(j < x.length-1) {
current2.setNext(x[j+1]);
current2 = current2.getNext();
j++;
}
(node to add).setNext(this.front)? - Jaybird