deque - Dequeue implementation error in Java -
i tried implement dequeue.
below part of code. used print() function in order print nodes in dequeue, seems nodes not connected @ all.
the addlast() function tries new node @ connect dequeue.
public class test<item>{ private node first, last; private int n; private class node{ item value; node next; } public void addlast(item item){ node oldlast = last; node last = new node(); last.value = item; last.next = oldlast; n++; } public void print(){ node temp = last; while(temp != null){ system.out.println(temp.value); temp = temp.next; } } public static void main(string[] args){ test<string> deque = new test<string>(); deque.addlast("hello"); deque.addlast("first"); deque.addlast("second"); deque.addlast("third"); } }
in addlast()
method:
instead of node last = new node();
should write:
last = new node();
Comments
Post a Comment