You are on page 1of 48

Data Structure and Algorithms

QUEUE

Puneet Kumar Jain

CSE Department
National Institute of Technology Rourkela
Content
▪ Queue
▪ Operations on queue,
▪ Array and linked list representation
▪ Deque
▪ Priority queue

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


QUEUE
▪ A queue is a FIFO (First-In, First-Out) data structure in which the
element that is inserted first is the first one
▪ The elements in a queue are added at one end called the REAR
and removed from the other end called the FRONT.

Alternative terms may be used for the four operations on a queue, including:

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Operations on queues
The following operations are performed on queues:
▪ Create queue(q)—to create queue as an empty queue.

▪ Enqueue(q,i)—to insert element I in a q.

▪ Dequeue(q)—to access and remove an element of queue q.

▪ Peek(q)—to access the first element of the queue q without


removing it.

▪ Isfull(q)—to check whether the queue q is full.

▪ Isempty(q)—to check whether the queue q is empty.

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Implementations
▪ We will look at two implementations of queues:
▪ Array
▪ Singly linked lists

▪ Requirements:
▪ All queue operations must run in Q(1) time

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Array representation of QUEUES
▪ Every queue has front and rear variables that point to the
position from where deletions and insertions can be done,
respectively
FRONT = 0 and REAR = 5.

▪ Operations on Queues
▪ Enqueue

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Array representation of QUEUES
▪ Dequeue

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Linked list representation of queue
▪ The START pointer of the linked list is used as FRONT.

▪ Here, we will also use another pointer called REAR, which will
store the address of the last element in the queue.

▪ All insertions will be done at the rear end and all the deletions
will be done at the front end.

▪ If FRONT = REAR = NULL, then it indicates that the queue is


empty.

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Operations on Linked Queues
▪ Enqueue:

Insert 9

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Operations on Linked Queues
▪ Dequeue:

Delete

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


TYPES OF QUEUES
▪ A queue data structure can be classified into the following types:
▪ Circular Queue Deque

▪ Priority Queue

▪ Multiple Queue

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Issue with array representation of queue
▪ Suppose that:
▪ The array capacity is 16
▪ We have performed 16 pushes
▪ We have performed 5 pops
▪ The queue size is now 11

▪ We perform one further push

▪ In this case, the array is not full and yet we cannot place
any more objects in to the array

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Circular queue
▪ Instead of viewing the array on the range 0, …, 15,
consider the indices being cyclic:
…, 15, 0, 1, …, 15, 0, 1, …, 15, 0, 1, …

▪ This is referred to as a circular array

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Circular queue
▪ Now, the next push may be performed in the next
available location of the circular array:
if ( Rear == MAX-1 ) {
Rear = 0;
}

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Circular Queues
▪ The first index comes right after the last index
▪ Conceptually, you can think of a circular queue as shown in Fig
FRONT
REAR

▪ Do we have circular data element?


▪ A circular queue is implemented in the same manner as a linear
queue is implemented.
▪ The only difference will be in the code that performs insertion
and deletion operations
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Circular Queues
Case: If front > 0 and front <rear, the circular queue have rear-front+1
element. Front = 2 rear=4
12 8 9
0 1 2 3 4 5 6 7 8 9

Case : If rear != MAX – 1, then rear will be incremented and the


value will be inserted
FRONT = 0 and REAR = 8

0 1 2 3 4 5 6 7 8 9

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Circular Queues
Case : If front = 0 and rear = MAX – 1, then the circular queue is full.
FRONT = 0 and REAR = 9

0 1 2 3 4 5 6 7 8 9

Case : If rear=front-1, the circular queue is full.


Front = 2 rear=1
70 6 12 8 9 5 80 68 4 90
0 1 2 3 4 5 6 7 8 9

Case : 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
FRONT = 2 and REAR = 9.

0 1 2 3 4 5 6 7 8 9
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Circular Queues
Case 1: If front = -1 and rear = – 1, the circular queue is empty.
Front = -1 rear=-1

0 1 2 3 4 5 6 7 8 9

Case 2: If front = = rear =0, the circular queue have one element
Front = 0 rear=0
5
0 1 2 3 4 5 6 7 8 9
Case 3: If front == rear != 0, the circular queue have one element
Front = 4 rear=4
9
0 1 2 3 4 5 6 7 8 9
Case 4: If Front =Max-1, deltion can be done by making front=0
Front = 9 rear=4
4 5 6 7 9 3
0 1 2 3 4 5 6 7 8 9
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Deque
▪ Deque (Double ended queue):
▪ Uses a explicit linear ordering
▪ Allows insertions at both the front and back of the deque
▪ 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
▪ implemented using either a circular array or a circular doubly
linked list
▪ Two pointers are maintained, LEFT and RIGHT, which point to
either end of the deque

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Deque variations
▪ 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.

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Priority Queues
▪ 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.

▪ Priority queues are widely used in operating systems to execute


the highest priority process first.

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Implementation of a Priority Queue
▪ Sorted list: store the elements in sorted order so that, the queue will
not have to be searched for the element with the highest priority

▪ Unsorted list: insertions are always done at the end of the list. The
element with the highest priority will be searched and removed.

▪ While a sorted list takes O(n) time to insert an element in the list, it
takes only O(1) time to delete an element.

