0% found this document useful (0 votes)
16 views

Queues Using Arrays

Using a Queue data structure, this presentation describes queue operations and two implementations of a queue using an array. It discusses the FIFO principle of queues and real-life examples. Key queue operations of enqueue and dequeue are demonstrated. Array implementation tracks size, front, and rear indexes but requires special logic for wrapping at the end of the array.

Uploaded by

Arjun Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Queues Using Arrays

Using a Queue data structure, this presentation describes queue operations and two implementations of a queue using an array. It discusses the FIFO principle of queues and real-life examples. Key queue operations of enqueue and dequeue are demonstrated. Array implementation tracks size, front, and rear indexes but requires special logic for wrapping at the end of the array.

Uploaded by

Arjun Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Using a Queue

 Introduction to the queue


data structure.
 This presentation describes
the queue operations and
two ways to implement a
queue.

Data Structures
Using C
Queue
 Storesa 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


Queue Applications

 Real life examples


 Waiting in line
 Waiting on hold for tech support

 Applications related to Computer Science


 Threads
 Jobscheduling (e.g. Round-Robin algorithm for
CPU allocation)
The Queue Operations

 A queue is like a line of people waiting for a bank


teller.
 The queue has a front and a rear.

Rear Front
The Queue Operations

 New people must enter the queue at the rear. The C


queue function calls this a push, although it is
usually called an enqueue operation.

Front
Rear
The Queue Operations

 When an item is taken from the queue, it always


comes from the front. The C queue function calls
this a pop, although it is usually called a dequeue
operation.
$ $

Front
Rear
The Queue Operations

 When an item is taken from the queue, front is


updated to refer to the next item in queue as first
item
$ $

Rear
Front
Array Implementation

 A queue can be implemented with an array, as


shown here. For example, this queue contains the
integers 4 (at the front), 8 and 6 (at the rear).

[0] [1] [2] [3] [4] [5] ...

4 8 6

An array of integers
to implement a We don't care what's in
queue of integers this part of the array.
Array Implementation

 The easiest implementation also keeps 3 size


track of the number of items in the
queue and the index of the first 0 first
element (at the front of the queue), the
last element (at the rear).
2 last

[0] [1] [2] [3] [4] [5] ...

4 8 6
A Dequeue Operation

 When an element leaves the queue, 2 size


size is decremented, and first changes,
too. 1 first

dequeue(); 2 last

[0] [1] [2] [3] [4] [5] ...

4 8 6
An Enqueue Operation

 When an element enters the queue, 3 size


size is incremented, and last changes,
too. 1 first

enqueue(2); 3 last

[0] [1] [2] [3] [4] [5] ...

8 6 2
An Enqueue Operation

 When an element enters the queue, 4 size


size is incremented, and last changes,
too. 1 first

enqueue(7); 4 last

[0] [1] [2] [3] [4] [5] ...

8 6 2 7
An Enqueue Operation

 When an element enters the queue, 5 size


size is incremented, and last changes,
too. 1 first

enqueue(1); 5 last

[0] [1] [2] [3] [4] [5]

8 6 2 7 1
An Enqueue Operation

 When an element enters the queue, 6 size


size is incremented, and last changes,
too. 1 first

enqueue(5); 6 last

[0] [1] [2] [3] [4] [5] [6]

8 6 2 7 1 5
An Dequeue Operation

 When an element enters the queue, 5 size


size is incremented, and last changes,
too. 2 first

dequeue(); 6 last

[0] [1] [2] [3] [4] [5] [6]

8 6 2 7 1 5
An Dequeue Operation

 When an element enters the queue,


size is incremented, and last changes,
too. 3 first

dequeue(); 6 last

[0] [1] [2] [3] [4] [5] [6]

8 6 2 7 1 5
Implementing a Queue

Enqueue at data[rear+1]
Dequeue at data[front]
The rear variable always contains the
index of the last item in the queue.
The front variable always contains the
index of the first item in the queue.
When we reach the end of the array, wrap
around to the front again.
17
Implementing a Queue

