You are on page 1of 33

Queues

Queues
• A queue is a linear data structure.
• A queue is an ordered list in which all insertion take place at one end, the
rear, whereas all deletions take place at the other end, the front.
• Works on principle first in first out (FIFO).
• It means: the first element inserted is the first one to be removed.
• Adding an item

• Removing an item

Deletion Insertion

front Rear
• Real life examples
– Waiting in line
– Waiting on hold for tech support
• Applications related to Computer Science
– Threads
– Job scheduling (e.g. Round-Robin algorithm for CPU allocation)
Implementation of Queue
• Queue can be implemented based on
–Array (static implementation)
–Linked list (dynamic implementation)

Operations perform on Queue


▪ Create the Queue
▪ Insert the element on Queue
▪ Delete the element from Queue
▪ Check the Queue is empty or full
Array-based Queue Implementation
Empty queue
struct queue
{ -1 0 1 2 3 4
int a[size];
int rear, front; q1.rear q1.front
}q1;

Add(23)
Increment rear by 1 and add 23 at rear end

23
-1 0 1 2 3 4
0 1 2 3 4
q1.rear
q1.rear q1.front
q1.front

Before Execution After Execution


Array-based Queue Implementation
Increment rear by 1 and add ele. at rear end
.
Add(36) Add(64)
23 36 64
23 36
0 1 2 3 4
0 1 2 3 4
q1.rear
q1.rear
q1.front
q1.front

Delete() Delete() Delete()


So front will be So front will be So front will be
increamented by 1. increamented by 1. increamented by 1.

36 64 64

0 1 2 3 4 0 1 2 3 4 0 1 2 3 4

q1.rear q1.rear q1.front


q1.rear
q1.front q1.front
Queue will be empty. So
queue empty condition is
q1.front=q1.rear+1
Array-based Queue Implementation
. Add(99) Add(5)

99 99 5

0 1 2 3 4 0 1 2 3 4

q1.front q1.front

q1.rear q1.rear

Increment rear by 1 and add 99 Increment rear by 1 and add 5 at rear end
at rear end

Add(75)
But rear is Queue full condition is q1.rear=MAX-1
pointing to last
location, so
cannot add
new element.
Array-based Queue Implementation
Algorithm :
1.Define structure containing array for queue and two variables
as rear & front.
2.Initialize rear to -1 and front to 0.
3.Perform add (enqueue) operation,
Before adding data to queue , check queue is full or not
as rear==MAX-1
If full, print as queue is full.
else
Enter the data to be added from user.
Increment the counter i.e. rear struct queue
add that data to queue from the rear side. {
int a[size];
int rear, front;
}q1;
Add Operation & Queue full condition using
function
void add()
{
int n,i,x;
if(full()==1)
printf("\n\tTHE QUEUE IS FULL");
int full()
else {
{ if(q1.rear==MAX-1)
printf("\n\tENTER THE ELEMENT: "); return 1;
scanf("%d",&x);
else
q1.rear++;
q1.a[q1.rear]=x;
return 0;
}
}
}
Array-based Queue Implementation
Algorithm :

5. Before deleting the data from queue , check queue is


empty or not.
If it is empty, print queue is empty.
If not , Print the deleted data and increment the
counter front.
Delete Operation & Queue Empty condition
using function
void del()
{ int empty()
int x; {
char ch; if(q1.front==q1.rear+1)
if(empty()==1) return 1;
printf("\n\tTHE QUEUE IS EMPTY!"); else
else return 0;
{ }
x=q1.a[q1.front];
printf("\n\tTHE ELEMENT DELETED IS: %d",x);
q1.front++;

}
}
Display Operation
void display()
{
int i;
if(empty()==1)
printf("\n\tTHE QUEUE IS EMPTY!");
else
{
for(i=q1.front;i<=q1.rear;i++)
{
printf("\t%d",q1.a[i]);
}
}
}
Linked list-based Queue Implementation

if front=NULL, Queue is empty.

Add (10)
Create the node using malloc function,

p
100
Enter the data as 10 in number field, as
p->number=10 and make next field as NULL and this is first node
in queue so front and rear must point to p as front=rear=p;

10 NULL
p
100
front
rear
Linked list-based Queue Implementation
add(30)
Create the node using malloc function,
p
150
Enter the data as 30 in number field, as
p->number=30 and make next field as NULL

front 10 NULL 30 NULL


p
rear 100 150

Now connect new node p at end as rear->next=p; and make rear


point to new node as rear=rear->next;

p
10 30 NULL
front
100 150
rear
Linked list-based Queue Implementation
add(45)
Create the node using malloc function,
p
150
Enter the data as 45 in number field, as
p->number=45 and make next field as NULL
45 NULL
p
350

Now connect new node p at end as rear->next=p; and make rear


point to new node as rear=rear->next;
p
10 30 45 NULL
front
100 150 350
rear

To display the content of queue, display from front to rear as


