Java

TOPIC 26 – IMPLEMENTING A STACK & QUEUE

 

 

LESSON NOTE

 

 

QUEUE

 

A queue data structure is simply a structure that stores elements in the same order that they arrive.  The programmer can only add to the end of a queue and can only access/remove from the front of the queue. 

 

It is very much like a lineup at a store.  As people arrive, they lineup and wait.  When the time is right, the person at the front is served and removed from the lineup (queue).

 

FUNCTIONALITY

 

There are three key methods for Queues.  There are:

 

   size – Returns the number of elements in the queue

   enqueue – Add a value to the end of the queue

   dequeue – Remove and get the element

 

STACK

 

A stack data structure is similar to a stack of plates that are piled up.  If you want to add the stack, you must add at the top of it.  If you want to remove from the stack, you must also remove from the top of it.  So, unlike a queue, for stacks, adding and removing occur at the same end.

 

FUNCTIONALITY

 

These are three key methods for Stacks:

 

   size – Returns the number of elements in the stack

   push(value) – Add value to the top of the stack

   pop – Remove and get the value from the top of the stack