SlideShare a Scribd company logo
1 of 43
Data Structure and
Algorithm
Using Python
Stack and Queue
04
TABLE OF
CONTENTS 04
Stacks 01
03
Queue
Circular Queue
05
Priority Queues
02
Parsing Arithmetic
Expressions
Introduction
4
Data structures have different kinds of
storage structures such us Arrays,
Stacks, queues, and priority queues .
Consider Arrays – as a data storage
structure:
1. very useful.
2. easy to insert into, delete from, and search for specific
items.
Stack
5
A stack is used to store data such that the last item inserted is the first
item removed. It is used to implement a last-in first-out (LIFO) type
protocol. The stack is a linear data structure in which new items are
added, or existing items are removed from the same end, commonly
referred to as the top of the stack.
Stacks are very common in computer science and are used in many types
of problems. Stacks also occur in our everyday lives. Consider a stack of
trays in a lunchroom. When a tray is removed from the top, the others
shift up. If trays are placed onto the stack, the others are pushed down.
Stack
6
A stack is used to store data such that the last item inserted is
the first item removed. It is used to implement a last-in first-out
(LIFO) type protocol. The stack is a linear data structure in which
new items are added, or existing items are removed from the
same end, commonly referred to as the top of the stack.
Stack
7
Stack
8
A stack is a data structure that stores a linear collection of items
with access limited to a last-in first-out order. Adding and
removing items is restricted to one end known as the top of the
stack. An empty stack is one containing no items.
• Stack(): Creates a new empty stack.
• isEmpty(): Returns a boolean value indicating if the stack is
empty.
• length (): Returns the number of items in the stack.
Stack
9
• pop(): Removes and returns the top item of the stack, if the
stack is not empty. Items cannot be popped from an empty
stack. The next item on the stack becomes the new top item.
• peek(): Returns a reference to the item on top of a non-empty
stack without removing it. Peeking, which cannot be done on
an empty stack, does not modify the stack contents.
• push( item ): Adds the given item to the top of the stack.
Push operation
10
The process of putting a new data element onto stack is known
as a Push Operation. Push operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next
empty space.
Step 4 − Adds data element to the stack location, where top is
pointing.
Step 5 − Returns success.
Pop Operation
11
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and
exit.
Step 3 − If the stack is not empty, accesses the data
element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
Implementing the Stack in Python
12
 Using a Python List
 Using a Linked Lis
 Using Collections.deque
 Using queue.LifoQueue
Application of stack
13
 Infix notation: the operator comes between the two
operands need to use parentheses to control the
evaluation of the operators.
 Postfix notation: the operator comes after its two operands :
no need to use parentheses
 Prefix notation: the operator comes before its two
operands : no need to use parentheses
Example: a + b
14
 Infix notation : a + b
 Prefix notation : + a b
 Postfix notation: a b +
Algorithm Transforming Infix Expression into Postfix Expression
15
Rules for the conversion from infix to postfix expression
 Print the operand as they arrive.
 If the stack is empty or contains a left parenthesis on top, push the incoming operator on to the
stack.
 If the incoming symbol is '(', push it on to the stack.
 If the incoming symbol is ')', pop the stack and print the operators until the left parenthesis is found.
 If the incoming symbol has higher precedence than the top of the stack, push it on the stack.
 If the incoming symbol has lower precedence than the top of the stack, pop and print the top of the
stack. Then test the incoming operator against the new top of the stack.
 If the incoming operator has the same precedence with the top of the stack then use the
associativity rules. If the associativity is from left to right then pop and print the top of the stack then
push the incoming operator. If the associativity is from right to left then push the incoming operator.
 At the end of the expression, pop and print all the operators of the stack.
Example
16
 Convert the following infix expression topostfix
using stack:
1. A + (B /C)
2. (( A – ( B + C)) * D) / (E+F)
Evaluation of postfix expression using stack.
17
 Scan the expression from left to right.
 If we encounter any operand in the expression, then we push the operand
in the stack.
 When we encounter any operator in the expression, then we pop the
corresponding operands from the stack.
 When we finish with the scanning of the expression, the final value
