SlideShare a Scribd company logo
UNIT II LINEAR DATA
STRUCTURES – STACKS AND
QUEUES
TYPES OF QUEUES-
CIRCULAR QUEUE
PRIORITY QUEUE
Why?
• Implementation of a linear queue brings the drawback of
memory wastage
• However, memory is a crucial resource that you should always
protect by analyzing all the implications while designing
algorithms or solutions.
• In the case of a linear queue, when the rear pointer reaches the
MaxSize of a queue, there might be a possibility that after a
certain number of dequeue() operations, it will create an empty
space at the start of a queue.
• Additionally, this newly created empty space can never be re-
utilized as the rear pointer reaches the end of a queue. Hence,
experts introduced the concept of the circular queue to
overcome this limitation.
• As shown in the figure,
the rear pointer arrives at the beginning
of a queue with the help of a circular
link to re-utilize the empty space to
insert a new element.
• This simple addition of a circular
link resolves the problem of memory
wastage in the case of queue implementation. Thus, this
particular type of queue is considered the best version of a queue
data structure.
What is Circular Queue in a Data
Structure?
• A circular queue is an extended version of a linear queue as it
follows the First In First Out principle with the exception that
it connects the last node of a queue to its first by forming a
circular link.
• Hence, it is also
called a Ring Buffer.
• The circular queue
resolves the memory
wastage problem with
the help of a circular
link.
• Now you will understand how you can achieve circular
incrementation, with the help of an example.
• Let’s say the MaxSize of your queue is 5, and the rear pointer
has already reached the end of a queue.
• There is one empty space at the beginning of a queue, which
means that the front pointer is pointing to location 1.
Operations on Circular Queue:
• Front: Get the front item from queue.
• Rear: Get the last item from queue.
• enQueue(value) This function is used to insert an element into the circular
queue. In a circular queue, the new element is always inserted at Rear position.
– Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) ||
(rear == front-1)).
– If it is full then display Queue is full. If queue is not full then, check if (rear
== SIZE – 1 && front != 0) if it is true then set rear=0 and insert element.
• deQueue() This function is used to delete an element from the circular queue.
In a circular queue, the element is always deleted from front position.
– Check whether queue is Empty means check (front==-1).
– If it is empty then display Queue is empty. If queue is not empty then step 3
– Check if (front==rear) if it is true then set front=rear= -1 else check if
(front==size-1), if it is true then set front=0 and return the element.
Insertion in Circular Queue
Step 1 : If FRONT = (REAR + 1) % MAXSIZE : then
Write : “Queue Overflow” and return.
[End of If structure]
Step 2 : Read NUM to be inserted in Circular Queue.
Step 3 : If FRONT= -1 : then
Set FRONT = REAR =0.
Else Set REAR=(REAR + 1) % MAXSIZE.
[End of If Else structure]
Step 4 : Set CQUEUE[REAR]=NUM;
Step 5 : Exit
Deletion of Circular Queue
Step 1 : If FRONT = - 1 : then
Write : “Queue Underflow” and return.
[End of If Structure]
Step 2 : Set NUM = CQUEUE[FRONT].
Step 3 : Write ‘Deleted element from circular queue is :
",NUM.
Step 4 : If FRONT = REAR : then
Set FRONT = REAR = -1;
Else Set FRONT = (FRONT + 1) % MAXSIZE.
Step 5 : Exit
Applications of Circular Queue
The circular Queue can be used in the following scenarios:
• Memory management: The circular queue provides memory
management. As we have already seen that in linear queue, the
memory is not managed very efficiently. But in case of a circular
queue, the memory is managed efficiently by placing the elements in
a location which is unused.
• CPU Scheduling: The operating system also uses the circular queue
to insert the processes and then execute them.
• Traffic system: In a computer-control traffic system, traffic light is
one of the best examples of the circular queue. Each light of traffic
light gets ON one by one after every interval of time. Like red light
gets ON for one minute then yellow light for one minute and then
green light. After green light, the red light gets ON.
PRIORITY QUEUE
USAGE SCENARIOS
A theatre with seats. The catch is that the first row alone has royal seats, while
the other rows have normal seats, The royal seats are for special or VIP
visitors. The normal seats, on the other hand, are for normal people. Here, all
the people enter in a queue, but only the prioritized members get the front
seats. The other people have to sit behind all the priority members. In the same
way, a priority queue maintains priority for each element. It works as a queue
with applied priority.
What is 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 get added or removed is decided by the priority of
the element.
• Following rules are applied to maintain a priority queue
(1) The element with a higher priority is processed before any element of
lower priority.
(2) If there are elements with the same priority, then the element added
first in the queue would get processed.
• Basically, in priority queues, the data does not follow the FIFO
order. The order of data is by the order of their priority. So, the
data gets into the queue, gets recorded based on priority, and
then comes out.
• Generally, the order of priority will be from lower to higher,
that is, element with priority value ‘0’ will come first. This can
be changed based on design.
• 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 queues is simulation systems
where priority corresponds to event times.
Implementation
• Priority queue can be implemented using an array, a
linked list, a heap data structure, or a binary search
tree.
• Among these data structures, heap data structure
provides an efficient implementation of priority
queues.
• The access modes of the priority queue are the same
as normal queue. ‘Enqueue’ is used to send data unit
in, ‘Dequeue’ is used to take data unit out.
DEMO
• https://www.studytonight.com/code/python/
ds/priority-queue-in-python.php
# A simple implementation of Priority Queue
# using Queue.
class PriorityQueue(object):
def __init__(self):
self.queue = []
def __str__(self):
return ' '.join([str(i) for i in self.queue])
# for checking if the queue is empty
def isEmpty(self):
return len(self.queue) == 0
# for inserting an element in the queue
def insert(self, data):
self.queue.append(data)
# for popping an element based on Priority
def delete(self):
try:
max = 0
for i in range(len(self.queue)):
if self.queue[i] > self.queue[max]:
max = i
item = self.queue[max]
del self.queue[max]
return item
except IndexError:
print()
exit()
if __name__ == '__main__':
myQueue = PriorityQueue()
myQueue.insert(12)
myQueue.insert(1)
myQueue.insert(14)
myQueue.insert(7)
print(myQueue)
while not myQueue.isEmpty():
print(myQueue.delete())
APPLICATIONS OF QUEUES :
1. Round Robin technique for processor scheduling is
implemented using queues.
2. All types of customer service (like railway ticket
reservation ) center software’s are designed using
queues to store customers information.

