Queue
Queue concepts
• The process of inserting an element in the
queue is called enqueue,
• The process of deleting an element from the
queue is called dequeue.
• Every queue has front variable that point to
the position from where deletions can be
done
• And rear variable that point to the position
from where insertions can be done
ARRAY REPRESENTATION OF QUEUES
Algorithm to insert an element in
a queue
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
Algorithm to delete an element from a
queue
Step 1: IF FRONT = -1 OR FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
Function to insert an element in
a queue
void enqueue(int val)
{
q.rear++;
//increment rear to point to next empty slot
q.data[q.rear]=val;
}
Function to delete an element from a
queue
int dequeue()
{
int k,ans;
k=isEmpty();
if(k==0)//queue is not empty
{ ans=q.data[q.front];
q.front++;
}
Function to delete an element from a
queue
else
{ printf("Queue is empty\n");
ans=-1;
}
return(ans);
}
Function to delete an element from a
queue
int isEmpty()
{ int ans;
if(q.rear<q.front)
ans=1;
else
ans=0;
return(ans);
}
Function to display an element from a
queue
void display()
{ int ans,i;
printf("****data elements in queue****\n");
ans=isEmpty();
if(ans ==0)
{ for(i=q.front;i<=q.rear;i++)
printf("%d\n",q.data[i]);
} else
printf("queue is empty\n");
}
LINKED REPRESENTATION OF QUEUES
Algorithm to insert an element in a
linked queue
Step 1: Allocate memory for the new node and name it as PTR
Step 2: SET PTR DATA = VAL
Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT NEXT = REAR NEXT = NULL
ELSE
SET REAR NEXT = PTR
SET REAR = PTR
SET REAR NEXT = NULL
[END OF IF]
Step 4: END
Delete queue
Algorithm to delete queue
Step 1: IF FRONT = NULL
Write Underflow
Go to Step 5
[END OF IF]
Step 2: SET PTR = FRONT
Step 3: SET FRONT = FRONT -> NEXT
Step 4: FREE PTR
Step 5: END
Implementation of linked queue
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*front = NULL,*rear = NULL;
Implementation of linked queue
void enqueue(int);
void dequeue();
void display();
int main()
{
int choice, value;
do
{
printf("\n Enter a choice : 1.Insert, 2.Delete, 3.Display
: ");
scanf("%d",&choice);
Implementation of linked queue
switch(choice)
{
case 1: printf("\n Enter a value : ");
scanf("%d", &value);
enqueue(value);
break;
case 2: dequeue();
break;
Implementation of linked queue
case 3: display();
break;
default: printf("\n Invalid choice \n");
}
} while(choice!=4);
}
Implementation of linked queue
void enqueue(int value)
{
struct node *newnode;
newnode = (struct node*)malloc(sizeof(struct
node*));
newnode->data = value;
newnode -> next = NULL;
Implementation of linked queue
if(front == NULL)
front = rear = newnode;
else
{
rear -> next = newnode;
rear = newnode;
}
printf("\n Number has been inserted. \n");
}
Implementation of linked queue
void dequeue()
{
if(front == NULL)
printf("\n Queue is Empty. \n");
else
{
struct node *temp = front;
front = front -> next;
printf("\n Deleted element: %d\n", temp->data);
free(temp);
}
}
Implementation of linked queue
void display()
{
if(front == NULL)
printf("\n Queue is Empty. \n");
else
{
struct node *temp = front;
while(temp->next != NULL)
{
printf("%d ---> ",temp->data);
temp = temp -> next;
}
printf("%d ---> NULL\n",temp->data);
}
}
Types of queues
• Simple queue- additions at rear and deletions from
front
• Circular queue- last node is connected to first
node, deletions at front end while insertions are
done at rear end
• Doubly ended queue- deletions and insertions can
be done at both the ends, has two pairs of fronts
and rears, both
• Priority queue- every element has predefined
priority
– Max priority : element with max priority is removed
first
– min priority: element with min priority is removed last
Simple Queue
Image courtesy: GeeksforGeeks.org
Circular Queue
Image courtesy: GeeksforGeeks.org
Circular Queue
Circular Queue array Representation
If front = 0 and rear = MAX – 1, then the circular queue is
full.
If rear != MAX – 1, then rear will be incremented and the
value will be inserted
If front != 0 and rear = MAX – 1, then it means that the
queue is not full. So, set rear = 0 and insert the new
element there
Algorithm to insert an element in
a circular queue
Step 1: IF FRONT = 0 and REAR = MAX-1
Write OVERFLOW
Goto step 4
[END OF IF]
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] = NUM
Step 4: EXIT
Algorithm to delete an element from a
circular queue
Step 1: IF FRONT = -1
Write UNDERFLOW
Goto Step 4
Step2: 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
Linked Representation of Circular
Queue
Image courtesy: https://www.programiz.com/
Linked Representation of Circular
Queue
struct node
{
int data;
struct node *next;
}*f = NULL,*r = NULL;
Linked Representation of Circular
Queue
void enqueue (int d) //Insert elements in Queue
{
struct node *n;
n = (struct node *) malloc (sizeof (struct node));
n->data = d;
n->next = NULL;
if ((r == NULL) && (f == NULL))
{
f = r = n;
r->next = f;
}
Linked Representation of Circular
Queue
//Insert elements in Queue
else
{
r->next = n;
r = n;
n->next = f;
}
}
Linked Representation of Circular
Queue
void dequeue () // Delete an element from Queue
{
struct node *t;
t = f;
if ((f == NULL) && (r == NULL))
printf ("\nQueue is Empty");
else if (f == r)
{
f = r = NULL;
free (t);
}
Linked Representation of Circular
Queue
// Delete an element from Queue
else
{
f = f->next;
r->next = f;
free (t);
}
Doubly ended Queue- Dequeue/deck
Array Representation
Image courtesy: GeeksforGeeks.org
Doubly ended Queue- Dequeue/deck
Linked List Representation
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.
Doubly ended Queue- Dequeue/deck
Linked List Representation
Insertion at Front end :
Allocate space for a newNode of doubly linked list.
IF newNode == NULL, then
print "Overflow" ELSE
IF front == NULL, then
rear = front = newNode
ELSE
newNode->next = front
front->prev = newNode
front = newNode
Doubly ended Queue- Dequeue/deck
Linked List Representation
Insertion at Rear end :
Allocate space for a newNode of doubly linked list.
IF newNode == NULL, then
print "Overflow"
ELSE
IF rear == NULL, then
front = rear = newNode
ELSE
newNode->prev = rear
rear->next = newNode
rear = newNode
Doubly ended Queue- Dequeue/deck
Linked List Representation
Deletion from Front end :
IF front == NULL
print "Underflow"
ELSE
Initialize temp = front
front = front->next
IF front == NULL
rear = NULL
ELSE
front->prev = NULL
Deallocate space for temp
Doubly ended Queue- Dequeue/deck
Linked List Representation
Deletion from Rear end :
IF front == NULL
print "Underflow"
ELSE
Initialize temp = rear
rear = rear->prev
IF rear == NULL
front = NULL
ELSE
rear->next = NULL
Deallocate space for temp
Priority Queue
Index Front Rear
Data 10 5 3 98 12 36
Priority 4 4 3 2 1 1
The priority of the element will be used to determine
the order in which the elements will be processed.
Array Representation of a Priority
Queue
Array Representation of a Priority
Queue
Priority Queue Linked List
implementation
• A priority queue can be represented using arrays or
linked lists.
• When a priority queue is implemented using a linked
list, then every node of the list will have three parts:
• (a) the information or data part, (b) the priority
number of the element, and (c) the address of the next
element.
• If we are using a sorted linked list, then the element
with the higher priority will precede the element with
the lower priority.
Priority Queue
Priority Queue
struct node
{
int data;
int priority;
struct node *next;
}
Priority Queue
struct node *insert(struct node *start)
{
int val, pri;
struct node *ptr, *p;
ptr = (struct node *)malloc(sizeof(struct node));
printf("\n Enter the value and its priority : " );
scanf( "%d %d", &val, &pri);
ptr–>data = val;
ptr–>priority = pri;
if(start==NULL || pri < start–>priority )
{
ptr–>next = start;
start = ptr;
}
Priority Queue
else
{
p = start;
while(p–>next != NULL && p–>priority <= pri)
p = p–>next;
ptr–>next = p–>next;
p–>next = ptr;
}
return start;
}
Priority Queue
struct node *delete(struct node *start)
{
struct node *ptr;
if(start == NULL)
{
printf("\n UNDERFLOW" );
return;
}
Priority Queue
else
{
ptr = start;
printf("\n Deleted item is: %d", ptr–>data);
start = start–>next;
free(ptr);
}
return start;
}
Priority Queue
void display(struct node *start)
{
struct node *ptr;
ptr = start;
if(start == NULL)
printf("\nQUEUE IS EMPTY" );
Priority Queue
else
{
printf("\n PRIORITY QUEUE IS : " );
while(ptr != NULL)
{
printf( "\t%d[priority=%d]", ptr–>data, ptr–>priority );
ptr=ptr–>next;
}
}
}
Applications of queues
• Queues are widely used as waiting lists for a
single shared resource like printer, disk, CPU.
• To transfer data asynchronously (data not
necessarily received at same rate as sent)
between two processes (IO buffers), e.g., pipes,
file IO, sockets.
• As buffers on MP3 players and portable CD
players, iPod playlist.
• used in operating system for handling interrupts.
Applications of queues
• In Josephus problem, n people stand in a circle
waiting to be executed.
• The counting starts at some point in the circle and
proceeds in a specific direction around the circle.
• In each step, a certain number of people are skipped
and the next person is executed (or eliminated).
• The elimination of people makes the circle smaller
and smaller.
• At the last step, only one person remains who is
declared the ‘winner’.
Applications of queues
• If there are n number of people and a number k
which indicates that k–1 people are skipped and k–th
person in the circle is eliminated, then the problem is
to choose a position in the initial circle so that the
given person becomes the winner.