Same as the earlier posts Here's the implementation.
Node Class
The implementation:
Have fun.
Node Class
1 2 3 4 5 6 7 8 9 10 11 12 | public class NodeClass { public int info; public NodeClass next; public NodeClass(int i){ this(i,null); } public NodeClass(int x, NodeClass n){ info = x; next = n; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public class QueueWithLL { protected NodeClass head, tail; public QueueWithLL() { head = tail = null; } public boolean isEmpty(){ return(head == null && tail == null); } public void add(int x){ if(isEmpty()){ head = tail = new NodeClass(x); }else{ tail.next = new NodeClass(x, null); tail = tail.next; } } public int peekFront(){ return head.info; } public int peekRear(){ return tail.info; } public int pop(){ int temp = head.info; head = head.next; return temp; } public static void main(String[] args) { QueueWithLL queue = new QueueWithLL(); queue.add(34); queue.add(45); queue.add(78); queue.add(100); System.out.println("Head peek :"+queue.peekFront()); System.out.println("Rear peek :"+queue.peekRear()); System.out.println("Pop the head element :"+queue.pop()); System.out.println("Pop the next head element :"+queue.pop()); } } |