0% found this document useful (0 votes)
6 views43 pages

Queues Jsn

Uploaded by

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

Queues Jsn

Uploaded by

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

The Queue

Queues
• Queue: a collection whose elements are
added at one end (the rear or tail of the
queue) and removed from the other end
(the front or head of the queue)
• A queue is a FIFO (first in, first out) data
structure
• Any waiting line is a queue:
• The check-out line at a grocery store
• The cars at a stop light
• An assembly line
Conceptual View of a Queue
Adding an element

Front of queue

New element is
added to the rear
of the queue
Conceptual View of a Queue
Removing an element

New front element of queue

Element is
removed from the
front of the queue
Uses of Queues in Computing

• For any kind of problem involving FIFO


data
• Printer queue (e.g. printer in MC 235)
• Keyboard input buffer
• GUI event queue (click on buttons,
menu items)
Uses of Queues in Computing
• In simulation studies, where the goal is
to reduce waiting times:
• Optimize the flow of traffic at a traffic
light
• Determine number of cashiers to have
on duty at a grocery store at different
times of day
• Other examples?
Queue Operations
• enqueue : add an element to the
tail(rear end) of a queue
• dequeue : remove an element from the
head(front) of a queue
• first : examine the element at the head
of the queue (“peek”)
• Other useful operations (e.g. is the
queue empty)
• It is not legal to access the elements in
the middle of the queue!
Operations on a Queue
Operation Description

dequeue Removes an element from the front of the queue

enqueue Adds an element to the rear of the queue

first Examines the element at the front of the queue

isEmpty Determines whether the queue is empty

size Determines the number of elements in the queue


• Just as stacks can be implemented
as arrays or linked lists, so with
queues.
Queue (Linear Queue)
• It is a linear data structure consisting of list of items.
• In queue, data elements are added at one end, called the rear and removed from another
end, called the front of the list.
• Two basic operations are associated with queue:
7
1. “Insert” operation is used to insert an element into a queue.
2. “Delete” operation is used to delete an element from a queue. 6

FIFO list
• Example: EEE 5
Queue: AAA, BBB, CCC, DDD, EEE
1 2 3 4 5 6 7 DDD 4

AAA BBB CCC DDD EEE CCC 3

BBB 2
Front Rear
AAA 1
Rear Front
Algorithms for Insert and Delete Operations in Linear Queue
For Insert Operation
Insert-Queue(QUEUE, REAR, FRONT,MAX, NUM)
Here, QUEUE is the place where to store data. REAR represents the location in which the
data element is to be inserted and FRONT represents the location from which the data
element is to be removed. Here MAX is the maximum size of the QUEUE and finally,
NUM is the new item to be added.

Step 1: IF REAR = MAX-1


Write OVERFLOW
Goto step 4
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR =0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = NUM
Step 4: EXIT.
For Delete Operation
Delete-Queue(QUEUE, FRONT, REAR, NUM)
Here, QUEUE is the place where data are stored. REAR represents the location in which the
data element is to be inserted and FRONT represents the location from which the data
element is to be removed. Front element is assigned to NUM.
Step 1: IF FRONT = -1 OR FRONT > REAR
Write UNDERFLOW
ELSE
NUM=QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
#include <stdio.h>
#include <conio.h>
#define MAX 10 // Changing this value will change length of array
int queue[MAX];
int front = -1, rear = -1;
void insert(void);
int delete_element(void);
int peek(void);
void display(void);
int main()
{
int option, val;
do
{
printf(“\n\n ***** MAIN MENU *****”);
printf(“\n 1. Insert an element”);
printf(“\n 2. Delete an element”);
printf(“\n 3. Peek”);
printf(“\n 4. Display the queue”);
printf(“\n 5. EXIT”);
printf(“\n Enter your option : “);
scanf(“%d”, &option);
switch(option)
{
case 1:
insert();
break;
case 2:
val = delete_element();
if (val != -1)
printf(“\n The number deleted is : %d”, val);
break;
case 3:
val = peek();
if (val != -1)
printf(“\n The first value in queue is : %d”, val);
break;
case 4:
display();
break;
}
}while(option != 5);
getch();
return 0;
}
void insert()
{
int num;
printf(“\n Enter the number to be inserted in the
queue : “);
scanf(“%d”, &num);
if(rear == MAX-1)
printf(“\n OVERFLOW”);
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}
int delete_element()
{
int val;
if(front == -1 || front>rear)
{
printf(“\n UNDERFLOW”);
return -1;
}
else
{
val = queue[front];
front++;
if(front > rear)
front = rear = -1;
return val;
}
}
int peek()
{
if(front==-1 || front>rear)
{
printf(“\n QUEUE IS EMPTY”);
return -1;
}
else
{
return queue[front];
}
}
void display()
{
int i;
printf(“\n”);
if(front == -1 || front > rear)
printf(“\n QUEUE IS EMPTY”);
else
{
for(i = front;i <= rear;i++)
printf(“\t %d”, queue[i]);
}
}
Example: Consider the following queue (linear queue).
Rear = 4 and Front = 1 and N = 7
10 50 30 40
1 2 3 4 5 6 7
(1) Insert 20. Now Rear = 5 and Front = 1

10 50 30 40 20
1 2 3 4 5 6 7
(2) Delete Front Element. Now Rear = 5 and Front = 2
50 30 40 20
1 2 3 4 5 6 7
(3) Delete Front Element. Now Rear = 5 and Front = 3

30 40 20
1 2 3 4 5 6 7
(4) Insert 60. Now Rear = 6 and Front = 3

30 40 20 60
1 2 3 4 5 6 7
Drawback of Linear Queue
• Once the queue is full, even though few elements from the front are deleted and
some occupied space is relieved, it is not possible to add anymore new elements,
as the rear has already reached the Queue’s rear most position.