More Related Content

What's hot

Queue
QueueQueue
Queues
QueuesQueues
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
Dabbal Singh Mahara
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
Farhanum Aziera
 
QUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING CQUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING C
Meghaj Mallick
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
Mekk Mhmd
 
Queue
QueueQueue
Queue
QueueQueue
Queue
Raj Sarode
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
Queue by rajanikanth
Queue by rajanikanthQueue by rajanikanth
Queue by rajanikanth
btechsmartclass
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
Tribhuvan University
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 
Queue - Operations and Implementations
Queue - Operations and ImplementationsQueue - Operations and Implementations
Queue - Operations and Implementations
Sagacious IT Solution
 
Queues
QueuesQueues
Queues
Hareem Aslam
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Queue
QueueQueue

What's hot (20)

Queue
QueueQueue
Queue
 
Queues
QueuesQueues
Queues
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
QUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING CQUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING C
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
Queue by rajanikanth
Queue by rajanikanthQueue by rajanikanth
Queue by rajanikanth
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Queue - Operations and Implementations
Queue - Operations and ImplementationsQueue - Operations and Implementations
Queue - Operations and Implementations
 
Queues
QueuesQueues
Queues
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue
QueueQueue
Queue
 

Similar to Unit ii linear data structures

queue.pptx
queue.pptxqueue.pptx
queue.pptx
Dr.Shweta
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
abinathsabi
 
Circular queue
Circular queueCircular queue
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
Ddushb
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
iqbalphy1
 
08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex
SadiaSharmin40
 
Queue
QueueQueue
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
VeerannaKotagi1
 
Queue
QueueQueue
Unit 5 dsa QUEUE
Unit 5 dsa QUEUEUnit 5 dsa QUEUE
10994103.ppt
10994103.ppt10994103.ppt
10994103.ppt
SushmaG48
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
Mehedi Hasan
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
LECO9
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
SKUP1
 
UNIT-2.pptx
UNIT-2.pptxUNIT-2.pptx
UNIT-2.pptx
ChiragSuresh
 
Queues
Queues Queues
Queues
nidhisatija1
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Afaq Mansoor Khan
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
Dr.Umadevi V
 
cp264_lecture18_queue.ppt
cp264_lecture18_queue.pptcp264_lecture18_queue.ppt
cp264_lecture18_queue.ppt
ssuserff72e4
 

Similar to Unit ii linear data structures (20)

queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
 
Circular queue
Circular queueCircular queue
Circular queue
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex
 
Queue
QueueQueue
Queue
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
Queue
QueueQueue
Queue
 