10 30 45
Linked list-based Queue Implementation
Algorithm :
1.Define structure containing data to be enter to the queue and link
to the next data.
2.Initialize rear and front variables to NULL.
3.Perform add/enqueue operation,
a. Create new node as p using malloc.
b. Enter data to be added to the queue.
c. make p->next=NULL
d. If (rear==NULL),
make front=rear=p;
struct queue
else, {
rear->next=p; int number;
rear=rear->next; struct queue *next;
}*front, *rear ,
* p,*q;
Add/Enqueue Operation condition using function
void add()
{
p=(struct queue*)malloc(sizeof(struct queue));
printf("\n\tENTER THE DATA: ");
scanf("%d",&p->number);
p->next=NULL;
if(rear==NULL)
{
rear=p;
front=p;
}
else
{
rear->next=p;
rear=rear->next;
}
}
Linked list-based Queue Implementation
delete()
p
10 30 45 NULL
front
100 150 rea 350
r
Deletion must be from front. So, point to front node as
q=front;
Make front to point to next node as front=front->next;
And free(q);
10 30 45 NULL
q
100 150 350
rear
front

front 45 NULL
30
150 350
rear
Linked list-based queue Implementation continue…..
1.Perform deletion operation,
a. initialize one variable as int x.
b. initialize one structure variable as q. and make q=front
c. check for queue is empty or not. i.e. front==NULL or not.
d. if not,
take data of front in the variable x, x=front->number;
move variable front to next node as front=front->next
free q node.
print which data is removed from the queue.

1.Display the queue


a. q=front
c. check for queue is empty or not. i.e. front==NULL or not.
c. If not, print the data in queue till q!=NULL
Delete/Dequeue Operation condition using function
void del()
{
q=front;
if(q==NULL)
printf("\n\tTHE QUEUE IS EMPTY");
else
{
printf("\n\tTHE NO. DELETED IS: %d",front->number);
front=front->next;
q->next=NULL;
free(q);
}
}
Display Operation
void display()
{
q=front;
if(q==NULL)
printf("\n\tTHE QUEUE IS EMPTY!");
else
{
while(q!=NULL)
{
printf("\t%d",q->data);
q=q->next;
}
}
}
Queue using Queue using
array linked list

Queue empty
front==rear+1 front == NULL
condition

Queue full rear == MAX-1 malloc function


returns NULL when
condition heap memory is full.
Then queue full
condition occurs.
Push operation in Same as insertion at
stack front in SLL

Pop operation in Same as deletion at


stack front in SLL

Add operation in Same as insertion at


queue end in SLL
Delete operation in Same as deletion at
queue front in SLL
Circular Queue

Note:
Note that the container of items is an array. Array is stored in main memory.
Main memory is linear. So this circularity is only logical.
There can not be physical circularity in main memory.
60
add 20
Circular Queue
⚫ Formula for circular Queue:
⚫ i=(i+1)% MAX
⚫ Circular Queue Full condition:
int full()
{
if( (q1.rear+1)%MAX==q1.front )
return 1;
else
return 0;
}
Circular Queue-Add function
void add(int x)
{
if(empty()==1)
{ q1.rear=q1.front=0; // rear & front will point to same element
q1.data[q1.rear] = x;
}
else
{ q1.rear=(q1.rear+1)%MAX ;
q1.data[q1.rear] = x;
}
}
Circular Queue-Delete function
int del()
{
int x;
x = q1.data[q1.front] ;
if(q1.rear==q1.front)
{ q1.rear=-1; q1.front=0;
}
else
{ q1.front=(q1.front+1)%MAX ;
}
return x;
}
Circular Queue-Display
void display()
{
int i;
i=q1.front;
while( i ! =q1.rear)
{ printf(“\n %d”,q1.data[i]);
i=(i+1)%MAX ;
}
printf(“\n %d”, q1.data[q1.rear]);
}
Priority Queue
Is an ordered list of Homogeneous elements.
Priority based on the urgency of need.
1. An element of higher priority is processed before any element
of lower priority.
2. Two elements with the same priority are processed according
to the order in which they were added to the queue.
◼ Types of priority queue :
(i) Ascending priority queue
(ii) Descending priority queue.

⚫ Implementation of Priority Queues:


1. Linked list
2. Circular array
Priority Queue
⚫ Implementation of Priority Queues using Linked list :
1. Each node in the list will contain three fields:
a. An information or data field,
b. A priority number.
c. Link or address of next node.
typedef struct node
{
int data, prn;
struct node *next;
}node;
Applications of queue
1. Categorizing data
2. Queue Simulation
3. Job Scheduling

Refer the following book for above applications:


Seymour Lipschutz, ‘Data Structure with C’, Schaum’s Outlines,
Tata McGrawHill (2004).
Real-time application of queue
1) Serving requests of a single shared resource (printer, disk, CPU), transferring data
asynchronously (data not necessarily received at same rate as sent) between two processes (IO
buffers), e.g., pipes, file IO, sockets.
2) Call center phone systems will use a queue to hold people in line until a service representative
is free.
3) Buffers on MP3 players and portable CD players, iPod playlist. Playlist for jukebox - add songs to
the end, play from the front of the list.
4) When programming a real-time system that can be interrupted (e.g., by a mouse click or
wireless connection), it is necessary to attend to the interrupts immediately, before proceeding
with the current activity. If the interrupts should be handles in the same order they arrive, then
a FIFO queue is the appropriate data structure.
5) When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk
Scheduling.
6) When data is transferred asynchronously (data not necessarily received at same rate as sent)
between two processes. Examples include IO Buffers, pipes, file IO, etc.

You might also like