0% found this document useful (0 votes)
19 views66 pages

Updated Slides of Queue Data Structure (Fall-2023)

The document provides an overview of queue data structures, highlighting their definition, operations, and differences from stacks. It covers basic queue operations such as enqueue and dequeue, as well as implementations using static and dynamic arrays, including circular queues. Additionally, it discusses problems related to array implementations and solutions for efficient queue management.

Uploaded by

xomami5714
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)
19 views66 pages

Updated Slides of Queue Data Structure (Fall-2023)

The document provides an overview of queue data structures, highlighting their definition, operations, and differences from stacks. It covers basic queue operations such as enqueue and dequeue, as well as implementations using static and dynamic arrays, including circular queues. Additionally, it discusses problems related to array implementations and solutions for efficient queue management.

Uploaded by

xomami5714
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/ 66

Queue Data

structure

Course Instructor:
Inayat-ur-Rehman, PhD
COMSATS University, Islamabad

1
WORDS OF WISDOM

COURSE INSTRUCTOR: DR. INAYAT-UR-REHMAN 2


OVERVIEW

Review of What is
Topics Stack vs
Last Topic : Queue
Covered Queue?
STACK (FIFO)?

Operations of What is
Operations of
Queue Circular
Circular
(Enqueue, Queue
Queue
Dequeue) (FIFO)?

LECTURE SLIDES BY: DR. INAYAT-UR-REHMAN 3


TOPICS COVERED

LECTURE SLIDES BY: DR. INAYAT-UR-REHMAN 4


REVIEW OF LAST TOPIC (STACK)

A stack is used to store elements


where the Last element In is the
First one Out (LIFO).
Common operations are:
• Push()
• Pop()
• Is_empty()
• Is_full()
• Top()

LECTURE SLIDES BY: DR. INAYAT-UR-REHMAN 5


WHAT IS QUEUE?
Definition:
Queue is an Abstract data structure Dequeue() Equeue()
that follow the First In First Out
(FIFO) i.e. the first element that is
entered in the queue is the first one
to be served.
Daily life example:
a line of people waiting for their turn
at a bank or
a line of vehicles waiting for token at
a tool plaza or
a line of customers at cash and carry
to pay their bills. Reference: codesdope.
com
LECTURE SLIDES BY: DR. INAYAT-UR-REHMAN 6
QUEUE AT BUS STOP

Front rear rear rear Rear


rear
QUEUE AT BUS STOP

Front Rear
rear
rear
QUEUE AT BUS STOP

Front
Rear
rear
QUEUE AT BUS STOP

rear
Front Rear
STACK VS QUEUE
APPLICATIONS OF QUEUES IN CS

Shared Printer
Operating system Communication Software
BASIC OPERATIONS OF QUEUE
 Enqueue – insert at end
 Dequeue – delete from front
 Is_empty()
 Is_full()
 Front()

LECTURE SLIDES BY: DR. INAYAT-UR-REHMAN 13


IMPLEMENTATION

Static Dynamic
Queue is implemented by an A queue can be implemented
array, and size of queue as a linked list, and expand
remains fix or shrink with each enqueue
or dequeue operation.
STATIC QUEUE IMPLEMENTATION

Front
First Element
When queue is empty both front and rear are set to -1
Second While enqueueing increment rear by 1, and while
Element
.
.
dequeueing increment front by 1
When there is only one value in the Queue, both rear
Rear and front have same index (0)
Last Element

maxlength
IMPLEMENTATION OF QUEUES

1. public class Que //why class name is not


