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_arrays
tameemyousaf
ย 
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
ย 

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

DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
prakashvs7
ย 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
ย 
stack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdfstack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdf
Kalpana 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.pptx
KALPANAC20
ย 
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
ย 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
Yogesh Pawar
ย 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
DEEPAK948083
ย 

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

biology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGYbiology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGY
1301aanya
ย 
Introduction,importance and scope of horticulture.pptx
Introduction,importance and scope of horticulture.pptxIntroduction,importance and scope of horticulture.pptx
Introduction,importance and scope of horticulture.pptx
Bhagirath Gogikar
ย 
Pests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdf
PirithiRaju
ย 
module for grade 9 for distance learning
module for grade 9 for distance learningmodule for grade 9 for distance learning
module for grade 9 for distance learning
levieagacer
ย 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learning
levieagacer
ย 
dkNET Webinar "Texera: A Scalable Cloud Computing Platform for Sharing Data a...
dkNET Webinar "Texera: A Scalable Cloud Computing Platform for Sharing Data a...dkNET Webinar "Texera: A Scalable Cloud Computing Platform for Sharing Data a...
dkNET Webinar "Texera: A Scalable Cloud Computing Platform for Sharing Data a...
dkNET
ย 
Conjugation, transduction and transformation
Conjugation, transduction and transformationConjugation, transduction and transformation
Conjugation, transduction and transformation
Areesha Ahmad
ย 

Recently uploaded (20)

Connaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verified
Connaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verifiedConnaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verified
Connaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verified
ย 
Zoology 5th semester notes( Sumit_yadav).pdf
Zoology 5th semester notes( Sumit_yadav).pdfZoology 5th semester notes( Sumit_yadav).pdf
Zoology 5th semester notes( Sumit_yadav).pdf
ย 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
ย 
Site Acceptance Test .
Site Acceptance Test                    .Site Acceptance Test                    .
Site Acceptance Test .
ย 
IDENTIFICATION OF THE LIVING- forensic medicine
IDENTIFICATION OF THE LIVING- forensic medicineIDENTIFICATION OF THE LIVING- forensic medicine
IDENTIFICATION OF THE LIVING- forensic medicine
ย 
biology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGYbiology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGY
ย 
Introduction,importance and scope of horticulture.pptx
Introduction,importance and scope of horticulture.pptxIntroduction,importance and scope of horticulture.pptx
Introduction,importance and scope of horticulture.pptx
ย 
Pests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdf
ย 
High Profile ๐Ÿ” 8250077686 ๐Ÿ“ž Call Girls Service in GTB Nagar๐Ÿ‘
High Profile ๐Ÿ” 8250077686 ๐Ÿ“ž Call Girls Service in GTB Nagar๐Ÿ‘High Profile ๐Ÿ” 8250077686 ๐Ÿ“ž Call Girls Service in GTB Nagar๐Ÿ‘
High Profile ๐Ÿ” 8250077686 ๐Ÿ“ž Call Girls Service in GTB Nagar๐Ÿ‘
ย 
module for grade 9 for distance learning
module for grade 9 for distance learningmodule for grade 9 for distance learning
module for grade 9 for distance learning
ย 
COST ESTIMATION FOR A RESEARCH PROJECT.pptx
COST ESTIMATION FOR A RESEARCH PROJECT.pptxCOST ESTIMATION FOR A RESEARCH PROJECT.pptx
COST ESTIMATION FOR A RESEARCH PROJECT.pptx
ย 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learning
ย 
dkNET Webinar "Texera: A Scalable Cloud Computing Platform for Sharing Data a...
dkNET Webinar "Texera: A Scalable Cloud Computing Platform for Sharing Data a...dkNET Webinar "Texera: A Scalable Cloud Computing Platform for Sharing Data a...
dkNET Webinar "Texera: A Scalable Cloud Computing Platform for Sharing Data a...
ย 
GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)
ย 
GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)
ย 
pumpkin fruit fly, water melon fruit fly, cucumber fruit fly
pumpkin fruit fly, water melon fruit fly, cucumber fruit flypumpkin fruit fly, water melon fruit fly, cucumber fruit fly
pumpkin fruit fly, water melon fruit fly, cucumber fruit fly
ย 
Feature-aligned N-BEATS with Sinkhorn divergence (ICLR '24)
Feature-aligned N-BEATS with Sinkhorn divergence (ICLR '24)Feature-aligned N-BEATS with Sinkhorn divergence (ICLR '24)
Feature-aligned N-BEATS with Sinkhorn divergence (ICLR '24)
ย 
Conjugation, transduction and transformation
Conjugation, transduction and transformationConjugation, transduction and transformation
Conjugation, transduction and transformation
ย 
SAMASTIPUR CALL GIRL 7857803690 LOW PRICE ESCORT SERVICE
SAMASTIPUR CALL GIRL 7857803690  LOW PRICE  ESCORT SERVICESAMASTIPUR CALL GIRL 7857803690  LOW PRICE  ESCORT SERVICE
SAMASTIPUR CALL GIRL 7857803690 LOW PRICE ESCORT SERVICE
ย 
Call Girls Alandi Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Alandi Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Alandi Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Alandi Call Me 7737669865 Budget Friendly No Advance Booking
ย 

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.