Beruflich Dokumente
Kultur Dokumente
What is a queue?
It is an ordered group of homogeneous items of
elements.
Queues have two ends:
Elements are added at one end REAR
Elements are removed from the other endFRONT
The element added first is also removed first
(FIFO: First In, First Out).
Insertions are at the rear of the queue and removals are at the front
of the queue
Main queue operations:
enqueue(object): inserts an element at the end of the queue
object dequeue(): removes and returns the element at the front of the
queue
Queue Specification
Definitions: (provided by the user)
MAX_ITEMS: Max number of items that might be on
the queue
ItemType: Data type of the items on the queue
Operations
MakeEmpty
Boolean IsEmpty
Boolean IsFull
Enqueue (ItemType newItem)
Dequeue (ItemType& item)
Enqueue (ItemType newItem)
Function: Adds newItem to the rear of the
queue.
Preconditions: Queue has been initialized and is
not full.
Postconditions: newItem is at rear of queue.
Dequeue (ItemType& item)
Function: Removes front item from queue and
returns it in item.
Preconditions: Queue has been initialized and is
not empty.
Post-conditions: Front element has been
removed from queue and item is a copy of
removed element.
Implementation issues
Implement the queue as a circular structure.
How do we know if a queue is full or empty?
Initialization of front and rear.
Testing for a full or empty queue.
Auxiliary queue operations:
object front(): returns the element at the front without removing
it
integer size(): returns the number of elements stored
boolean isEmpty(): indicates whether no elements are stored
Example
Operation Output Q
enqueue(5) (5)
enqueue(3) (5, 3)
dequeue() 5 (3)
enqueue(7) (3, 7)
dequeue() 3 (7)
front() 7 (7)
dequeue() 7 ()
dequeue() error ()
isEmpty() true ()
enqueue(9) (9)
enqueue(7) (9, 7)
size() 2 (9, 7)
enqueue(3) (9, 7, 3)
enqueue(5) (9, 7, 3, 5)
dequeue() 9 (7, 3, 5)
Applications of Queues
Direct applications
Waiting lists, bureaucracy
Access to shared resources (e.g., printer)
Multiprogramming
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
Representation of Queues
Represented in two ways:
One-way list
or Linear Arrays
Two pointers variables: FRONT, containing
the location of the front element of the queue
and REAR, containing the location of the rear
element of the queue.
FRONT=NULL means queue is empty.
Whenever an element is deleted from the queue,
the value of FRONT is increased by 1; this can be
implemented by the assignment
FRONT:=FRONT+1
Whenever an element is added to the queue, the
value of REAR is increased by 1; this can be
implemented by the assignment:
REAR:=REAR+1
Circular Queue
When a new item is inserted at the rear, the
pointer to rear moves upwards.
Similarly, when an item is deleted from the
queue the front arrow moves downwards.
After a few insert and delete operations the rear
might reach the end of the queue and no more
items can be inserted although the items from
the front of the queue have been deleted and
there is space in the queue.
Circular Queue
To solve this problem, queues implement
wrapping around. Such queues are called
Circular Queues.
Both the front and the rear pointers wrap around
to the beginning of the array.
It is also called as Ring buffer.
Items can inserted and deleted from a queue in
O(1) time.
Generally it is assumed that array QUEUE is
circular, i.e. QUEUE[1] comes after QUEUE[N].