queue
2. {
3. public :
4. int size; // default capacity
5. int *q; // array that holds queue
elements
6. int front; // index of front of queue
7. int rear; // index of rear of
queue
8.
CONSTRUCTOR
1. Que() //default constructor
2. {
3. size = 10; //default size
4. q = new int[size]; // run time size allocation
5. front=-1; // initially front and rear are
at -1
6. rear=-1;
7. }
8. Que(int x) //overloaded constructor
9. {
10. size = x; //user given size
11. q = new int[size]; // run time size allocation
12. front=-1; // initially front and rear are
at -1
13. rear=-1;
EMPTY OPERATION
bool is_empty()
1. {
2. if (rear==-1)
3. return true;
4. else
5. return false;
6. }
FULL OPERATION
1. bool is_full()
2. {
3. if (rear==SIZE-1)
4. return true;
5. else
6. return false ;
7. }
8. Void display()
9. {
10. if(is_empty()) { cout<<“\n Queue is
empty….”; }
11.Else{
12. for(int i=front; i<=rear; i++)
13. {
14. cout<<“\n Value at index “<<i<< “ is:
“<<q[i];
ENQUEUE OPERATION
1. void Enqueue(int x)
2. {
3. if (is_full())
4. {
5. cout<<“No space “;
6. }
7. else {
8. if(is_empty())
9. {
10. front=rear=0;
11. }

12. else{
13. rear++;
14. }
15. q[rear]=x;
16. }
17. }
DEQUEUE OPERATION
1. int Dequeue()
2. {
3. if (is_empty())
4. {
5. cout<<“Queue is already empty”;
6. return -1;
7. }
8. else
9. {
10. int x= q[front];
11. if(front==rear)
12. {
13. front=rear=-1;
14. }else{

15. front++;
16. }
17. return x;
18. }
MAIN METHOD
1. Void main ()
2. {
3. Que q1; //Default constructor: object of que class with default
size
4. Que q2(5); //Overloaded constructor: object of que class (q2) with
size 5
5. Que q3(15);
6. q1.enqueue(5);
7. q2.enqueue(7);
8. q3.enqueue(6);
9. int x= q2.dequeue();
10.Cout<<“Dequeued element from q3 is: “<<x;
Shifting values/Quiz
4 5 7 2
0 1 2 3 4 5 6 7
Front=0 Rear = 3

5 7 2 2
0 1 2 3 4 5 6 7

After Dequeue();
Front=0 Rear = 2
Value at index 3 is now garbage as it is repeated, so
after new equeue() new value will be at index 3.
Hence value 2 at index 3 will be overwritten.
DYNAMIC QUEUE IMPLEMENTATION
PROBLEMS RELATED TO ARRAY
IMPLEMENTATION OF QUEUES

• Using array to implement queue introduces


the possibility of overflow if the queue grows
larger than the size of the array.
• Now, we study other problems that arise in the
linear implementation of queue.
CONT….
Elements 7 5 2
Indexes 0 1 2 3 4 5

Front = 3 Rear = 5

What if we call Enqueue (3)? Queue is full

but the queue is not full

Question:
What to do now?
SOLUTIONS
• Shift the elements while dequeuing
– Not efficient solution
• Implement by Circular Queue
Circular Queue
CIRCULAR IMPLEMENTATION
• We can see that , in this implementation the
first element immediately follows last
element.
• Even if the last element is occupied , we can
insert new element in the first position, as
long as the first element is empty.
• In this implementation , we have an additional
variable count , to keep track of the number of
elements in the queue.
SOLUTION: USING CIRCULAR QUEUE
• If the queue is not full and rear is at size-1?
if(rear == queueSize-1)
rear = 0;
else
rear++;
• Or use module arithmetic
rear = (rear + 1) % queueSize;
EXAMPLE-1
When the queue is empty: front=rear=-1;
After enqueuer(8) front and rear will move to index 0.
front

Size - 1 rear
0

Move both front


6 8 1
and rear at index 0.

5 2

4 3
CIRCULAR ARRAY
To enqueue, advance the REAR pointer, and insert in its
new position.

Now the rear was at index 3 Size - 1 0


and after enqueue(9) rear 6
moved by 1 front
4
R=(r+1)%queue_size
so 5
1 2
R= (3+1)%8 = 4 9 7
4 3
rear
CIRCULAR ARRAY
• Consider this case:
Rear
Now the rear was at index 7
(size-1) and front is at index
3. If we enqueue(5) rear Size - 1 0
should move at index 0 by 6 1
formula: 1
4
R=(r+1)% queue_size
so 5
2
2
R= (7+1)%8 = 0 9 7
4 3 front
CIRCULAR ARRAY
• Consider this case:
Rear Rear
Now the rear was at index 7
(size-1) and front is at index
3. If we enqueue(5) rear Size - 1 0
should move at index 0 by 6 1 5
formula: 1
4
R=(r+1)% queue_size
so 5
2
2
R= (7+1)%8 = 0 9 7
4 3 front
CIRCULAR ARRAY
Advance the front to dequeue an item.

Size - 1 0 front
To dequeue the element, we
move the front to new 6 2 1
location by: 4
F=(f+1)%queue_size 1
5 2
so 7
F= (0+1)%8 = 1 Rear
4 3
EMPTY

Size - 1 0

In the case of a single item, 6 1


the front and back point to
the same index. So after
deletion front and rear will 5 2
9
have value -1.
4 3
front
Rear
FULL CASE-1

Rear front

Size - 1 0
Front = 0 and Rear = size-1 6 9 2 1
7 4

4 6 2
5 5 7
4 3
FULL CASE-2

Size - 1 0
6 1 7 1
count = size;
if(Rear+1)%queue_size = = front 4 4 Full !!
2 3 2
5 9 7
front
4 3
Rear
ENQUEUE
EXAMPLE-2 OPERATION

0 1 2 3 4 5 6 7 8
Front= -1
Rear= -1
Enqueue 9 As the queue is empty so we move front and rear to 0

9
0 1 2 3 4 5 6 7 8
Front=0
Rear=0

Enqueue 3 Rear=(Rear+1) mod Queue Size = (0+1) mod 9 = 1

9 3
0 1 2 3 4 5 6 7 8
Front=0
9 5 1 6
0 1 2 3 4 5 6 7 8
Front=5 Display Queue
Rear=8

Enqueue 4 Rear=(Rear+1) mod Queue Size = (8+1) mod 9 = 0 int p=front;


if(p==-1)
4 9 5 1 6 cout<<“Empty”;
0 1 2 3 4 5 6 7 8 else{
do{
Front=5
Rear=0 cout<<que[p];
P=p+1%que_size;
}while(p!
Enqueue 0 Rear=(Rear+1) mod Queue Size = (0+1) mod 9 = 1 =rear+1%que_size);
4 0 9 5 1 6
0 1 2 3 4 5 6 7 8
Front=5
Rear=1
QUEUE FULL

Enqueue 8 Rear=(Rear+1) mod Queue Size = (4+1) mod 9 = 5

3 4 2 3 9 7 6 5 1
0 1 2 3 4 5 6 7 8
Front=5
Rear=4

if(rear+1%queue_size==front) – FULL

(4+1)%9 = = 5
DEQUEUE OPERATION

7 6 5 1
0 1 2 3 4 5 6 7 8
Front=5
Rear=8

Dequeue() Front=(Front+1) mod Queue Size = (5+1) mod 9 = 6


6 5 1
0 1 2 3 4 5 6 7 8
Front=6
Rear=8
DEQUEUE OPERATION

3 4 1
0 1 2 3 4 5 6 7 8
Front=8
Rear=1

Dequeue() Front=Front+1) mod Queue Size = (8+1) mod 9 = 0

3 4
0 1 2 3 4 5 6 7 8
Front=0
Rear=1
DEQUEUE OPERATION

1
0 1 2 3 4 5 6 7 8
Front=8
Rear=8

Dequeue() Front= = Rear

0 1 2 3 4 5 6 7 8
Front= -1 QUEUE
Rear= -1
BECOMES
EMPTY
void Equeue(int x )
{
if(Is_full())
{
cout<<“Queue is already full”;
}
else{
if(IsEmpty())
{
front=rear=0;
}
else
{
Rear=(Rear+1) % QueueSize;
}
que[rear]=x;
}
int Dequeue( )
{
if (IsEmpty())
{
cout<<“Underflow”;
return -1;
}
else
{
int ReturnValue=Data[Front];
if (Front==Rear) //only one element in
the queue
Front=Rear=-1;
else
Front=(Front+1) % QueueSize;
return ReturnValue;
}
bool IsEmpty()
{
if (Front==-1)
return true;
else return false;
}

bool IsFull()
{
if (((Rear+1)%QueueSize)==Front)
return true;
else return false;
}
Double Ended Queue (Deque)

•It is also a homogeneous list of elements in which


insertion and deletion operations are performed from both
the ends.
•That is, we can insert elements from the rear end or from
the front ends.
•Hence it is called double-ended queue. It is commonly
referred as a Deque.
•There are two types of Deque. These two types are due to
the restrictions put to perform either the insertions or
deletions only at one end.
Deques
• A deque is a double-ended queue
– Insertions and deletions can occur at either end
– Implementation is similar to that for queues
– Deques are not heavily used

49
ADT Deque
• Need for an ADT which offers
– Add, remove, retrieve
– At both front and back of a queue
• Double ended queue
– Called a deque
– Pronounced “deck”
• Actually behaves more like a combination of Stack and Queue
ADT Deque
Double-Ended Queues (deque)( "D.Q.")
Supports insertion and deletion at both the front and the rear of the queue.
The Deque Abstract Data Type
• addFirst(e): Insert a new element e at the beginning of the deque.
• addLast(e): Insert a new element e at the end of the deque.
• removeFirst(): Remove and return the first element of the deque; an error occurs if
the deque is empty.
• removeLast(): Remove and return the last element of the deque; an error occurs if
the deque is empty.
• getFirst(): Return the first element of the deque; an error occurs if the deque is
empty.
• getLast(): Return the last element of the deque; an error occurs if the deque is empty.
• size(): Return the number of elements of the deque.
• isEmpty(): Determine if the deque is empty.
Double-Ended Queues (cont.)
• Operation Output D
• addFirst(3) - (3)
• addFirst(5) - (5,3)
• removeFirst() 5 (3)
• addLast(7) - (3,7)
• removeFirst() 3 (7)
• removeLast() 7 ()
• removeFirst() "error" ()
• isEmpty() true ()
Implementing a Deque

Doubly linked list:


Since the deque requires insertion and removal at both ends of a
list, using a singly linked list to implement a deque would be
inefficient.
Method Time
• size, isEmpty O(1)
• getFirst, getLast O(1)
• add First, addLast O(1)
• removeFirst, removeLast O(1)
Double Ended Queue (Deque)

• There are:
– Input-restricted Deque.
– Output-restricted Deque.
• Bellow show a figure a empty Deque Q[5] which
can accommodate five elements.

deletion insertion
insertion deletion
Q[0] Q[1] Q[2] Q[3] Q[4]

Fig: A Deque
Double Ended Queue (Deque)

 There are:
 Input-restricted Deque: An input restricted Deque

restricts the insertion of the elements at one end


only, the deletion of elements can be done at both
the end of a queue.

deletion 10 20 30 40 50 insertion
deletion
Q[0] Q[1] Q[2] Q[3] Q[4]

Fig: A representation of an input-restricted Deque


Double Ended Queue (Deque)

 There are:
 Output-restricted Deque: on the contrary, an

Output-restricted Deque, restricts the deletion of


elements at one end only, and allows insertion to
be done at both the ends of a Deque.

insertion 10 20 30 40 50 insertion
deletion
Q[0] Q[1] Q[2] Q[3] Q[4]

Fig: A representation of an Output-restricted Deque


Priority Queue

• A priority queue is a collection of elements where


the elements are stored according to their priority
levels.
• The order in which the elements should get added or
removed is decided by the priority or the element.
• Following rules are applied to maintain a priority
queue.
– The element with a higher priority is processes
before any element of lower priority.
Priority Queue
– If there are elements with same priority, then the
element added first in the queue would get
processed
• Here, smallest number that is most highest priority
and greater number that is less priority.
• Priority queues are used for implementing job
scheduling by the operating system.
• Where jobs with higher priorities are to be processed
first.
• Another application of priority queue is simulation
systems where priority corresponds to event times.
Priority queue
• A stack is first in, last out
• A queue is first in, first out
• A priority queue is least-first-out
– The “smallest” element is the first one removed
• (You could also define a largest-first-out priority queue)
• This means that in general the Priority Queue can be called as
Highest-Priority-Out
– Contrast bank queue and emergency room queue(s)
– If there are several “smallest” elements, the
implementer must decide which to remove first
• Remove any “smallest” element (don’t care which)
• Remove the first one added
61
class PriorityQApp
{
void main( )
{
PriorityQ thePQ = new PriorityQ(5);
thePQ.insert(30);
thePQ.insert(50);
thePQ.insert(10);
thePQ.insert(40);
thePQ.insert(20);

while( !thePQ.isEmpty() )
{
long item = thePQ.remove();
cout<<item<<“ “; // 10, 20, 30, 40, 50 Note: ORDERED!
} // end while
} // end main()
//-------------------------------------------------------------
} // end class PriorityQApp

62
Array implementations
• A priority queue could be implemented as an unsorted array (with a count
of elements)
– Adding an element would take O(1) time (why?)
– Removing an element would take O(n) time (why?)
– Hence, adding and removing an element takes O(n) time
– This is an inefficient representation
• A priority queue could be implemented as a sorted array (again,
with a count of elements)
– Adding an element would take O(n) time (why?)
– Removing an element would take O(1) time (why?)
– Hence, adding and removing an element takes O(n) time
– Again, this is inefficient
63
Linked list implementations
•A priority queue could be implemented as an unsorted linked list
– Adding an element would take O(1) time (why?)
– Removing an element would take O(n) time (why?)
•A priority queue could be implemented as a sorted linked list
– Adding an element would take O(n) time (why?)
– Removing an element would take O(1) time (why?)
•As with array representations, adding and removing an element takes O(n) time
– Again, these are inefficient implementations

– Efficient implementations of priority queues can be done using balanced binary tree
implementations …… we will see in the coming lectures.

64
References
• Dr. Yasir Faheem, COMSATS University, Islamabad
• C++ plus data structures / Nell Dale
• Data Structures and Algorithms in C++, Second Edition by
Adam Drozdek
• https://www.geeksforgeeks.org/

You might also like