Back

Queue Pseudocode

Queues can be implemented using an array data structure, with each element of the array representing an item in the queue. New items are placed in the queue using enqueue and items are removed from the front using dequeue.

Q is the queue being operated on and x is the element to add to the queue. Variables are used to hold the pointers head and tail and to hold the queue’s size.

  • Enqueue
  •         Enqueue(Q,x) 
            IF tail > size THEN  
              Queue is full 
            ELSE  
            BEGIN 
              Q[tail] = x 
              tail++ 
            END 
             
  • Dequeue
  •       Dequeue(Q) 
          IF head == tail THEN       
            Queue is empty 
          ELSE 
          BEGIN       
            x = Q[head] 
            head++
            return x 
          END