0% found this document useful (0 votes)
8 views11 pages

DS - UNIT 2 Queue

Uploaded by

Akash Gaonkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

DS - UNIT 2 Queue

Uploaded by

Akash Gaonkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT 2 Data Structures Using C 1

QUEUE
Queue is defined as an ordered collection of items from which, the items may be deleted at one
end called FRONT end and the items may be inserted at the other end called REAR end.
Queue works on FIFO (First In First Out) fashion.
Classification of Queue

Queue

Circular Priority Double Ended


Linear Queue
Queue Queue Queue

Linear Queue
It is also known as sequential queue. It is implemented using linear array or linear list. This
linear queue is represented using linear array with 2 variables FRONT & REAR.

0 1 2 3 4 5 6 7
26 9 23 34 45 67 45 34

FRONT REAR
Deletion Insertion
Queue ADT
A collection of queue data members and queue operations that preform on queue data. The data
members or variables are FRONT and REAR. Queue operations such as
1. Create()- Create queue with n elements.
2. Insert()- insert an element at the end of the queue
3. Delete()- delete an element from the beginning of the queue.
4. Isempty()- returns true if the queue is empty otherwise false
5. Traverse()- accessing all the elements in the queue.
6. Count()- count the number of elements in the queue.
Insertion()
This operation inserts an element on the end of a queue. If queue is full, insertion is not possible.
Algorithm: Qinsert(Q, FRONT, REAR, N, item)
Step 1: check overflow
if REAR= = N-1 then
print “Overflow”
return
Step 2: Increment REAR pointer

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 2

REAR= REAR+1
Step 3: Insert the item
Q[REAR]= item
Step 4: Return
Explanation:
Consider a queue contain 5 elements and Max is 8

0 1 2 3 4 5 6 7
26 9 23 34 45

FRONT REAR
1. Check queue overflow condition if it is print overflow. Here, REAR=4 so it is false.
2. Increment REAR=REAR + 1

0 1 2 3 4 5 6 7
26 9 23 34 45

FRONT REAR
3. Insert the item
Q[REAR]= item i.e., Q[5]=86

0 1 2 3 4 5 6 7
26 9 23 34 45 86