remains in the stack.
Algorithm Transforming Infix Expression into Prefix Expression
18
Rules for the conversion of infix to prefix expression:
 First, reverse the infix expression given in the problem.
 Scan the expression from left to right.
 Whenever the operands arrive, print them.
 If the operator arrives and the stack is found to be empty, then simply push the operator into the stack.
 If the incoming operator has higher precedence than the TOP of the stack, push the incoming operator into
the stack.
 If the incoming operator has the same precedence with a TOP of the stack, push the incoming operator into
the stack.
 If the incoming operator has lower precedence than the TOP of the stack, pop, and print the top of the stack.
Test the incoming operator against the top of the stack again and pop the operator from the stack till it finds
the operator of a lower precedence or same precedence.
•
Algorithm Transforming Infix Expression into Prefix Expression
19
 If the incoming operator has the same precedence with the top of the stack and the
incoming operator is ^, then pop the top of the stack till the condition is true. If the
condition is not true, push the ^ operator.
When we reach the end of the expression, pop, and print all the operators from the
top of the stack.
If the operator is ')', then push it into the stack.
If the operator is '(', then pop all the operators from the stack till it finds ) opening
bracket in the stack.
If the top of the stack is ')', push the operator on the stack.
At the end, reverse the output.
Example
20
 Convert the following infix expression toPrefix
using stack:
1. A + (B /C)
2. (( A – ( B + C)) * D) / (E +F)
Evaluation of Prefix Expression using Stack
21
Step 1: Initialize a pointer 'S' pointing to the end of the expression.
Step 2: If the symbol pointed by 'S' is an operand then push it into the stack.
Step 3: If the symbol pointed by 'S' is an operator then pop two operands from the
stack. Perform the operation on these two operands and stores the result into the
stack.
Step 4: Decrement the pointer 'S' by 1 and move to step 2 as long as the symbols left
in the expression.
Step 5: The final result is stored at the top of the stack and return it.
Step 6: End
Converting Exercise
22
1 ) Convert these INFIX to PREFIX and POSTFIX :
A / B – C / D
(A + B) ^ 3 – C * D A ^ (B + C)
2)Convert these PREFIX to INFIX and POSTFIX :
+ – / A B C ^ DE
– + D E / X Y
^ + 2 3 – C D
3)Convert these POSTFIX to INFIX and PREFIX :
A B C + –
G H + I J / * A B ^ C D + –
Queue
23
The term queue is commonly defined to be a line of people waiting to be
served like those you would encounter at many business establishments.
Each person is served based on their position within the queue. Thus, the
next person to be served is the first in line. As more people arrive, they
enter the queue at the back and wait their turn.
A queue structure is well suited for problems in computer science that
require data to be processed in the order in which it was received. Some
common examples include computer simulations,
CPU process scheduling, and shared printer management.
Queue
24
A queue is a specialized list with a limited number of operations in which items
can only be added to one end and removed from the other. A queue is also
known as a first-in, first-out (FIFO) list.
Consider the below pictures, which illustrates an abstract view of a queue. New
items are inserted into a queue at the back while existing items are removed
from the front. Even though the illustration shows the individual items, they
cannot be accessed directly.
Queue
25
A queue is a data structure that a linear collection of items in which access is
restricted to a first-in first-out basis. New items are inserted at the back and
existing items are removed from the front. The items are maintained in the
order in which they are added to the structure.
 Queue(): Creates a new empty queue, which is a queue containing no
items.
 isEmpty(): Returns a boolean value indicating whether the queue is empty.
 length (): Returns the number of items currently in the queue.
 enqueue( item ): Adds the given item to the back of the queue.
 dequeue(): Removes and returns the front item from the queue. An item
