Saturday, 25 August 2018

Queue implementation in java


Queue --> Dequeue --> LinkedList

Queue : Only add() from tail and remove() from head (FIFO obviously)







Dequeue: add() and remove() from both the sides methods like addFirst(10),addLast(10),removeFirst(),removeLast().


Notes on LinkedList

1. it maintained all node as 

Node<E> Object attributes{
E item;
Node next;
Node previous;
}

So if you want to implement you own queue/LinkedList,  you should have similars fields

2. remove and add, increase the size field of
Class LinkedList {

int size =0;
Node first/head;
Node last/tail;

}

add(item) {
added to tail, but if head is also null, then head and tail both should point to same.


No comments:

Post a Comment