// Linked queue implementationclassLQueueimplementsQueue{privateLinkfront;// Pointer to front queue nodeprivateLinkrear;// Pointer to rear queue nodeprivateintsize;// Number of elements in queue// ConstructorsLQueue(){init();}LQueue(intsize){init();}// Ignore size// Initialize queuevoidinit(){front=rear=newLink(null);size=0;}// Put element on rearpublicbooleanenqueue(Objectit){rear.setNext(newLink(it,null));rear=rear.next();size++;returntrue;}// Remove and return element from frontpublicObjectdequeue(){if(size==0)returnnull;Objectit=front.next().element();// Store the valuefront.setNext(front.next().next());// Advance frontif(front.next()==null)rear=front;// Last elementsize--;returnit;// Return element}// Return front elementpublicObjectfrontValue(){if(size==0)returnnull;returnfront.next().element();}// Return queue sizepublicintlength(){returnsize;}// Check if the queue is emptypublicbooleanisEmpty(){returnsize==0;}}
// Linked queue implementationclassLQueue<E>implementsQueue<E>{privateLink<E>front;// Pointer to front queue nodeprivateLink<E>rear;// Pointer to rear queue nodeprivateintsize;// Number of elements in queue// ConstructorsLQueue(){init();}LQueue(intsize){init();}// Ignore size// Initialize queuevoidinit(){front=rear=newLink<E>(null);size=0;}// Put element on rearpublicbooleanenqueue(Eit){rear.setNext(newLink<E>(it,null));rear=rear.next();size++;returntrue;}// Remove and return element from frontpublicEdequeue(){if(size==0){returnnull;}Eit=front.next().element();// Store the valuefront.setNext(front.next().next());// Advance frontif(front.next()==null){rear=front;}// Last elementsize--;returnit;// Return element}// Return front elementpublicEfrontValue(){if(size==0){returnnull;}returnfront.next().element();}// Return queue sizepublicintlength(){returnsize;}//Tell if the queue is empty or notpublicbooleanisEmpty(){returnsize==0;}}
H0 | Class: class LQueue implements Queue H1 | Code BlockH1 | Method: LQueue() H2 | Code BlockH1 | Method: LQueue(int size) H2 | Code BlockH1 | Method: void init() H2 | Code BlockH1 | Method: public boolean enqueue(Object it) H2 | Code BlockH1 | Method: public Object dequeue() H2 | If: if (size == 0) return null; Object it = front.next().element(); // Store the value front.setNext(front.next().next()); // Advance front if (front.next() == null) rear = front; // Last element size--; return it; // Return element } // Return front element public Object frontValue() H3 | If: if (size == 0) return null; return front.next().element(); } // Return queue size public int length() H4 | Code BlockH3 | Method: public boolean isEmpty() H4 | Code Block
5.13.3. Comparison of Array-Based and Linked Queues¶
All member functions for both the array-based and linked queue
implementations require constant time.
The space comparison issues are the same as for the equivalent stack
implementations.
Unlike the array-based stack implementation, there is no convenient
way to store two queues in the same array,
unless items are always transferred directly from one queue to the other.