cannot be dequeued from an empty queue.
Queue
26
A queue is like a line of people waiting for a bank teller. The
queue has a front and a rear.
$ $
Front
Rear
Queue
27
New people must enter the queue at the rear. it is usually
called an enqueue operation.
$
$
Front
Rear
Queue
28
When an item is taken from the queue, it always comes from the front. it
is usually called a dequeue operation
$ $
Front
Rear
Algorithm to insert element (enqueue Operation
29
The following steps should be taken to enqueue (insert) data into a queue
−
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow error and exit.
Step 3 − If the queue is not full, increment rear pointer to point the next
empty space.
Step 4 − Add data element to the queue location, where the rear is
pointing.
Step 5 − return success..
Algorithm to delete element (dequeue Operation)
30
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce underflow error and exit.
Step 3 − If the queue is not empty, access the data where front is
pointing.
Step 4 − Increment front pointer to point to the next available data
element.
Step 5 − Return success.
31
0 1 2 3 4
Front=-1
Rear=-1
A
0 1 2 3 4
Front=0
Rear=0
1
2
Enqueue A
A B
0 1 2 3 4
Front=0
Rear=1
3
Enqueue B
A B C
0 1 2 3 4
Front=0
Rear=2
4
Enqueue C
B C
0 1 2 3 4
Front=1
Rear=2
5
dequeue
C
0 1 2 3 4
Front=2
Rear=2
6
dequeue
0 1 2 3 4
Front=3
Rear=2
7
dequeue
Queue is empty
Queue is empty
Entry point is called Rear &
Exit point is called Front
Disadvantages of linear queue
32
On deletion of an element from existing queue, front pointer is
shifted to next position.
This results into virtual deletion of an element. By doing so
memory space which was occupied by deleted element is wasted
and hence inefficient memory utilization is occur.
Overcome disadvantage of linear queue
33
To overcome disadvantage of linear queue, circular queue is
use.
We can solve this problem by joining the front and rear end of a
queue to make the queue as a circular queue .
Circular queue is a linear data structure. It follows FIFO
principle.
In circular queue the last node is connected back to the first node
to make a circle.
Circular queue example
34
Circular queue example
35
Implementation
36
There are various ways to implement a queue in Python.
This article covers the implementation of queue using data
structures and modules from Python library.
Queue in Python can be implemented by the following
ways:
list
collections.deque
queue.Queue
Priority Queue
37
 A priority queue is a more specialized data structure
than a stack or a queue.:
 insert in rear,
 remove from front.
 But items in the priority queue are ordered by a key
value so that ‘the item with the lowest key (in some
 implementations the highest key) is always at the front.
Priority Queue
38
Priority Queues are abstract data structures
where each data/value in the queue has a certain
priority. For example, In airlines, baggage with
the title “Business” or “First-class” arrives earlier
than the rest.
Priority Queue
39
 Idea behind the Priority Queue is simple:
• Is a queue But the items are ordered by a key.
• So, once in the queue, your ‘position’ in the queue
may be changed by the arrival of a new item.
 Various applications of Priority queue in Computer Science
are: Job Scheduling algorithms, CPU and Disk Scheduling,
managing resources that are shared among different
processes, etc.
Key differences between Priority Queue and Queue:
40
In Queue, the oldest element is dequeued first. While, in Priority
Queue, element based on highest priority is dequeued.
When elements are popped out of a priority queue then result
obtained in either sorted in Increasing order or in Decreasing
Order. While, when elements are popped from a simple queue, a
FIFO order of data is obtained in the result.
Enqueue in priority queue
41
Dequeue in priority queue
42
END
43
Any Question?

More Related Content

What's hot

03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTSoumen Santra
 
Stack Data Structure V1.0
Stack Data Structure V1.0Stack Data Structure V1.0
Stack Data Structure V1.0Zidny Nafan
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operationSenthil Kumar
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9sumitbardhan
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 

What's hot (19)

Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
stack presentation
stack presentationstack presentation
stack presentation
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 
Stack
StackStack
Stack
 
Stack Data Structure V1.0
Stack Data Structure V1.0Stack Data Structure V1.0
Stack Data Structure V1.0
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack
StackStack
Stack
 
Stack
StackStack
Stack
 
Queues-handouts
Queues-handoutsQueues-handouts
Queues-handouts
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
Stack application
Stack applicationStack application
Stack application
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 

Similar to CH4.pptx

Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptxline24arts
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxprakashvs7
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
stack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdfstack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdfKalpana Mohan
 
Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Ramachandra Adiga G
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxKALPANAC20
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxYogesh Pawar
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURESUsha Mahalingam
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 

Similar to CH4.pptx (20)

Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
stack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdfstack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdf
 
Data structures
Data structures Data structures
Data structures
 
Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
stack.ppt
stack.pptstack.ppt
stack.ppt
 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 

Recently uploaded

Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real timeSatoshi NAKAHIRA
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoSérgio Sacani
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptxRajatChauhan518211
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |aasikanpl
 
GFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxGFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxAleenaTreesaSaji
 
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...ssifa0344
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Patrick Diehl
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsAArockiyaNisha
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...jana861314
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhousejana861314
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfmuntazimhurra
 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...RohitNehra6
 
Presentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptxPresentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptxgindu3009
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Nistarini College, Purulia (W.B) India
 
Botany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questionsBotany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questionsSumit Kumar yadav
 

Recently uploaded (20)

Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real time
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
CELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdfCELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdf
 
Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on Io
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptx
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
 
The Philosophy of Science
The Philosophy of ScienceThe Philosophy of Science
The Philosophy of Science
 
GFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxGFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptx
 
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhouse
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdf
 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...
 
Presentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptxPresentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptx
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...
 
Botany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questionsBotany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questions
 

CH4.pptx

  • 3. TABLE OF CONTENTS 04 Stacks 01 03 Queue Circular Queue 05 Priority Queues 02 Parsing Arithmetic Expressions
  • 4. Introduction 4 Data structures have different kinds of storage structures such us Arrays, Stacks, queues, and priority queues . Consider Arrays – as a data storage structure: 1. very useful. 2. easy to insert into, delete from, and search for specific items.
  • 5. Stack 5 A stack is used to store data such that the last item inserted is the first item removed. It is used to implement a last-in first-out (LIFO) type protocol. The stack is a linear data structure in which new items are added, or existing items are removed from the same end, commonly referred to as the top of the stack. Stacks are very common in computer science and are used in many types of problems. Stacks also occur in our everyday lives. Consider a stack of trays in a lunchroom. When a tray is removed from the top, the others shift up. If trays are placed onto the stack, the others are pushed down.
  • 6. Stack 6 A stack is used to store data such that the last item inserted is the first item removed. It is used to implement a last-in first-out (LIFO) type protocol. The stack is a linear data structure in which new items are added, or existing items are removed from the same end, commonly referred to as the top of the stack.
  • 8. Stack 8 A stack is a data structure that stores a linear collection of items with access limited to a last-in first-out order. Adding and removing items is restricted to one end known as the top of the stack. An empty stack is one containing no items. • Stack(): Creates a new empty stack. • isEmpty(): Returns a boolean value indicating if the stack is empty. • length (): Returns the number of items in the stack.
  • 9. Stack 9 • pop(): Removes and returns the top item of the stack, if the stack is not empty. Items cannot be popped from an empty stack. The next item on the stack becomes the new top item. • peek(): Returns a reference to the item on top of a non-empty stack without removing it. Peeking, which cannot be done on an empty stack, does not modify the stack contents. • push( item ): Adds the given item to the top of the stack.
  • 10. Push operation 10 The process of putting a new data element onto stack is known as a Push Operation. Push operation involves a series of steps − Step 1 − Checks if the stack is full. Step 2 − If the stack is full, produces an error and exit. Step 3 − If the stack is not full, increments top to point next empty space. Step 4 − Adds data element to the stack location, where top is pointing. Step 5 − Returns success.
  • 11. Pop Operation 11 Step 1 − Checks if the stack is empty. Step 2 − If the stack is empty, produces an error and exit. Step 3 − If the stack is not empty, accesses the data element at which top is pointing. Step 4 − Decreases the value of top by 1. Step 5 − Returns success.
  • 12. Implementing the Stack in Python 12  Using a Python List  Using a Linked Lis  Using Collections.deque  Using queue.LifoQueue
  • 13. Application of stack 13  Infix notation: the operator comes between the two operands need to use parentheses to control the evaluation of the operators.  Postfix notation: the operator comes after its two operands : no need to use parentheses  Prefix notation: the operator comes before its two operands : no need to use parentheses
  • 14. Example: a + b 14  Infix notation : a + b  Prefix notation : + a b  Postfix notation: a b +
  • 15. Algorithm Transforming Infix Expression into Postfix Expression 15 Rules for the conversion from infix to postfix expression  Print the operand as they arrive.  If the stack is empty or contains a left parenthesis on top, push the incoming operator on to the stack.  If the incoming symbol is '(', push it on to the stack.  If the incoming symbol is ')', pop the stack and print the operators until the left parenthesis is found.  If the incoming symbol has higher precedence than the top of the stack, push it on the stack.  If the incoming symbol has lower precedence than the top of the stack, pop and print the top of the stack. Then test the incoming operator against the new top of the stack.  If the incoming operator has the same precedence with the top of the stack then use the associativity rules. If the associativity is from left to right then pop and print the top of the stack then push the incoming operator. If the associativity is from right to left then push the incoming operator.  At the end of the expression, pop and print all the operators of the stack.
  • 16. Example 16  Convert the following infix expression topostfix using stack: 1. A + (B /C) 2. (( A – ( B + C)) * D) / (E+F)
  • 17. Evaluation of postfix expression using stack. 17  Scan the expression from left to right.  If we encounter any operand in the expression, then we push the operand in the stack.  When we encounter any operator in the expression, then we pop the corresponding operands from the stack.  When we finish with the scanning of the expression, the final value remains in the stack.
  • 18. Algorithm Transforming Infix Expression into Prefix Expression 18 Rules for the conversion of infix to prefix expression:  First, reverse the infix expression given in the problem.  Scan the expression from left to right.  Whenever the operands arrive, print them.  If the operator arrives and the stack is found to be empty, then simply push the operator into the stack.  If the incoming operator has higher precedence than the TOP of the stack, push the incoming operator into the stack.  If the incoming operator has the same precedence with a TOP of the stack, push the incoming operator into the stack.  If the incoming operator has lower precedence than the TOP of the stack, pop, and print the top of the stack. Test the incoming operator against the top of the stack again and pop the operator from the stack till it finds the operator of a lower precedence or same precedence. •
  • 19. Algorithm Transforming Infix Expression into Prefix Expression 19  If the incoming operator has the same precedence with the top of the stack and the incoming operator is ^, then pop the top of the stack till the condition is true. If the condition is not true, push the ^ operator. When we reach the end of the expression, pop, and print all the operators from the top of the stack. If the operator is ')', then push it into the stack. If the operator is '(', then pop all the operators from the stack till it finds ) opening bracket in the stack. If the top of the stack is ')', push the operator on the stack. At the end, reverse the output.
  • 20. Example 20  Convert the following infix expression toPrefix using stack: 1. A + (B /C) 2. (( A – ( B + C)) * D) / (E +F)
  • 21. Evaluation of Prefix Expression using Stack 21 Step 1: Initialize a pointer 'S' pointing to the end of the expression. Step 2: If the symbol pointed by 'S' is an operand then push it into the stack. Step 3: If the symbol pointed by 'S' is an operator then pop two operands from the stack. Perform the operation on these two operands and stores the result into the stack. Step 4: Decrement the pointer 'S' by 1 and move to step 2 as long as the symbols left in the expression. Step 5: The final result is stored at the top of the stack and return it. Step 6: End
  • 22. Converting Exercise 22 1 ) Convert these INFIX to PREFIX and POSTFIX : A / B – C / D (A + B) ^ 3 – C * D A ^ (B + C) 2)Convert these PREFIX to INFIX and POSTFIX : + – / A B C ^ DE – + D E / X Y ^ + 2 3 – C D 3)Convert these POSTFIX to INFIX and PREFIX : A B C + – G H + I J / * A B ^ C D + –
  • 23. Queue 23 The term queue is commonly defined to be a line of people waiting to be served like those you would encounter at many business establishments. Each person is served based on their position within the queue. Thus, the next person to be served is the first in line. As more people arrive, they enter the queue at the back and wait their turn. A queue structure is well suited for problems in computer science that require data to be processed in the order in which it was received. Some common examples include computer simulations, CPU process scheduling, and shared printer management.
  • 24. Queue 24 A queue is a specialized list with a limited number of operations in which items can only be added to one end and removed from the other. A queue is also known as a first-in, first-out (FIFO) list. Consider the below pictures, which illustrates an abstract view of a queue. New items are inserted into a queue at the back while existing items are removed from the front. Even though the illustration shows the individual items, they cannot be accessed directly.
  • 25. Queue 25 A queue is a data structure that a linear collection of items in which access is restricted to a first-in first-out basis. New items are inserted at the back and existing items are removed from the front. The items are maintained in the order in which they are added to the structure.  Queue(): Creates a new empty queue, which is a queue containing no items.  isEmpty(): Returns a boolean value indicating whether the queue is empty.  length (): Returns the number of items currently in the queue.  enqueue( item ): Adds the given item to the back of the queue.  dequeue(): Removes and returns the front item from the queue. An item cannot be dequeued from an empty queue.
  • 26. Queue 26 A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ Front Rear
  • 27. Queue 27 New people must enter the queue at the rear. it is usually called an enqueue operation. $ $ Front Rear
  • 28. Queue 28 When an item is taken from the queue, it always comes from the front. it is usually called a dequeue operation $ $ Front Rear
  • 29. Algorithm to insert element (enqueue Operation 29 The following steps should be taken to enqueue (insert) data into a queue − Step 1 − Check if the queue is full. Step 2 − If the queue is full, produce overflow error and exit. Step 3 − If the queue is not full, increment rear pointer to point the next empty space. Step 4 − Add data element to the queue location, where the rear is pointing. Step 5 − return success..
  • 30. Algorithm to delete element (dequeue Operation) 30 Step 1 − Check if the queue is empty. Step 2 − If the queue is empty, produce underflow error and exit. Step 3 − If the queue is not empty, access the data where front is pointing. Step 4 − Increment front pointer to point to the next available data element. Step 5 − Return success.
  • 31. 31 0 1 2 3 4 Front=-1 Rear=-1 A 0 1 2 3 4 Front=0 Rear=0 1 2 Enqueue A A B 0 1 2 3 4 Front=0 Rear=1 3 Enqueue B A B C 0 1 2 3 4 Front=0 Rear=2 4 Enqueue C B C 0 1 2 3 4 Front=1 Rear=2 5 dequeue C 0 1 2 3 4 Front=2 Rear=2 6 dequeue 0 1 2 3 4 Front=3 Rear=2 7 dequeue Queue is empty Queue is empty Entry point is called Rear & Exit point is called Front
  • 32. Disadvantages of linear queue 32 On deletion of an element from existing queue, front pointer is shifted to next position. This results into virtual deletion of an element. By doing so memory space which was occupied by deleted element is wasted and hence inefficient memory utilization is occur.
  • 33. Overcome disadvantage of linear queue 33 To overcome disadvantage of linear queue, circular queue is use. We can solve this problem by joining the front and rear end of a queue to make the queue as a circular queue . Circular queue is a linear data structure. It follows FIFO principle. In circular queue the last node is connected back to the first node to make a circle.
  • 36. Implementation 36 There are various ways to implement a queue in Python. This article covers the implementation of queue using data structures and modules from Python library. Queue in Python can be implemented by the following ways: list collections.deque queue.Queue
  • 37. Priority Queue 37  A priority queue is a more specialized data structure than a stack or a queue.:  insert in rear,  remove from front.  But items in the priority queue are ordered by a key value so that ‘the item with the lowest key (in some  implementations the highest key) is always at the front.
  • 38. Priority Queue 38 Priority Queues are abstract data structures where each data/value in the queue has a certain priority. For example, In airlines, baggage with the title “Business” or “First-class” arrives earlier than the rest.
  • 39. Priority Queue 39  Idea behind the Priority Queue is simple: • Is a queue But the items are ordered by a key. • So, once in the queue, your ‘position’ in the queue may be changed by the arrival of a new item.  Various applications of Priority queue in Computer Science are: Job Scheduling algorithms, CPU and Disk Scheduling, managing resources that are shared among different processes, etc.
  • 40. Key differences between Priority Queue and Queue: 40 In Queue, the oldest element is dequeued first. While, in Priority Queue, element based on highest priority is dequeued. When elements are popped out of a priority queue then result obtained in either sorted in Increasing order or in Decreasing Order. While, when elements are popped from a simple queue, a FIFO order of data is obtained in the result.