Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
71 views

Data Structure Module-3 Queue

The document discusses queue data structures and their array implementation, describing queues as first-in, first-out linear data structures and how to implement basic queue operations like enqueue, dequeue, and checking for empty or full using arrays. It also covers applications of queues and a C implementation of a queue using structures.

Uploaded by

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

Data Structure Module-3 Queue

The document discusses queue data structures and their array implementation, describing queues as first-in, first-out linear data structures and how to implement basic queue operations like enqueue, dequeue, and checking for empty or full using arrays. It also covers applications of queues and a C implementation of a queue using structures.

Uploaded by

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

Data Structures and

Algorithms
Module -3
Queue
Dr. R. Jothi, SCOPE
VIT Chennai
Outline
 Queue data structure
 Array Implementation
 Applications

Dr. R. Jothi, VIT Chennai


Queue
 A queue is a linear data structure
 models the first-In-first-Out order (FIFO)
 The element that is inserted first into the queue, is
deleted first
I arrived
first

Dr. R. Jothi, VIT Chennai


Queue

Dr. R. Jothi, VIT Chennai


Applications
 Mailbox
 Whatsapp messages
 Waiting line at any counter
 Process Scheduling
 Etc.,

Dr. R. Jothi, VIT Chennai


Operations on Queue
 Enqueue(Q, x) – Inserting an element x at rear end of Q
 Dequeu(Q) - Deleting the element from front end of Q
 Front(Q) - Return the front element of queue
 Rear(Q) - Return the rear element of queue
 IsEmpty
 IsFull

Dr. R. Jothi, VIT Chennai


Queue Example

5 0

Dr. R. Jothi, VIT Chennai


Array Implementation
front rear

5 3 12 4
0 1 2 3 4 5 6 7 8 9

 How do you initialize front and rear ?

Dr. R. Jothi, VIT Chennai


Empty Queue
rear
front

-1 0 1 2 3 4 5 6 7 8 9

int IsEmpty(Q)
 Empty Queue {

 front  -1 #write your code here


 rear  -1
}
Dr. R. Jothi, VIT Chennai
Empty Queue
rear
front

-1 0 1 2 3 4 5 6 7 8 9

int IsEmpty(Q)
{
 Empty Queue
if (front == -1 && rear==-1)
 front  -1
return 1;
 rear  -1
return 0;
} Dr. R. Jothi, VIT Chennai
Enqueue : insert x into Q
front rear
 Enqueue
9 2 5
 rear  rear +1
Before
0 1 2 3 4 5 6 7 8 9  Q[rear] x

front rear
• Suppose if Q is empty?
• Inserting first entry
After 9 2 5 x
• Suppose if Q is full ?
0 1 2 3 4 5 6 7 8 9

Dr. R. Jothi, VIT Chennai


Enqueue
Enqueue(Q)
{
if (IsFull(Q))
{ printf(“Full “)
return;
}
else if (front == -1 && rear==-1)
{ front=0;
rear=0;
}
else
rear=rear+1;

Q[rear]= x;
} Dr. R. Jothi, VIT Chennai
How do you check whether Queue is Full?
front rear
 rear = Maxsize-1
9 2 5 11 7 23 4 1 6 3
0 1 2 3 4 5 6 7 8 9

front rear
 Is this Q full?
 Free slots at front can not be
reused 5 11 7 23 4 1 6 3
 Solution
0 1 2 3 4 5 6 7 8 9
 Circular Queue

Dr. R. Jothi, VIT Chennai


Dequeue : delete x from Q
front rear
 Dequeue
9 2 5 1 8
 front  front +1
Before
0 1 2 3 4 5 6 7 8 9

front rear • Suppose if Q is empty?


• Suppose Q has only one
element?
After 2 5 1 8
0 1 2 3 4 5 6 7 8 9

Dr. R. Jothi, VIT Chennai


Dequeue
Dequeue(Q)
{
if (IsEmpty(Q))
{ printf(“Empty “)
return;
}
else if (front == rear )
{ front=-1;
rear=-1;
}
else
front = front +1;

}
Dr. R. Jothi, VIT Chennai
Front :
front rear
front_function(Q)
Before 9 2 5 1 8 {
0 1 2 3 4 5 6 7 8 9 return Q[front];
}
front rear

After 9 2 5 1 8
0 1 2 3 4 5 6 7 8 9

Dr. R. Jothi, VIT Chennai


Linear Vs. Circular Queue
front rear

 Linear Queue
b c j e f g i a
 Waste of memory
0 1 2 3 4 5 6 7 8 9

0 1
rear
9 2 front
a b

 Circular Queue 8 i c 3

g j
7 4
f e
Dr. R. Jothi, VIT Chennai
6 5
Circular Queue
 front increases by (1 modulo Maxsize) 0 1
rear
after each dequeue( ): 9 2 front
 front=front+1 % Maxsize a b
 rear increases by (1 modulo capacity)
8 i c 3
after each enqueue( ):
 rear=rear+1 % Maxsize g j
7
f e 4
6 5

Dr. R. Jothi, VIT Chennai


When Circular Queue is Fullrear
0 1
 Condition for overflow p s
9 2 front
 (rear+1) % Maxsize == front a b

