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

Queue 3

Uploaded by

Hare Krishna Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Queue 3

Uploaded by

Hare Krishna Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

APEX INSTITUTE OF TECHNOLOGY

Bachelor of Engineering (Computer Science &


Engineering)

Subject: Data Structure


Subject Code: CSH-231
Chapter: Queue
Subject Coordinator:
Mr. Vishal Kumar
(E12820)

Queue DISCOVER . LEARN .


Lecture No. 2.3.3 EMPOWER
Index

• Operations on queue
• Enqueue

• Dequeue

• Priority queue

2 2
OPERATIONS ON QUEUE :
• What operations we can perform on queue is called
ADT of Queue.
(ADT : Abstract Data Type)
• ADT of Queue :
(1) Enqueue ( )
(2) Dequeue ( )
We perform two operations on queue, they are
Enqueue and Dequeue.
(1) Enqueue ( ) :
Using enqueue an element is inserted into the
queue.
To insert ‘n’ elements there are ‘n’ enqueue()
operations needed.
(2) Dequeue ( ) :
Using dequeue an element is deleted from the
queue.
To delete ‘n’ elements from the queue there are ‘n’
dequeue() operations needed.
For Example
• Consider an empty queue of size 6,

Enqueue (a) : Will insert ‘a’ at 0.


Enqueue (b) : Will insert b at 1 and so on.
Dequeue ( ) : Will delete a by default because it was
inserted first and so on.
Implementation of Queue
• We can implement queue by using other data structures
like using array, stack or linked list etc.
Implementation of Queue using Array :
Implementing queue means implementing basic
operations of queue by using array.
• So, let’s take an array first ;
• Array is denoted with ‘q’ which represents a queue.
• ‘S’ is the size of array, ‘q’.
• Front is represented with ‘F’.
• Rear is represented with ‘R’.
Enqueue Operation:
Dequeue Operation:
Assumptions :
• Array positions starts from zero i.e.

• Initially, when there is no any item in queue, F = –1 and R = –1.


• If at any stage F = R, there is only one element in the queue.
• In general, ‘R’ increments by one for each insertion.
• In general, ‘F’ increments by one for each deletion.
• So, queue can be represented by using
array as :

Here, S = 8 i.e. size of queue.


Implementation of Queue Using
Linked List
• A brief note on Linked List :
Linked List is a data structure which is in the form of list of nodes
as shown in the diagram below :

A node contains two fields :


(1) An information field (we can use integer data or char data etc.)
(2) Next address field
• An external pointer ‘V’ that points to the fi rst node and we
can access entire list by this external pointer. ‘V’ is a pointer
variable which contains address of the first node of Linked List.
• Implementing queue using linked list means, performing
Enqueue and Dequeue operations on linked list to satisfy the
condition of First In First Out, which is of queue’s property.
• Defi nition of two operations of queue using linked list :
(1) Enqueue/Insert Operation : It means we need to add a
node at the end of a given linked list.
(2) Dequeue/Remove Operation : It means we need to delete
a node from starting of a given linked list.
Let’s understand with an example :
Consider a linked list :

(1) Enqueue/Insert Operation :


Steps : I – Create a node
II – Attach it to end of linked list and so on
Code for Enqueue Operation :
• Continue
(2) Dequeue/Remove
Operation :
Steps : I – Just remove the fi rst node, second node
and so on.
Code for Dequeue Operation :
• So, now list will appear as :

• Thus, we can perform Insert/Remove operation of


queue using linked list.
Priority queue
In Priority Queue, every element is associated with
some priority and according to priority an element in
the queue is processed, especially on deletion time.
So, while inserting elements on the queue, we can
follow any order but while deleting, we consider the
requirement.
• Types of Priority Queue :
(1) Ascending Priority Queue
(2) Descending Priority Queue
(1) Ascending Priority Queue :
• We insert items arbitrarily in the queue.
• While deleting, on fi rst deletion, it gives the
smallest item of all and so on.
• For example : items are 3, 9, 2, 0, 11, 10, 20
• Insertion and elements follow as :
• Deletion of elements follow as :

• Thus, by ascending priority queue, we get items of the queue in


ascending order.
(2) Descending Priority Queue :
• Insertion of items on queue follows an arbitrary
order.
• Deletion follows as : On fi rst deletion it gives the
largest item of all and so on.
• For example : Items are 3, 9, 2, 0, 11, 10, 20.
• Insertion of items follow as :
• Deletion of elements/items follow as :

Thus, by descending priority queue, we get items of the


queue in descending order.
References
WEB LINKS
• https://www.geeksforgeeks.org/data-structures/
• https://www.javatpoint.com/data-structure-tutoria
l
• https://www.tutorialspoint.com/data_structures_al
gorithms/index.htm
VIDEO LINK
• https://www.youtube.com/watch?v=AT14lCXuMKI
&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU
Research Paper
• https://books.google.co.in/books?id=S-tXjl1hsUYC
THANK YOU

For queries
Email: [email protected]

25

You might also like