0% found this document useful (0 votes)
1 views32 pages

Queues - Array Implementation

A queue is an ordered collection of elements that follows the first-in first-out (FIFO) principle, with basic operations including enqueue (inserting at the rear) and dequeue (removing from the front). Various types of queues exist, such as linear, circular, deques, and priority queues, each serving different applications in real life and computer science. Implementation can be done using arrays or linked lists, with specific functions to manage operations like checking if the queue is empty or full, and displaying its elements.

Uploaded by

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

Queues - Array Implementation

A queue is an ordered collection of elements that follows the first-in first-out (FIFO) principle, with basic operations including enqueue (inserting at the rear) and dequeue (removing from the front). Various types of queues exist, such as linear, circular, deques, and priority queues, each serving different applications in real life and computer science. Implementation can be done using arrays or linked lists, with specific functions to manage operations like checking if the queue is empty or full, and displaying its elements.

Uploaded by

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

QUEUE

• Queue is defined as ordered collection of Elements that


are inserted and removed according to the first-in first-
out (FIFO) principle.
• Basic operations Fron Rear
t
Enqueue, which inserts an element at the end of
the list (called the rear)
Dequeue, which deletes (and returns) the element
at the start of the list (known as the front).

July 9, 2025 1
Queue
• Stores a set of elements in a particular order
• Queue principle: FIRST IN FIRST OUT
• = FIFO
• It means: the first element inserted is the first one
to be removed
• Example

• The first one in line is the first one to be served


Abstract Model of Queue

July 9, 2025 3
Queue Represetation

July 9, 2025 4
Applications of queue
• Queues are useful for many simulations, and are also
used for some operations on graphs and trees.
• When jobs are submitted to a printer, they are
arranged in order of arrival.
• Every real-life line is a queue. For instance, lines at
ticket counters are queues, because service is first-
come first-served

July 9, 2025 5
Types of Queues
• Linear Queue
• Circular Queue
• Deques(Double Ended queue)
• Priority Queues
Operations/ADT
• enqueue
– add a new item at the rear
• dequeue
– remove a item from the front
• isEmpty
– check whether the queue is empty or not
• isFull
– check whether the queue is full or not
• size
– return the number of items in the queue
• peek
– return the front item

7
Queue Applications
• 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)
Queues Representation
• Front-Deletion-Dequeue
– Front = NULL=> Queue is empty
– Front=Front+1
• Rear-Insertion-Enqueue
– Rear=Null=>Queue is empty
– Rear=Rear+1
• Front=0,rear=0
Implementation of Linear Queue
• Using Array
• Using Linked list
Queue Using Array
• Structure can be implemented using one dimensional
array. But, queue implemented using array can store
only fixed number of data values.

• Insert or delete the values into that array by using FIFO


(First In First Out) principle with the help of
variables 'front' and 'rear'. Initially both 'front' and
'rear' are set to -1.

• Whenever, we want to insert a new value into the queue,


