You are on page 1of 26

Queue

• A linear list in which data can only be


inserted at one end (the rear), and deleted
from the other end (the front)
• These restrictions ensure that data is
processed through queue in the order in
which they are received.
• It is a 1st in-1st out (FIFO) data structure.

1
Queue Concept

2
Queue Operations
• Four basic operations
– Enqueue: Insertion of an element at rear of
queue
– Dequeue: deletion of an element at the front of
queue
– Queue front: examines the element at front of
queue
– Queue rear: examines element at rear of queue

3
Enqueue
• After insertion, the new element becomes
the rear, if no more space, queue in
overflow state

4
Dequeue
• Returns the element at front of queue to
user and removes it from queue. If no
element to dequeue, queue in underflow
state.

5
Queue Front
• Returns element at front of queue without
changing contents of queue.

6
Queue Rear
• Returns element at rear of queue if not
empty

7
Queue Example

8
Queue Linked List Design
• Use two types of data structures: head and a
data node for link list implementation
• Hence a queue will have one head and zero
or more data nodes

9
Queue Implementations

10
Queue Head & Data Node
Structures
• Head has 2 pointers and a count. Could
have more fields if required depending on
application
• Nodes in a queue are stored in a heap and
are inserted and deleted as requested by
using program.

11
Queue Data Structure

12
Queue Algorithms
• 4 basic operations:
Create queue
Enqueue
Dequeue
Retrieving Queue Data

13
Enqueue
• More complex than stack. 3 conditions to
consider:
1. Insertion into empty queue (count is 0 or rear null)
2. Insertion into queue with data (check for
overflow, returns true if successful)
3. Insertion into queue when no memory in
heap( check for overflow, returns false if no
memory => can’t insert)

14
Enqueue Example

15
Dequeue
• Tests if queue has data, if empty underflow
state, else delete and recycle node
• See alg. 5-3

16
Dequeue Example

17
Retrieving Queue Data
• Two possibilities depending on which
pointer is used: front or rear.
Queue front
dataOut = queue.front->data
Queue rear
dataOut = queue.rear->data

18
Other Algorithms
• Include
Empty Queue
Full Queue
Queue Count
Destroy Queue
• See text
• Refer to text for example on Queue appl.

19
Queue ADT – Array
Implementation
• Arrays can also be used for very large
queues, in files.
• Enqueue
• To an empty queue, element placed in 1st location of
array becoming front and rear
• Subsequent enqueues placed current rear location
• Dequeue
• Takes place at front of queue. Deletion of element
advances queue front / index by 1.

20
Array Implementation
• Example below shows a queue after being
in operation for a while

21
Array Implementation
• A queue is said to be full if queue count is equal to max.
array size.
• Happens when data arrive faster than the queue service
time resulting in last location occupied, but with empty
locations at the front

22
Array Queue with Last Element
filled
• How then can we enqueue new elements?
1. Shift all elements from end to beginning of
the array. With reference to above slide
element[5] moved to index 0, with eventually
last element[16] shifted to index 11
2. Use of circular queue in which last element is
logically followed by 1st element, hence we
search for index 0 during enqueue.

23
A Circular Queue

24
Array Queues Data Structure
• Requires 2 changes from linked list version:
the address of queue array, and max.
number of elements in array

25
Array Implementation
• Refer to text for the C++ code on all the
implementations.

26

You might also like