FRONT REAR
4. Return
The following C function illustrates Queue insertion
void Qinsert()
{
if(REAR == N-1)
printf("\n Queue Overflow");
else
{
printf("\n Enter an item:");
scanf("%d", &item);
REAR++;
QUEUE[REAR]= item;
}

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 3

}
Deletion
This operation deletes an item from the beginning of the queue. If Queue is Empty, deletion
operation is impossible.
Algorithm: Qdelete(Q, FRONT, REAR, N, item)
Step 1: Check underflow
if REAR= = FRONT-1 then
print "Underflow"
return
Step 2: Delete the item
item = Q[FRONT]
Step 3: if FRONT= = REAR
FRONT=0, REAR=-1
else
FRONT=FRONT+1
`Step 4: return

Explanation
Consider a queue contain 5 elements and Max is 8
0 1 2 3 4 5 6 7
26 9 23 34 45

FRONT REAR
1. Check queue is underflow. Here, FRONT=0 and REAR=4, so it is false
2. Delete the item. i.e., item= Q [0]⟹item =26
0 1 2 3 4 5 6 7
9 23 34 45

FRONT REAR
3. Check FRONT and REAR value is equal. Here FRONT=0 and REAR =4. So, it is
false, increment FRONT=FRONT+1
0 1 2 3 4 5 6 7
9 23 34 45

FRONT REAR
4. Return
The following C function illustrates Queue deletion
void Qdelete()
{
if (REAR= = FRONT - 1)

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 4

printf("\nQueue Underflow");
else if(REAR= = FRONT)
{
printf("This is the last element in the Queue");
printf("\n The Last element deleted is : %d", QUEUE[FRONT]);
FRONT=0;
REAR=-1;
}
else
{
printf("\n Deleted item is %d", QUEUE[FRONT]);
FRONT++;
}
}
Qempty Operation
This operation returns true if the queue is empty otherwise false.
A Queue is empty only when REAR=-1 and FRONT=0
The following function check Queue is empty
int Qempty()
{
if(REAR= = FRONT-1)
return 1;
else
return 0;
}
Qfull Operation
This operation returns true, if the queue is full otherwise false. A Queue is full only when
REAR== N-1. The following function check Queue is full.
int Qfull()
{
if(REAR= = N-1)
return 1;
else
return 0;
}
Traversal operation
This operation access all the elements in the queue.
Algorithm: Qdisplay(Q, FRONT, REAR, N)
Step 1: Check underflow
if REAR== FRONT-1 then
print "Underflow"
return
Step 2: Repeatedly access the queue elements
for (i=FRONT; i<= REAR; i++)

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 5

print Q[i];
Step 3: return
Explanation
Consider the following Queue Max Size is 8 and it contain 5 elements.

0 1 2 3 4 5 6 7
26 9 23 34 45

FRONT REAR
1. Check underflow, here REAR=4, FRONT=0 so it is false therefore Go to step 2
2. Repeatedly print the value of Queue 26 9 23 34 45
3. Return
Linked Representation of Queues
 In linked list implementation, every queue element has two parts, one that stores the
data and the other that stores the address of the next element.
 The FRONT pointer points the first element and the REAR pointer points the last
element.
 All insertions will be done at the REAR end and all insertions at the FRONT end
 FRONT = REAR = NULL indicates the queue is empty

Insert Operation
The insert operation is used to insert an element into a queue. The new element is added as
the last element of the queue.
Algorithm: QINSERT()
Step 1: Allocate memory for the new node and name it as NEW_NODE
Step 2: Set NEW_NODE → INFO=item
Step 3: If FRONT = NULL
Set FRONT=REAR=NEW_NODE
Set FRONT→ LINK = REAR→ LINK =NULL
Else
Set REAR→> LINK=NEW_NODE
Set REAR = NEW_NODE
Set REAR→NEXT=NULL
End if
Step 4: END
Consider the following figure shows linked queue.
FRONT REAR

2025 2010 215


50 10 90 NULL

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 6

To insert an element with value 80, we first allocate memory for a new node, store the value
80 in its INFO part and NULL in its LINK part. We add the new node at the end of the linked
stack. The updated linked queue is shown below:
FRONT REAR

2025 2010 215 1040


50 10 90 80 NULL

Delete Operation
The delete operation is used to delete the FRONT element from the queue.
Algorithm QDELETE()
Step 1: If FRONT = NULL
Print "Underflow"
goto Step 5
Step 2: Set PTR = FRONT
Step 3: Set FRONT = FRONT → LINK
Step 4: Free PTR
Step 5 End
Consider the linked Queue as shown below:
FRONT REAR

2025 2010 215 1040


50 10 90 80 NULL

Before deleting the values, we first check FRONT = = NULL if so, the queue is empty
otherwise it deletes a value from a queue.
FRONT REAR

2010 215 1040


10 90 80 NULL

CIRCULAR QUEUE
Circular Queue is a linear data structure in which the operations are performed based on FIFO
(First In First Out) principle and the last position is connected back to the first position to make
a circle. It is also called ‘Ring Buffer’. The representation of circular queue is shown below:

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 7

Insert Operation
This operation inserts an element on the REAR position of a queue. If queue is full, insertion
is not possible.
Algorithm CQINSERT(Q,FRONT,REAR,N,ITEM)
Step 1: If FRONT == (REAR+1) %N
display “overflow”
Step 2: IF FRONT == -1
FRONT = REAR = 0
Step 3: ELSE REAR = (REAR + 1)%N
Step 4: Q[REAR] = ITEM
Step 5: END
Delete Operation
This operation deletes an item from the FRONT position of the queue. If Queue is Empty,
deletion operation is impossible.
Algorithm QDELETE (Q, FRONT, REAR, N, ITEM)
Step 1: If FRONT == -1
Write “underflow”
Return
Step 2: ITEM = Q[FRONT]
Step 3: IF FRONT = REAR
FRONT = REAR = -1
ELSE
FRONT = (FRONT + 1)%N
Step 4 : END
The following figure illustrates insertion and deletion of a queue

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 8

Circular Queue Traversal


This operation access all the elements in the circular queue.
Algorithm CQDISPLAY(Q,FRONT,REAR,N)
Step 1: If FRONT <= REAR
Display the array from Q[FRONT] to Q[REAR]
Step 2: IF FRONT > REAR
Display the array from Q[FRONT] to Q[N-1]
Display the array from Q[0] to Q[REAR]

PRIORITY QUEUE
A priority queue is a queue such that element of the queue has been assigned with a
priority such that the order in which elements are processed comes from the following
rules:
o An element of higher priority is processed before any element of lower
priority
o If two elements have same priority, then the element which come first will be
processed first.
Types of priority queue
There are two types of priority queue. They are:

Priority Queue

Ascending Priority Queue Descending Priority Queue

 Ascending priority queue


In ascending priority queue, elements can be inserted in arbitrary order, but only
smallest can be deleted first.
 Descending priority queue
In descending priority queue, elements can be inserted in arbitrary order, but only the
largest element can be deleted first.
The representation of priority queue is shown below:
Suppose an array with elements 10, 5, 30, 20 and 80 in the same order, so, insertion can
be done with the same sequence, but the order of deleting the elements is 80, 30, 20,
10, 5 for descending priority queue and the order for deleting elements is 5, 10, 20, 30,
80 for ascending order

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 9

Implementing a priority queue


The various ways of implementing a priority queue is as follows
• Using a simple linear array
• Using multi-queue implementation
Priority queue using arrays

 It is implemented in two ways


First Method
 Let insertion happen in FIFO like simple queue,
 For deletion, traverse the array for an element of the highest priority and then
delete this element from the queue.
 If this is not the front most element, then shift all its trailing elements to the left
side to fill up the vacant position.
Second Method
 Sort the inserted elements in ascending order
 Now delete the front element like in the simple queue.
Multi Queue Implementation
 The multi-queue implementation of a priority queue is similar to array implementation
except that
 A separate queue for each level of priority has to be maintained
 And each such queue represents a circular array.
 Number of priority levels used = number of queues to be used
 It is represented using a 2D array
 Two linear arrays FRONT[] and REAR[] are to be maintained to store Front
and Rear elements of each priority levels

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 10

DEQUE (or, Double Ended Queue)

In Deque or Double Ended Queue, insertion and deletion can be done from both ends of
the queue either from the front or rear.

Types of DEQUE
The 2 variations of deque is as follows:

Double Ended Queue

Input restricted deque Output restricted deque

 Input restricted deque


In input restricted queue, insertion operation can be performed at only one
end, while deletion can be performed from both ends.

 Output restricted deque


In output restricted queue, deletion operation can be performed at only one
end, while insertion can be performed from both ends.

Operations on Deque:
The basic operations are:
insertFront(): Adds an item at the front of Deque
insertLast(): Adds an item at the rear of Deque
deleteFront(): Deletes an item from front of Deque.
deleteLast(): Deletes an item from rear of Deque.

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 11

Consider an input restricted deque and now we can add element only on the REAR side of the
queue but we can delete from both sides.

Applications of Queues:
 Queues are used as waiting lists for a single shared resource like printer, disk, CPU.
 Queues are used as buffers in most of the applications like MP3 media player, CD
player, etc.
 Queue are used to maintain the play list in media players in order to add and remove
the songs from the play-list.
 Queues are used in operating systems for handling interrupts.

SINDHI COLLEGE BANGALORE - 24

You might also like