Data Structures Using C
Queue
Shikha Sharma
Topics Covered
2
Topics
• The main topics covered include:
• Queue
• Circular queue
• Deque
• Priority Queue
• Operations on Queue
3
Objectives
4
Objectives
• After studying this module, you will be able to:
• Explain queues and its representations
• Discuss different types of queues
• Explain operations that can be performed on queues
5
Queue Data Structure
6
Introduction
• Queue is a linear data structure.
• First in First out (FIFO) list.
• The element can be added into the rear of the queue and can be
deleted from the front of the queue.
• Add elements into the queue - insert
• Remove or delete elements from the queue – delete
Representation of Queue
8
Representation of Queue
• Queue can be implemented in two ways:
• Using arrays (static implementation)
• Using pointers (dynamic implementation)
Algorithm for pushing an element into the queue
NORMQUEINS(FRONT,REAR,ITEM)
• Step-1: If (REAR == N-1) Then Print: “Overflow “ & Exit
• Step-2: Else-If (FRONT and REAR == -1) Then
(a) Set FRONT = 0
(b) Set REAR = 0
• Step-3: Else Set REAR = REAR + 1
• Step-4: Set QUEUE[REAR] = ITEM
• Step-5: Exit
Algorithm for popping an element from the queue
NORMQUEDEL(FRONT,REAR,ITEM)
• Step-1: If (FRONT == -1) Then Print: Underflow Exit
• Step-2: Else ITEM = QUEUE[FRONT]
• Step-3: If (FRONT == REAR) Then
(a) Set FRONT = -1
(b) Set REAR = -1
• Step-4: Else Set FRONT = FRONT + 1
• Step-5: Exit
Disadvantage of Queue
• Improper memory use
• Example:
Types of queues and Operations on Queues
Introduction
• By making some variations in the simple queue, three types of queues can
be formed:
• Circular queue
• Double ended queue (de-queue)
• Priority queue
Circular Queue
• The elements will be stored in a circular manner.
• Let Queue[0], Queue[1] … to Queue[n-1] be the elements.
• In a circular queue, Queue[n-1] will be joined to Queue[0].
• New elements can be inserted in front of queue, if free.
Example of Circular queue
Algorithm for inserting element into circular queue
CIRQUEINS(FRONT,REAR,ITEM,N)
• Step-1: If FRONT ==0AND REAR == N -1OR FRONT==REAR+1
Then Print: “Overflow “ & Exit
• Step-2: If (FRONT and REAR == -1) Then
(a) Set FRONT = 0
(b) Set REAR = 0
• Else –IF REAR ==N-1THEN SET REAR=0
• Else Set REAR = REAR + 1
• Step-3: Set QUEUE[REAR] = ITEM
• Step-4: Exit
Algorithm for deleting element from circular queue
CIRQUEDEL(FRONT,REAR,ITEM,N)
• Step-1: if (FRONT is equal to -1)
• Display “Queue is empty” or “UnderFlow”
• Exit
• Step-2: Set ITEM = Queue[FRONT]
• Step-3: if (REAR is equal to FRONT)
• SET : FRONT = -1
• SET : REAR = -1
• Else-IF FRONT==N-1 THEN Set FRONT=0
Else Set : FRONT = FRONT +1
• Step-4: Exit
Deque
• In the deque, you can insert and delete data elements from both sides of
the queue.
• It is called double ended queue
Types of Deque
• Depending on the restriction of insertion and deletion into this deque,
there can be two variations for deque:
• Input restricted deque
Insert data elements only at one end of the deque.
• Output restricted deque
Delete data elements only at one end of the deque.
Algorithm for inserting the data element at right side
of a deque
• Input the data_element to be inserted
• If ((left == 0 && right == Size-1) || (left== right+ 1))
• Display “Queue Overflowing”
• Exit
• If (left==right==NULL)
• left= 0
• right = 0
• Else
• if (right == Size-1 )
Set- right= 0
• else
Set- right = right + 1
• DEQUE[right] = data_element
• Exit
Algorithm for inserting the data element at left side of
a deque
• Input the data_element to be inserted
• If ((left == 0 && right == Size-1) || (left== right+ 1))
• Display “Queue Overflow”
• Exit
• If (left==right==NULL)
• left= 0
• right =0
• Else
• if (left == 0)
Set- left = Size -1
• Else
Set- left =left – 1
• DEQUE[left] = data_element
• Exit
Algorithm for deleting the data element at right side
of a deque
• If (left==right==NULL)
• Display “Queue Underflow”
• Exit
• Data_element = DEQUE [right]
• If (left == right)
• left = -1
• right = -1
• Else
• if(right== 0)
Set- right = Size-1
• Else
Set- right = right-1
• Exit
Algorithm for deleting the data element at left side of
a deque
• If (left==right==NULL)
• Display “Queue Underflow”
• Exit
• data_element = DEQUE [front]
• If(left == right)
• left = -1
• right = -1
• Else
• if (left == Size-1)
Set left = 0
• Else
left = left +1
• Exit
Priority Queue
• A priority queue is like any other queue with a priority associated with
each element stored in it.
• A priority queue will have the following operations:
• Insert
• Delete
Applications of Priority Queue
• Bandwidth management
• Dijkstra’s algorithm
• A* shortest path search
Applications of Queue
• Round robin scheduling techniques
• Printing services
• Customer service software
Summary
2
Summary
• Queue is a simple data structure which is used extensively.
• They can be implemented both by using arrays and pointers.
• It is possible to insert and delete data elements from queue.
• A few variations of queue are also available - circular queue, priority queue
and deque