Unit 5 dsa QUEUE
Unit 5 dsa QUEUEUnit 5 dsa QUEUE
Unit 5 dsa QUEUE
 
10994103.ppt
10994103.ppt10994103.ppt
10994103.ppt
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
UNIT-2.pptx
UNIT-2.pptxUNIT-2.pptx
UNIT-2.pptx
 
Queues
Queues Queues
Queues
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
 
cp264_lecture18_queue.ppt
cp264_lecture18_queue.pptcp264_lecture18_queue.ppt
cp264_lecture18_queue.ppt
 

More from LavanyaJ28

Cs1301 syllabus
Cs1301  syllabusCs1301  syllabus
Cs1301 syllabus
LavanyaJ28
 
Ds important questions
Ds important questionsDs important questions
Ds important questions
LavanyaJ28
 
2 marks- DS using python
2 marks- DS using python2 marks- DS using python
2 marks- DS using python
LavanyaJ28
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sorting
LavanyaJ28
 
Hashing
HashingHashing
Hashing
LavanyaJ28
 
Graphs
GraphsGraphs
Graphs
LavanyaJ28
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
LavanyaJ28
 
Heap types & Trees
Heap types & TreesHeap types & Trees
Heap types & Trees
LavanyaJ28
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
LavanyaJ28
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
LavanyaJ28
 
Stack and queue
Stack and queueStack and queue
Stack and queue
LavanyaJ28
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked list
LavanyaJ28
 
Unit 1 Basic concepts to DS
Unit 1 Basic concepts to DSUnit 1 Basic concepts to DS
Unit 1 Basic concepts to DS
LavanyaJ28
 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementation
LavanyaJ28
 
Unit 1 polynomial manipulation
Unit 1   polynomial manipulationUnit 1   polynomial manipulation
Unit 1 polynomial manipulation
LavanyaJ28
 
Unit 1 abstract data types
Unit 1 abstract data typesUnit 1 abstract data types
Unit 1 abstract data types
LavanyaJ28
 

More from LavanyaJ28 (16)

Cs1301 syllabus
Cs1301  syllabusCs1301  syllabus
Cs1301 syllabus
 
Ds important questions
Ds important questionsDs important questions
Ds important questions
 
2 marks- DS using python
2 marks- DS using python2 marks- DS using python
2 marks- DS using python
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sorting
 
Hashing
HashingHashing
Hashing
 
Graphs
GraphsGraphs
Graphs
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
Heap types & Trees
Heap types & TreesHeap types & Trees
Heap types & Trees
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked list
 
Unit 1 Basic concepts to DS
Unit 1 Basic concepts to DSUnit 1 Basic concepts to DS
Unit 1 Basic concepts to DS
 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementation
 
Unit 1 polynomial manipulation
Unit 1   polynomial manipulationUnit 1   polynomial manipulation
Unit 1 polynomial manipulation
 
Unit 1 abstract data types
Unit 1 abstract data typesUnit 1 abstract data types
Unit 1 abstract data types
 

Recently uploaded

一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
zwunae
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
Kamal Acharya
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 

Recently uploaded (20)

一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 