▪ On the contrary, an unsorted list will take O(1) time to insert an


element and O(n) time to delete an element from the list.

▪ Practically, both these techniques are inefficient and usually a blend of


these two approaches is adopted that takes roughly O(log n) time or
less. (Using heap)
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Linked Representation of a Priority Queue
▪ Every node of the list will have three parts:
▪ the information or data part
▪ the priority number of the element, and
▪ the address of the next element.

▪ Lower priority number means higher priority

▪ Here, the element with a higher priority comes before the


element with a lower priority

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Priority Queues
▪ Deletion: The first node of the list will be deleted (The node with
highest priority).

▪ Insertion:
▪ Traverse the entire list until we find a node that has a priority
lower than that of the new element.
▪ The new node is inserted before the node with the lower
priority.
▪ if same priority element is there in the queue as the new
element, the new element is inserted after that element.

data = F and priority number = 4

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Applications
▪ The most common application is in client-server models
▪ Multiple clients may be requesting services from one or more
servers
▪ Some clients may have to wait while the servers are busy
▪ Those clients are placed in a queue and serviced in the order of
arrival

▪ Grocery stores, banks, and airport security use queues

▪ The SSH Secure Shell and SFTP are clients

▪ Most shared computer services are servers:


▪ Web, file, ftp, database, mail, printers, WOW, etc.

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Applications of Queue
▪ Queues are widely used as waiting lists for a single shared resource like
printer, disk, CPU.

▪ Queues are used to transfer data asynchronously (data not necessarily


received at same rate as sent) between two processes (IO buffers), e.g.,
pipes, file IO, sockets.

▪ Queues are used as buffers on MP3 players and portable CD players,


iPod playlist.

▪ Priority queues are used in operating system for handling interrupts.


When programming a real-time system that can be interrupted, for
example, by a mouse click, it is necessary to process the interrupts
immediately, before proceeding with the current job. If the interrupts
have to be handled in the order of arrival, then a FIFO queue is the
appropriate data structure.

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Another application is performing a breadth-first
traversal of a directory tree
▪ Consider searching the directory structure

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ We would rather search the more shallow directories
first then plunge deep into searching one sub-directory
and all of its contents

▪ One such search is called a breadth-first traversal


▪ Search all the directories at one level before descending a
level

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ The easiest implementation is:
▪ Place the root directory into a queue
▪ While the queue is not empty:
▪ Pop the directory at the front of the queue
▪ Push all of its sub-directories into the queue

▪ The order in which the directories come out of the


queue will be in breadth-first order

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Push the root directory A

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Pop A and push its two sub-directories: B and H

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Pop B and push C, D, and G

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Pop H and push its one sub-directory I

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Pop C: no sub-directories

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Pop D and push E and F

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Pop G

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Pop I and push J and K

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Pop E

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Pop F

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Pop J

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ Pop K and the queue is empty

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Application: tree traversal (BFS)
▪ The resulting order
ABHCDGIEFJK
is in breadth-first order:

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Josephus Problem
▪ It consists of a group of soldiers surrounded by a heavy enemy
force.
▪ There is only one horse to escape. Therefore, only one soldier
can escape.
▪ In order to determine which soldier will escape, they form a
circle and pick up a number n from a hat. A name is also picked
up from the hat.
▪ They start counting clockwise around the circle from a soldier
whose name is picked up from the hat. And the soldier on which
count reaches n is removed from the circle.
▪ The count again starts from the soldier who was next to the
soldier who is removed from the circle.
▪ This process goes on till only one soldier is left.
▪ This soldier will take the horse and escapes.
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Josephus Problem
▪ In a queue of N people, in each step, a certain number of people
(k) are skipped and the next person is executed (or eliminated).

▪ Example: N=5, k=1

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Josephus Problem
#include <stdio.h> void myInit()
#include <conio.h> {
#include <stdlib.h> HEAD=(struct node *)
#include <string.h> malloc(sizeof(struct node));
struct node TAIL=(struct node *)
{ malloc(sizeof(struct node));
char info[25]; HEAD->NEXT=TAIL;
struct node *NEXT; TAIL->NEXT=HEAD;
}; }
struct node *HEAD;
struct node *TAIL;
char name[25];
Josephus Problem
void myInsert() void myRemove()
{ {
struct node *TEMP; struct node *TEMP;
TEMP=(struct TEMP=(struct node*)
node*)malloc(sizeof(struct malloc(sizeof(struct node));
node)); TEMP=HEAD->NEXT;
strcpy(TEMP->info,name); HEAD->NEXT=HEAD->NEXT-
TEMP->NEXT=HEAD->NEXT; >NEXT;
HEAD->NEXT=TEMP; strcpy(name,TEMP->info);
} }
Josephus Problem
void main() scanf("%s",&name);
myInsert();
{
}
clrscr(); printf("Value for Counter: ");
int numSol, counter; scanf("%d",&counter);
myInit(); for(int j=1;j<=numSol;j++)
printf("Enter the number of {
soldier: "); for(int k=1;k<=counter;k++)
{
scanf("%d",&numSol);
myRemove();
for(int i=1;i<=numSol; i++) if(k==counter)
{ {
printf("Pls. Type a Name: "); puts(name);
getch();
}}}}
• End of chapter

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”

You might also like