Conditions for
 Queue FULL?
3 first
 Queue EMPTY? 3

66 last

[0] [1] [2] [3] [4] [5] [6]

8 6 2 7 1 5

18
 Initialization of front and rear?
 int front = -1, rear = -1;
 How do we know if a queue is full or
empty?
 Testing for a full or empty queue.
if(rear == maxsize-1) if (front == -1 || front > rear)
{ {
printf("\nOVERFLOW\n"); printf("\nUNDERFLOW\n");
return; return;
} }
Implementing a Queue

// enqueue function

enqueue(Object item) { // check for overflow


if(totalItems == 0) front = rear = 0;
else rear = (rear + 1);
data[rear] = item;
totalItems++;
}
20
Implementing a Queue

// dequeue function
dequeue() { // check for underflow
item = data[front];
if (front == rear)
{ front = -1;
rear = -1 ; }
else
{ front = front + 1;

totaltems--; }

printf("\nvalue deleted ");


}
21
return answer; }
Implementing Queue

Check the following code segment carefully:


if(rear == maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}

Issue?
At the End of the Array

 There is special behavior at the end of 3 TE


the array. For example, suppose we
want to add a new element to this 3 first
queue, where the last index is [5]:
5 last
if(rear == maxsize-1)

[0] [1] [2] [3] [4] [5]

2 6 1
Circular Queue - At the End of
the Array
 The new element goes at the front of 4 TE
the array (if that spot isn’t already
used): 3 first

0 last

[0] [1] [2] [3] [4] [5]

4 2 6 1
Circular Queue

 When an element enters the queue, 4 TE


size is incremented, and last
changes, too. 3 first

0 last

[0] [1] [2] [3] [4] [5]

4 2 6 1
Implementing a Circular Queue

// enqueue function

enqueue(Object item) {// check for overflow


if(totaItems == size)
{
printf("Queue is full\n");
}
if(totaltems == 0) front = rear = 0;
else rear = (rear + 1) mod size;
data[rear] = item;
totalItems++; }
26
Implementing a Circular Queue

// dequeue function
dequeue() {// check for underflow
if(totaltems==0)
{
printf("Queue is empty\n");
}

else{ answer = data[front];


front = (front + 1) mod size;
totaltems--;
27 return answer; } }
Array Implementation

 Easy to implement 3 TE
 But it has a limited capacity with a fixed array
 Special behaviour is needed when the rear 0 first
reaches the end of the array.

2 last

[0] [1] [2] [3] [4] [5] ...

4 8 6
Array Implementation of Linear Queue
#include<stdio.h> switch(choice)
#include<stdlib.h> {
void insert(); case 1:
void dequeue(); insert();
void display(); break;
int front = -1, rear = -1 ,maxsize; case 2:
int queue[100]; dequeue();
int main () break;
{ case 3:
int choice; display();
printf("\n Enter the size of QUEUE : "); break;
scanf("%d",&maxsize); case 4:
printf("\n QUEUE OPERATIONS USING exit(0);
ARRAY"); break;
printf("\n1.insert an element\n2.Delete an default:
element\n3.Display the queue\n4.Exit"); printf("\nEnter valid choice??\n");
while(choice != 4) }
{ }
printf("\nEnter your choice : "); return 0;
scanf("%d",&choice); }
Array Implementation of Linear Queue
void enqueue()
{
int item;
printf("\nEnter the element\n");
scanf("\n%d",&item);
if(rear == maxsize-1 && front==0)
{
printf("\nOVERFLOW\n");
return; State the issue in this enqueue function.
} Include the solution to overcome the issue.
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");
}
Array Implementation of Linear Queue
void dequeue()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}
}
Array Implementation of Linear Queue

void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\n Elements in the queue are\n");
for(i=front;i<=rear;i++)
{
printf("\n%d",queue[i]);
}
}
}
Write the complete program to implement the circular queue.

You might also like