Circular Queue
• This queue is not linear but circular.

• Its structure can be like the following figure:


• In circular queue, once the Queue is full the

"First" element of the Queue becomes the

"Rear" most element, if and only if the "Front" Figure: Circular Queue having
Rear = 5 and Front = 0
has moved forward. otherwise it will again be

a "Queue overflow" state.


Algorithms for Insert and Delete Operations in Circular Queue

For Insert Operation


Insert-Circular-Q(CQueue, Rear, Front, N, Item)
Here, CQueue is a circular queue where to store data. Rear represents the location in
which the data element is to be inserted and Front represents the location from which
the data element is to be removed. Here MAX is the maximum size of CQueue and
finally, VALis the new item to be added. Initailly Rear = -1 and Front = -1
Step 1: IF FRONT = and Rear = MAX-1
Write OVERFLOW
Step 2: IF FRONT=-1 and REAR=-1
SET FRONT = REAR = 0
ELSE IF REAR = MAX-1 and FRONT != 0
SET REAR = 0
ELSE
SET REAR = REAR+1
[END OF IF]
Step 3: SET QUEUE[REAR] = VAL
Step 4: EXIT
For Delete Operation
Delete-Circular-Q(CQueue, Front, Rear, Item)
Here, CQueue is the place where data are stored. Rear represents the location in
which the data element is to be inserted and Front represents the location from
which the data element is to be removed. Front element is assigned to Item.
Initially, Front = 1.
Step 1: IF FRONT=-1
Write UNDERFLOW
Goto Step 4 [END of IF]
Step 2: SET VAL = QUEUE[FRONT]
Step 3: IF FRONT = REAR
SET FRONT = REAR=-1
ELSE IF FRONT = MAX -1
SET FRONT = 0
ELSE SET FRONT = FRONT+1
[END of IF]
[END OF IF]
Step 4: EXIT
Example: Consider the following circular queue with N = 5.
1. Initially, Rear = 0, Front = 0. 4. Insert 20, Rear = 3, Front = 0.
Front

Rear

2. Insert 10, Rear = 1, Front = 1. 5. Insert 70, Rear = 4, Front = 1.


Rear Front
Front

Rear
3. Insert 50, Rear = 2, Front = 1. 6. Delete front, Rear = 4, Front = 2.
Front Rear Front

Rear
7. Insert 100, Rear = 5, Front = 2. 10. Delete front, Rear = 1, Front = 3.
Front Rear

Front

Rear

8. Insert 40, Rear = 1, Front = 2. 11. Delete front, Rear = 1, Front = 4.


Rear
Rear Front

Front

9. Insert 140, Rear = 1, Front = 2. 12. Delete front, Rear = 1, Front = 5.


As Front = Rear + 1, so Queue overflow. Rear

Rear
Front

Front
• while inserting an element
rear=(rear+1) mod (size)
• while deleting an element
front=(front+1) mod (size)
• Overflow?
front=(rear+1) mod (size)
• Empty?
front=rear
Double Ended Queue
• A deque (pronounced as ‘deck’ or ‘dequeue’) is a list
in which the elements can be inserted or deleted at
either end.
• It is also known as a head-tail linked list because
elements can be added to or removed from either the
front (head) or the back (tail) end.
• In a deque, two pointers are maintained, LEFT and
RIGHT, which point to either end of the deque.
• It is a circular
• The elements in a deque extend from the LEFT end
to the RIGHT end and since it is circular, Dequeue[N–
1] is followed by Dequeue[0].
Variants of Deque
• Input restricted deque: In this dequeue,
insertions can be done only at one of
the ends, while deletions can be done
from both ends.
• Output restricted deque :In this
dequeue, deletions can be done only at
one of the ends, while insertions can be
done on both ends.
DEQUEUE
INPUT RESTRICTED DEQUEUE
OUTPUT RESTRICTED DEQUEUE
void insert_left()
{
int val;
printf("\n Enter the value to be added:");
scanf("%d", &val);
if((left == 0 && right == MAX–1) || (left == right+1))
{
printf("\n Overflow");
return;
}
if (left == –1)/*If queue is initially empty*/
{
left = 0;
right = 0;
}
else
{
if(left == 0)
left=MAX–1;
else
left=left–1;
}
deque[left] = val;
}
void display()
{
int front = left, rear = right;
if(front == –1)
{
printf("\n QUEUE IS EMPTY"); return;
}
printf("\n The elements of the queue are : ") if(front <= rear )
{
while(front <= rear)
{
printf("%d",deque[front]); front++;
}
}
else
{
while(front <= MAX–1)
{
printf("%d", deque[front]); front++;
}
front = 0;
while(front <= rear)
{
printf("%d",deque[front]);front++;
}
}
printf("\n");
}
Priority Queue
• A priority queue is a data structure in which each
element is assigned a priority. The priority of the
element will be used to determine the order in which
the elements will be processed.
• The general rules of processing the elements of a
priority queue are
• An element with higher priority is processed before an
element with a lower priority.
• Two elements with the same priority are processed on a
first-come-first-served (FCFS) basis.
Array Representation of a Priority
Queue
• When arrays are used to implement a priority queue,
then a separate queue for each priority number is
maintained.
• Each of these queues will be implemented using
circular arrays or circular queues.
• A two-dimensional array for this purpose where each
queue will be allocated the same amount of space.
• Insertion: Insertion To insert a new
element with priority K in the priority
queue, add the element at the rear end
of row K, where K is the row number as
well as the priority number of that
element.
• Deletion: To delete an element, we find
the first nonempty queue and then
process the front element of the first
non-empty queue

You might also like