I have an interesting situation: When I add a node into a queue, only two nodes added.
Here's how I add nodes into a queue:
Queue queue = new Queue();
Node a = new Node("1");
Node b = new Node("2");
Node c = new Node("3");
Node d = new Node("4");
queue.add(a);
queue.add(b);
queue.add(c);
queue.add(d);
Now, here's the add method:
public void add(Node newNode)
{
// assigning first Node
if (head == null){
head = newNode;
return;
}
// since first Node assigned, assigning second one
if (head.next == null){
head.next = newNode;
}
}
It prints:
1
2
I want to print all of them, but only first two are printed. Also, it is like stack, but FIFO, not LIFO.
Here's the print if it helps:
public void print()
{
Node p;
// Display all the nodes in the stack
for( p = head; p != null; p = p.next )
p.print();
}
Please let me know if additional info needed. Thanks!
// since first Node assigned, assigning second one. Does that ever change after the 2nd node added?