8 i c 3

g j
7
f e 4
6 5
(rear+1)% Maxsize = (1+1) % 10 = 2 = front

Dr. R. Jothi, VIT Chennai


Enqueue in Circular queue
Enqueue(Q, x)
{
if(rear+1) % Maxsize == front)
return;
else if IsEmpty(Q)
{
front=rear=0;
}
else
rear= (rear+1) % Maxsize ;

Q[rear]=x;
}

Dr. R. Jothi, VIT Chennai


When Circular Queue is Empty
0 1
 Condition for empty
9 2
 front==-1 && rear==-1

8 3

7
4
6 5
rear = -1
front = -1
Dr. R. Jothi, VIT Chennai
Queue : C Implementation

Data structure Function prototypes


struct queue{  struct queue * InitQueue(int size);
int front,rear;  void enqueue(struct queue T*, int x);
int *contents;  void dequeue(struct queue * T);
int Maxsize;  int IsEmpty(struct queue *T);
};  int Isfull(struct queue *T);
 int front_fun(struct queue * T);
 int rear_fun(struct queue * T);

Dr. R. Jothi, VIT Chennai


Deueue in Circular queue
Dequeue(Q)
{
if (IsEmpty(Q))
{ printf(“Empty “)
return;
}
else if (front == rear )
{ front=-1;
rear=-1;
}
else
front = (front +1) %Maxsize;

}
Dr. R. Jothi, VIT Chennai
Circular Queue Applications
 CPU Process scheduling
 Memory Management

Dr. R. Jothi, VIT Chennai


Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b)
 Enqueue(Q, c)
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b)
 Enqueue(Q, c) 0 1 2 3 4
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b) a
 Enqueue(Q, c) r,f
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b) a b
 Enqueue(Q, c) f r
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b) a b c
 Enqueue(Q, c) f r
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b) a b c d
 Enqueue(Q, c) f r
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b) b c d
 Enqueue(Q, c) f r
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b) c d
 Enqueue(Q, c) f r
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b) c d e
 Enqueue(Q, c)
f r
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b) d e
 Enqueue(Q, c)
f r
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b) g d e
 Enqueue(Q, c)
r f
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-1
Consider a circular queue Q with Maxsize as 5. Starting from empty
queue, show the result of following sequence of operations on Q.
What are the positions of rear and front after the last operation?
 Enqueue(Q, a)
 Enqueue(Q, b) g e
 Enqueue(Q, c)
r f
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q) Dr. R. Jothi, VIT Chennai
Exercise-2
Consider a circular queue Q with Maxsize as 3. Starting from empty queue, show the
result of following sequence of operations on Q. What are the elemenets present in the
Q after last operation? If any operation results in overflow or underflow , you just have
to discard the current operation, maintain the previous stage and continue further.
 Enqueue(Q, a)
 Enqueue(Q, b)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, c)
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q)
Dr. R. Jothi, VIT Chennai
Exercise-2
Consider a circular queue Q with Maxsize as 3. Starting from empty queue, show
the result of following sequence of operations on Q. What are the elemenets
present in the Q after last operation?
 Enqueue(Q, a)
 Enqueue(Q, b)
 Dequeue(Q) a
 Dequeue(Q)
r,f
 Enqueue(Q, c)
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q)
Dr. R. Jothi, VIT Chennai
Exercise-2
Consider a circular queue Q with Maxsize as 3. Starting from empty queue, show
the result of following sequence of operations on Q. What are the elemenets
present in the Q after last operation?
 Enqueue(Q, a)
 Enqueue(Q, b)
 Dequeue(Q) a b
 Dequeue(Q)
r r
 Enqueue(Q, c)
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q)
Dr. R. Jothi, VIT Chennai
Exercise-2
Consider a circular queue Q with Maxsize as 3. Starting from empty queue, show
the result of following sequence of operations on Q. What are the elemenets
present in the Q after last operation?
 Enqueue(Q, a)
 Enqueue(Q, b)
 Dequeue(Q) b
 Dequeue(Q)
r, f
 Enqueue(Q, c)
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q)
Dr. R. Jothi, VIT Chennai
Exercise-2
Consider a circular queue Q with Maxsize as 3. Starting from empty queue, show
the result of following sequence of operations on Q. What are the elemenets
present in the Q after last operation?
 Enqueue(Q, a)
 Enqueue(Q, b)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, c)
 Enqueue(Q, d)
 Dequeue(Q) Q becomes empty
 Dequeue(Q) r=-1
 Enqueue(Q, e) f=-1
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q)
Dr. R. Jothi, VIT Chennai
Exercise-2
Consider a circular queue Q with Maxsize as 3. Starting from empty queue, show
the result of following sequence of operations on Q. What are the elemenets
present in the Q after last operation?
 Enqueue(Q, a)
 Enqueue(Q, b)
 Dequeue(Q) c
 Dequeue(Q)
r,f
 Enqueue(Q, c)
 Enqueue(Q, d)
 Dequeue(Q)
 Dequeue(Q)
 Enqueue(Q, e)
 Dequeue(Q)
 Enqueue(Q, g)
 Dequeue(Q)
Dr. R. Jothi, VIT Chennai

You might also like