Unit ii linear data structures

  • 1. UNIT II LINEAR DATA STRUCTURES – STACKS AND QUEUES TYPES OF QUEUES- CIRCULAR QUEUE PRIORITY QUEUE
  • 2. Why? • Implementation of a linear queue brings the drawback of memory wastage • However, memory is a crucial resource that you should always protect by analyzing all the implications while designing algorithms or solutions. • In the case of a linear queue, when the rear pointer reaches the MaxSize of a queue, there might be a possibility that after a certain number of dequeue() operations, it will create an empty space at the start of a queue.
  • 3. • Additionally, this newly created empty space can never be re- utilized as the rear pointer reaches the end of a queue. Hence, experts introduced the concept of the circular queue to overcome this limitation. • As shown in the figure, the rear pointer arrives at the beginning of a queue with the help of a circular link to re-utilize the empty space to insert a new element. • This simple addition of a circular link resolves the problem of memory wastage in the case of queue implementation. Thus, this particular type of queue is considered the best version of a queue data structure.
  • 4. What is Circular Queue in a Data Structure? • A circular queue is an extended version of a linear queue as it follows the First In First Out principle with the exception that it connects the last node of a queue to its first by forming a circular link. • Hence, it is also called a Ring Buffer. • The circular queue resolves the memory wastage problem with the help of a circular link.
  • 5. • Now you will understand how you can achieve circular incrementation, with the help of an example. • Let’s say the MaxSize of your queue is 5, and the rear pointer has already reached the end of a queue. • There is one empty space at the beginning of a queue, which means that the front pointer is pointing to location 1.
  • 6. Operations on Circular Queue: • Front: Get the front item from queue. • Rear: Get the last item from queue. • enQueue(value) This function is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at Rear position. – Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)). – If it is full then display Queue is full. If queue is not full then, check if (rear == SIZE – 1 && front != 0) if it is true then set rear=0 and insert element. • deQueue() This function is used to delete an element from the circular queue. In a circular queue, the element is always deleted from front position. – Check whether queue is Empty means check (front==-1). – If it is empty then display Queue is empty. If queue is not empty then step 3 – Check if (front==rear) if it is true then set front=rear= -1 else check if (front==size-1), if it is true then set front=0 and return the element.
  • 7. Insertion in Circular Queue Step 1 : If FRONT = (REAR + 1) % MAXSIZE : then Write : “Queue Overflow” and return. [End of If structure] Step 2 : Read NUM to be inserted in Circular Queue. Step 3 : If FRONT= -1 : then Set FRONT = REAR =0. Else Set REAR=(REAR + 1) % MAXSIZE. [End of If Else structure] Step 4 : Set CQUEUE[REAR]=NUM; Step 5 : Exit
  • 8. Deletion of Circular Queue Step 1 : If FRONT = - 1 : then Write : “Queue Underflow” and return. [End of If Structure] Step 2 : Set NUM = CQUEUE[FRONT]. Step 3 : Write ‘Deleted element from circular queue is : ",NUM. Step 4 : If FRONT = REAR : then Set FRONT = REAR = -1; Else Set FRONT = (FRONT + 1) % MAXSIZE. Step 5 : Exit
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. Applications of Circular Queue The circular Queue can be used in the following scenarios: • Memory management: The circular queue provides memory management. As we have already seen that in linear queue, the memory is not managed very efficiently. But in case of a circular queue, the memory is managed efficiently by placing the elements in a location which is unused. • CPU Scheduling: The operating system also uses the circular queue to insert the processes and then execute them. • Traffic system: In a computer-control traffic system, traffic light is one of the best examples of the circular queue. Each light of traffic light gets ON one by one after every interval of time. Like red light gets ON for one minute then yellow light for one minute and then green light. After green light, the red light gets ON.
  • 19. USAGE SCENARIOS A theatre with seats. The catch is that the first row alone has royal seats, while the other rows have normal seats, The royal seats are for special or VIP visitors. The normal seats, on the other hand, are for normal people. Here, all the people enter in a queue, but only the prioritized members get the front seats. The other people have to sit behind all the priority members. In the same way, a priority queue maintains priority for each element. It works as a queue with applied priority.
  • 20. What is 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 get added or removed is decided by the priority of the element. • Following rules are applied to maintain a priority queue (1) The element with a higher priority is processed before any element of lower priority. (2) If there are elements with the same priority, then the element added first in the queue would get processed.
  • 21. • Basically, in priority queues, the data does not follow the FIFO order. The order of data is by the order of their priority. So, the data gets into the queue, gets recorded based on priority, and then comes out. • Generally, the order of priority will be from lower to higher, that is, element with priority value ‘0’ will come first. This can be changed based on design. • 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 queues is simulation systems where priority corresponds to event times.
  • 22. Implementation • Priority queue can be implemented using an array, a linked list, a heap data structure, or a binary search tree. • Among these data structures, heap data structure provides an efficient implementation of priority queues. • The access modes of the priority queue are the same as normal queue. ‘Enqueue’ is used to send data unit in, ‘Dequeue’ is used to take data unit out.
  • 24. # A simple implementation of Priority Queue # using Queue. class PriorityQueue(object): def __init__(self): self.queue = [] def __str__(self): return ' '.join([str(i) for i in self.queue]) # for checking if the queue is empty def isEmpty(self): return len(self.queue) == 0 # for inserting an element in the queue def insert(self, data): self.queue.append(data) # for popping an element based on Priority def delete(self): try: max = 0 for i in range(len(self.queue)): if self.queue[i] > self.queue[max]: max = i item = self.queue[max] del self.queue[max] return item except IndexError: print() exit() if __name__ == '__main__': myQueue = PriorityQueue() myQueue.insert(12) myQueue.insert(1) myQueue.insert(14) myQueue.insert(7) print(myQueue) while not myQueue.isEmpty(): print(myQueue.delete())
  • 25. APPLICATIONS OF QUEUES : 1. Round Robin technique for processor scheduling is implemented using queues. 2. All types of customer service (like railway ticket reservation ) center software’s are designed using queues to store customers information.