increment 'rear' value by one and then insert at that
position. Whenever we want to delete a value from the
queue, then increment 'front' value by one and then
display the value at 'front' position as deleted element.
Create an empty queue
• Step 1: Include all the header files which are used in the
program and define a constant 'SIZE' with specific value.
• Step 2: Declare all the user defined functions which are used
in queue implementation.
• Step 3: Create a one dimensional array with above defined
SIZE (int queue[SIZE])
• Step 4: Define two integer variables 'front' and 'rear' and
initialize both with '-1'. (int front = -1, rear = -1)
• Step 5: Then implement main method by displaying menu of
operations list and make suitable function calls to perform
operation selected by the user on queue.
Representation of queue
#define SIZE 10
struct queue
{
int que[size];
int front;
int rear;
}Q;
#include<stdio.h>
#include<stdlib.h> case 2: deQueue();
void enQueue(int); break;
void deQueue(); case 3: display();
void display(); break;
int queue[SIZE], front = -1, rear = -1; default:
void main()
printf("Enter a valid choice\n");
{
break;
int item, choice,a;
}
clrscr();
do{ printf("press 1 to continue,2 to exit\n");
printf("\n\n***** MENU *****\n"); scanf("%d",&e);
printf("1. Insertion\n2. Deletion\n3. }while(e==1);
Display\n4. Exit"); }
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be
insert: ");
scanf("%d",&item);
enQueue(item);
break;
QUEUE OVERFLOW
int qfull()
{
if(Q.rear==size-1)
return 1;
else
return 0;
}
QUEUE UNDERFLOW
int qempty()
{
if((Q.front==-1)||(Q.front> Q.rear))
return 1;
else
return 0;
}
enQueue(item) - Inserting
value into the queue
• Step 1: Check whether queue is FULL. (rear == SIZE-
1)

• Step 2: If it is FULL, then display "Queue is FULL!!!


Insertion is not possible!!!" and terminate the
function.

• Step 3: If it is NOT FULL, then increment rear value


by one (rear++) and set queue[rear] = item.
void enQueue(int item){
if(qfull())
printf("\nQueue is Full!!! Insertion is not
possible!!!");
else{
if(Q.front == -1)
{ Q.front++; }
Q.rear++;
Q.queue[Q.rear] = item;
printf("\nInsertion success!!!");
}
}
deQueue() - Deleting a value from the
Queue
• Step 1: Check whether queue is EMPTY. (front == rear)

• Step 2: If it is EMPTY, then display "Queue is EMPTY!!!


Deletion is not possible!!!" and terminate the function.

• Step 3: If it is NOT EMPTY, then increment the front value by


one (front ++). Then display queue[front] as deleted element.
Then check whether both front and rear are equal
(front == rear), if it TRUE, then set both front and rear to '-1'
(front = rear = -1).
void deQueue()
{
if(qempty())
printf("\nQueue is Empty!!! Deletion is not
possible!!!");
else
{
printf("\nDeleted : %d", Q.queue[Q.front]);
Q.front++;

}}
display() - Displays the elements of a
Queue
• Step 1: Check whether queue is EMPTY. (front == rear)

• Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and


terminate the function.

• Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set
'i = front+1'.

• Step 4: Display 'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until 'i' value is equal to rear (i <= rear)
void display()
{
if(qempty())
printf("\nQueue is Empty!!!");
else
{
int i;
printf("\nQueue elements are:\n");
for(i=Q.front; i<=Q.rear; i++)
printf("%d\t",Q.queue[i]);
Circular Queue
• Element next to the last element is the first
element.
• Front=-1, Queue=-1.
Circular Array

rear=3
• Wrapped around array
3
2
C
n-1 3 2 1 0 front=0 1 B
…… C BA
0 A
rear=3 front=0 n-1

25
EnQueue & DeQueue In Circular Array
• EnQueue • DeQueue
– rear = (rear + 1) MOD n – front = (front + 1) MOD n

rear=3 front=1
3 3
2 2
C C
1 B 1 B
0 A 0
n-1 n-1

26
do enqueue
{
Rear=(rear+1)%max,
If(front==rear)
{ “Q is full”;}
Else
{
Q[rear]=x;
Front=0;
}
}
While(op==1)
Front=-1,rear=-1;
Do Dequeue
{
Rear=(rear+1)%max,
If(front==rear)
{ “Q is empty”;}
Else
{
Q[front]=0;
Rear=0;
}
}
While(op==1)
DEQUES
• Two ends LEFT and RIGHT
• Two Variations
– Input Restricted deque
• Insertion at only one end of the list but deletion at both
the ends
– Output Restricted deque
• Deletion at only one end of the list but Insertion at
both the ends
Priority Queues
• Each element is assigned priority
• Elements are added or removed as per
priority
• Lower priority number indicates higher
priority
• Priority Queue can be implemented using
multiple queues
Priority Queue
• Two queues
– one is high priority queue
– the other is low priority queue
• Service rules:
– First serve the people in high priority queue
– If no passengers are in high priority queue, serve
the passengers in low priority queue

31
Two Queues

• High Priority Queue, will come in hpQue


• Low Priority Queue, will come in lpQue
High Priority Queue
Customers coming in
HD C
Check In
G F E B A
Low Priority Queue

32

You might also like