SlideShare a Scribd company logo
1 of 38
Data Structure & Algorithm
CS-102
Ashok K Turuk
• There are certain frequent situations in
computer science when one wants to
restrict insertion and deletions so that
they can take place only at the
beginning or at the end not in the
middle.
– Stack
– Queue
2
Stack
3
Stack
• A Stack is a list of elements in which an
element may be inserted or deleted only
at one end, call top of the Stack
• Two basic operations are associated
with Stack
– Push : Insert an element into a stack
– Pop : Delete an element from a stack
4
Stack
• Stores a set of elements in a
particular order
• Stack principle: LAST IN FIRST
OUT= LIFO
• It means: the last element inserted is
the first one to be removed
• Which is the first element to pick up?
5
Last In First Out
B
A
D
C
B
A
C
B
A
C
B
Atop
top
top
top
A
6
1 3 4 52Initial
Last In First Out
B
A
C
B
A
top
top
top
A
7
5678
8
Representation of Stack
Stack can be represented in two
different ways :
[1] Linear ARRAY
[2] One-way Linked list
Array Representation of Stack
9
AA BB CC
1 2 3 4 5 7 7 8
STACK
TOP 3 MAXSTK 8
TOP = 0 or TOP = NULL will indicates
that the stack is empty
PUSH Operation
Perform the following steps to PUSH an
ITEM onto a Stack
[1] If TOP = MAXSTK, Then print:
Overflow, Exit [ Stack already filled]
[2] Set TOP = TOP + 1
[3] Set STACK[TOP] = ITEM [Insert
Item into new TOP Position]
[4] Exit
10
POP Operation
Delete top element of STACK and assign
it to the variable ITEM
[1] If TOP = 0, Then print Underflow and
Exit
[2] Set ITEM = STACK[TOP]
[3] Set TOP = TOP -1
[4] Exit
11
Linked List Representation of
Stack
CC XAABB
TOP
Top Of Stack
12
Bottom Of Stac
Head
PUSH Operation
• Push operation into the stack is
accomplished by inserting a node into
the front of the list [Insert it as the
first node in the list]
13
CC XAABB
TOP
DDPUSH DD into STACK
PUSH Operation
14
CC XAABB
TOP
CC XAABB
TOP
DD
STACK before PUSH Operation
STACK After PUSH Operation
PUSH Operation
[1] NEW->INFO = ITEM
[2] NEW->LINK = TOP
[3] TOP = NEW
[4] Exit
15
POP Operation
• POP operation is accomplished by
deleting the node pointed to by the
TOP pointer [Delete the first node in
the list]
16
POP Operation
17
CC XAABB
TOP
CC XAABB
TOP
DD
STACK before POP Operation
STACK After POP Operation
POP Operation
[1] IF TOP == NULL Then Write
Underflow and Exit
[2] Set ITEM = TOP->INFO
[3] Set TOP = TOP->LINK
[4] Exit
18
Arithmetic Expression; Polish
Notation
• Let Q be an arithmetic expression
involving constant and operations
• Find the value of Q using reverse Polish
(Postfix) Notation
19
Polish Notation
• Evaluate the following parenthesis-free
arithmetic expression
2 î 3 + 5 * 2 î 2 – 12 / 6
Evaluate the exponentiation to obtain
8 + 5 * 4 – 12 /6
Evaluate Multiplication and Division
8 + 20 – 2
Evaluate Addition and Subtraction
20
20
Polish Notation
• Infix notation [Operator symbol is
placed between two Operand]
A + B , C – D , E * F , G /H
(A + B) * C and A + (B*C)
• Polish Notation [Operator symbol is
placed before its operand]
+AB, -CD, *EF , /GH
Polish Notations are frequently called
Prefix
21
Polish Notation
• Infix expression to Polish Notation
[ ] to indicate a partial translation
(A+B)*C = [+AB]*C = *+ABC
A+(B*C) = A+[*BC] = +A*BC
(A+B)/(C-D) = [+AB]/[-CD] = /+AB-CD
22
Polish Notation
• The fundamental property of Polish
notation is that the order in which the
operations are to be performed is
completely determined by the positions
of the operators and operand in the
expression.
• One never needs parenthesis when
writing expression in Polish notations
23
Reverse Polish Notation
• Operator symbol is placed after its two
operand
AB+, CD-, EF*, GC/
One never needs parenthesis to
determine the order of the operation
in any arithmetic expression written in
reverse Polish notation.
Also known as Postfix notation
24
• Computer usually evaluates an
arithmetic expression written in infix
notation in two steps:
• First Step: Converts the expression to
Postfix notation
• Second Step: Evaluates the Postfix
expression.
25
Evaluation of Postfix Expression
• Algorithm to find the Value of an
arithmetic expression P Written in
Postfix
[1] Add a right parenthesis ‘)” at the end
of P. [This act as delimiter]
[2] Scan P from left to right and repeat
Steps 3 and 4 for each element of P
until the delimiter “)” is encountered
26
Evaluation of Postfix Expression
[3] If an operand is encountered, put it on
STACK
[4] If an operator is encountered, then
(a) Remove the two top elements of
STACK, where A is the top element and
B is the next-to-top element
(b) Evaluate B A
(c ) Place the result of (b) on STACK
27
Evaluation of Postfix Expression
[5] Set Value equal to the top element of
STACK
[6] Exit
28
Example
• P = 5, 6, 2, + , *, 12, 4, /, - [Postfix]
• Q = 5 * ( 6 + 2) – 12 / 4 [Infix]
• P:
29
5, 6, 2, +, *, 12, 4, /, -, )
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
30
5, 6, 2, +, *, 12, 4, /, -, )
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
Symbol Scanned STACK
(1) 5 5
(2) 6 5, 6
(3) 2 5, 6, 2
(4) + 5, 8
(5) * 40
(6) 12 40, 12
(7) 4 40, 12, 4
(8) / 40, 3
(9) - 37
Infix to Postfix
• Q is an arithmetic expression written in
infix notation
• î , * , / , + , -
• Three level of precedence
31
Infix to Postfix
• Q is an arithmetic expression written in
infix notation. This algorithm finds the
equivalent postfix notation
[1] Push “(“ onto STACK and “)” to the end
of Q
[2] Scan Q from Left to Right and Repeat
Steps 3 to 6 for each element of Q
until the STACK is empty
32
[3] If an operand is encountered, add it
to P
[4] If a left parenthesis is encountered,
push it onto STACK
[5] If an operator is encountered, then:
(a) Repeatedly pop from STACK and
to P each operator (on the top of
STACK) which has same precedence as
or higher precedence than .
33
(b) Add to STACK
[6] If a right parenthesis is encountered,
then
(a) Repeatedly pop from the STACK
and add to P each operator (on top of
STACK) until a left parenthesis is
encountered.
(b) Remove the left parenthesis. [Do
not add it to P]
[7] Exit
34
Example
• Q : A + ( B * C – ( D / E î F ) * G ) * H
35
A + ( B * C - ( D / E î F ) * G ) * H )
1 2 3 4 5 6 7 8 9 20
36
A + ( B * C - ( D / E î F ) * G ) * H )
1 2 3 4 5 6 7 8 9 20
Symbol STACK Expression P
Scanned
1 A ( A
2 + ( + A
3 ( ( + ( A
4 B ( + ( A B
5 * ( + ( * A B
6 C ( + ( * A B C
7 - ( + ( - A B C *
8 ( ( + ( - ( A B C *
37
A + ( B * C - ( D / E î F ) * G ) * H )
1 2 3 4 5 6 7 8 9 20
Symbol STACK Expression P
Scanned
8 ( ( + ( - ( A B C *
9 D ( + ( - ( A B C * D
10 / ( + ( - ( / A B C * D
11 E ( + ( - ( / A B C * D E
12 î ( + ( - ( / î A B C * D E
13 F ( + ( - ( / î A B C * D E F
14 ) ( + ( - A B C * D E F î /
15 * ( + ( - * A B C * D E F î /
38
A + ( B * C - ( D / E î F ) * G ) * H )
1 2 3 4 5 6 7 8 9 20
Symbol STACK Expression P
Scanned
15 * ( + ( - * A B C * D E F î /
16 G ( + ( - * A B C * D E F î /G
17 ) ( + A B C * D E F î / G * -
18 * ( + * A B C * D E F î / G * -
19 H ( + * A B C * D E F î / G * - H
20 ) A B C * D E F î / G * - H * +

More Related Content

What's hot

Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 
Shell sort in Data Structure Using C
Shell sort in Data Structure Using CShell sort in Data Structure Using C
Shell sort in Data Structure Using CAshish Gaurkhede
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESSowmya Jyothi
 
Pipeline oriented programming
Pipeline oriented programmingPipeline oriented programming
Pipeline oriented programmingScott Wlaschin
 
Complicated declarations in c
Complicated declarations in cComplicated declarations in c
Complicated declarations in cRahul Budholiya
 
Computer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab reportComputer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab reportBijoy679
 
Selection sort
Selection sortSelection sort
Selection sortJay Patel
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHISowmya Jyothi
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queuezzzubair
 
Insertion sort
Insertion sortInsertion sort
Insertion sortRajendran
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structureeShikshak
 
Operating system Describe the actions taken by a kernel to context-s.pdf
Operating system Describe the actions taken by a kernel to context-s.pdfOperating system Describe the actions taken by a kernel to context-s.pdf
Operating system Describe the actions taken by a kernel to context-s.pdfforwardcom41
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary searchNisha Soms
 
Html tables, forms and audio video
Html tables, forms and audio videoHtml tables, forms and audio video
Html tables, forms and audio videoSaad Sheikh
 

What's hot (20)

Sort presentation
Sort presentationSort presentation
Sort presentation
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Shell sort in Data Structure Using C
Shell sort in Data Structure Using CShell sort in Data Structure Using C
Shell sort in Data Structure Using C
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Pipeline oriented programming
Pipeline oriented programmingPipeline oriented programming
Pipeline oriented programming
 
Algorithmic Notations
Algorithmic NotationsAlgorithmic Notations
Algorithmic Notations
 
Complicated declarations in c
Complicated declarations in cComplicated declarations in c
Complicated declarations in c
 
Computer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab reportComputer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab report
 
Selection sort
Selection sortSelection sort
Selection sort
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queue
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Merge sort
Merge sortMerge sort
Merge sort
 
Operating system Describe the actions taken by a kernel to context-s.pdf
Operating system Describe the actions taken by a kernel to context-s.pdfOperating system Describe the actions taken by a kernel to context-s.pdf
Operating system Describe the actions taken by a kernel to context-s.pdf
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
Functions & Recursion
Functions & RecursionFunctions & Recursion
Functions & Recursion
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
ACID Property in DBMS
ACID Property in DBMSACID Property in DBMS
ACID Property in DBMS
 
Html tables, forms and audio video
Html tables, forms and audio videoHtml tables, forms and audio video
Html tables, forms and audio video
 

Similar to 5.stack

Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsAakash deep Singhal
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdfsoniasharmafdp
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxsandeep54552
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfixSenthil Kumar
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms StacksManishPrajapati78
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)mailmerk
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid ali rashid
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 
13 Stacks and Queues.pptx
13 Stacks and Queues.pptx13 Stacks and Queues.pptx
13 Stacks and Queues.pptxssuserb7c8b8
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsAfaq Mansoor Khan
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacksmaamir farooq
 

Similar to 5.stack (20)

Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
 
Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stack
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Circular queues
Circular queuesCircular queues
Circular queues
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
Stack application
Stack applicationStack application
Stack application
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
13 Stacks and Queues.pptx
13 Stacks and Queues.pptx13 Stacks and Queues.pptx
13 Stacks and Queues.pptx
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Stack
StackStack
Stack
 
Stack ADT
Stack ADTStack ADT
Stack ADT
 

More from Chandan Singh

Fundamental of Tissue engineering
Fundamental of Tissue engineeringFundamental of Tissue engineering
Fundamental of Tissue engineeringChandan Singh
 
Resistance Measurement instruments
Resistance Measurement instrumentsResistance Measurement instruments
Resistance Measurement instrumentsChandan Singh
 
Resistance Measurement instruments
Resistance Measurement instrumentsResistance Measurement instruments
Resistance Measurement instrumentsChandan Singh
 
Moving iron (MI) instruments
Moving iron (MI) instrumentsMoving iron (MI) instruments
Moving iron (MI) instrumentsChandan Singh
 
Moving iron (MI) instruments
Moving iron (MI) instrumentsMoving iron (MI) instruments
Moving iron (MI) instrumentsChandan Singh
 
Electrical Measurement & Instruments
Electrical Measurement & InstrumentsElectrical Measurement & Instruments
Electrical Measurement & InstrumentsChandan Singh
 
Static characteristics of Instruments
Static characteristics of InstrumentsStatic characteristics of Instruments
Static characteristics of InstrumentsChandan Singh
 
Resistance measurement
Resistance measurementResistance measurement
Resistance measurementChandan Singh
 
Introduction to sensors
Introduction to sensorsIntroduction to sensors
Introduction to sensorsChandan Singh
 
Classification (Analog instruments)
 Classification (Analog instruments) Classification (Analog instruments)
Classification (Analog instruments)Chandan Singh
 
AC Bridges: Balance Condition
AC Bridges: Balance ConditionAC Bridges: Balance Condition
AC Bridges: Balance ConditionChandan Singh
 
Cathode Ray Osciloscope
Cathode Ray OsciloscopeCathode Ray Osciloscope
Cathode Ray OsciloscopeChandan Singh
 
Instrument transformer CT & PT
Instrument transformer CT & PTInstrument transformer CT & PT
Instrument transformer CT & PTChandan Singh
 
Permanent Magnet Moving Coil
Permanent Magnet Moving Coil Permanent Magnet Moving Coil
Permanent Magnet Moving Coil Chandan Singh
 
10.m way search tree
10.m way search tree10.m way search tree
10.m way search treeChandan Singh
 
9.bst(contd.) avl tree
9.bst(contd.) avl tree9.bst(contd.) avl tree
9.bst(contd.) avl treeChandan Singh
 

More from Chandan Singh (20)

Fundamental of Tissue engineering
Fundamental of Tissue engineeringFundamental of Tissue engineering
Fundamental of Tissue engineering
 
Resistance Measurement instruments
Resistance Measurement instrumentsResistance Measurement instruments
Resistance Measurement instruments
 
Resistance Measurement instruments
Resistance Measurement instrumentsResistance Measurement instruments
Resistance Measurement instruments
 
Moving iron (MI) instruments
Moving iron (MI) instrumentsMoving iron (MI) instruments
Moving iron (MI) instruments
 
Moving iron (MI) instruments
Moving iron (MI) instrumentsMoving iron (MI) instruments
Moving iron (MI) instruments
 
Electrical Measurement & Instruments
Electrical Measurement & InstrumentsElectrical Measurement & Instruments
Electrical Measurement & Instruments
 
Static characteristics of Instruments
Static characteristics of InstrumentsStatic characteristics of Instruments
Static characteristics of Instruments
 
Resistance measurement
Resistance measurementResistance measurement
Resistance measurement
 
Introduction to sensors
Introduction to sensorsIntroduction to sensors
Introduction to sensors
 
Energy meter
Energy meterEnergy meter
Energy meter
 
Classification (Analog instruments)
 Classification (Analog instruments) Classification (Analog instruments)
Classification (Analog instruments)
 
AC Bridges: Balance Condition
AC Bridges: Balance ConditionAC Bridges: Balance Condition
AC Bridges: Balance Condition
 
Cathode Ray Osciloscope
Cathode Ray OsciloscopeCathode Ray Osciloscope
Cathode Ray Osciloscope
 
Instrument transformer CT & PT
Instrument transformer CT & PTInstrument transformer CT & PT
Instrument transformer CT & PT
 
Megohmmeter
MegohmmeterMegohmmeter
Megohmmeter
 
Moving Iron
Moving IronMoving Iron
Moving Iron
 
Permanent Magnet Moving Coil
Permanent Magnet Moving Coil Permanent Magnet Moving Coil
Permanent Magnet Moving Coil
 
10.m way search tree
10.m way search tree10.m way search tree
10.m way search tree
 
9.bst(contd.) avl tree
9.bst(contd.) avl tree9.bst(contd.) avl tree
9.bst(contd.) avl tree
 
8.binry search tree
8.binry search tree8.binry search tree
8.binry search tree
 

Recently uploaded

UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 

Recently uploaded (20)

UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 

5.stack

  • 1. Data Structure & Algorithm CS-102 Ashok K Turuk
  • 2. • There are certain frequent situations in computer science when one wants to restrict insertion and deletions so that they can take place only at the beginning or at the end not in the middle. – Stack – Queue 2
  • 4. Stack • A Stack is a list of elements in which an element may be inserted or deleted only at one end, call top of the Stack • Two basic operations are associated with Stack – Push : Insert an element into a stack – Pop : Delete an element from a stack 4
  • 5. Stack • Stores a set of elements in a particular order • Stack principle: LAST IN FIRST OUT= LIFO • It means: the last element inserted is the first one to be removed • Which is the first element to pick up? 5
  • 6. Last In First Out B A D C B A C B A C B Atop top top top A 6 1 3 4 52Initial
  • 7. Last In First Out B A C B A top top top A 7 5678
  • 8. 8 Representation of Stack Stack can be represented in two different ways : [1] Linear ARRAY [2] One-way Linked list
  • 9. Array Representation of Stack 9 AA BB CC 1 2 3 4 5 7 7 8 STACK TOP 3 MAXSTK 8 TOP = 0 or TOP = NULL will indicates that the stack is empty
  • 10. PUSH Operation Perform the following steps to PUSH an ITEM onto a Stack [1] If TOP = MAXSTK, Then print: Overflow, Exit [ Stack already filled] [2] Set TOP = TOP + 1 [3] Set STACK[TOP] = ITEM [Insert Item into new TOP Position] [4] Exit 10
  • 11. POP Operation Delete top element of STACK and assign it to the variable ITEM [1] If TOP = 0, Then print Underflow and Exit [2] Set ITEM = STACK[TOP] [3] Set TOP = TOP -1 [4] Exit 11
  • 12. Linked List Representation of Stack CC XAABB TOP Top Of Stack 12 Bottom Of Stac Head
  • 13. PUSH Operation • Push operation into the stack is accomplished by inserting a node into the front of the list [Insert it as the first node in the list] 13 CC XAABB TOP DDPUSH DD into STACK
  • 14. PUSH Operation 14 CC XAABB TOP CC XAABB TOP DD STACK before PUSH Operation STACK After PUSH Operation
  • 15. PUSH Operation [1] NEW->INFO = ITEM [2] NEW->LINK = TOP [3] TOP = NEW [4] Exit 15
  • 16. POP Operation • POP operation is accomplished by deleting the node pointed to by the TOP pointer [Delete the first node in the list] 16
  • 17. POP Operation 17 CC XAABB TOP CC XAABB TOP DD STACK before POP Operation STACK After POP Operation
  • 18. POP Operation [1] IF TOP == NULL Then Write Underflow and Exit [2] Set ITEM = TOP->INFO [3] Set TOP = TOP->LINK [4] Exit 18
  • 19. Arithmetic Expression; Polish Notation • Let Q be an arithmetic expression involving constant and operations • Find the value of Q using reverse Polish (Postfix) Notation 19
  • 20. Polish Notation • Evaluate the following parenthesis-free arithmetic expression 2 î 3 + 5 * 2 î 2 – 12 / 6 Evaluate the exponentiation to obtain 8 + 5 * 4 – 12 /6 Evaluate Multiplication and Division 8 + 20 – 2 Evaluate Addition and Subtraction 20 20
  • 21. Polish Notation • Infix notation [Operator symbol is placed between two Operand] A + B , C – D , E * F , G /H (A + B) * C and A + (B*C) • Polish Notation [Operator symbol is placed before its operand] +AB, -CD, *EF , /GH Polish Notations are frequently called Prefix 21
  • 22. Polish Notation • Infix expression to Polish Notation [ ] to indicate a partial translation (A+B)*C = [+AB]*C = *+ABC A+(B*C) = A+[*BC] = +A*BC (A+B)/(C-D) = [+AB]/[-CD] = /+AB-CD 22
  • 23. Polish Notation • The fundamental property of Polish notation is that the order in which the operations are to be performed is completely determined by the positions of the operators and operand in the expression. • One never needs parenthesis when writing expression in Polish notations 23
  • 24. Reverse Polish Notation • Operator symbol is placed after its two operand AB+, CD-, EF*, GC/ One never needs parenthesis to determine the order of the operation in any arithmetic expression written in reverse Polish notation. Also known as Postfix notation 24
  • 25. • Computer usually evaluates an arithmetic expression written in infix notation in two steps: • First Step: Converts the expression to Postfix notation • Second Step: Evaluates the Postfix expression. 25
  • 26. Evaluation of Postfix Expression • Algorithm to find the Value of an arithmetic expression P Written in Postfix [1] Add a right parenthesis ‘)” at the end of P. [This act as delimiter] [2] Scan P from left to right and repeat Steps 3 and 4 for each element of P until the delimiter “)” is encountered 26
  • 27. Evaluation of Postfix Expression [3] If an operand is encountered, put it on STACK [4] If an operator is encountered, then (a) Remove the two top elements of STACK, where A is the top element and B is the next-to-top element (b) Evaluate B A (c ) Place the result of (b) on STACK 27
  • 28. Evaluation of Postfix Expression [5] Set Value equal to the top element of STACK [6] Exit 28
  • 29. Example • P = 5, 6, 2, + , *, 12, 4, /, - [Postfix] • Q = 5 * ( 6 + 2) – 12 / 4 [Infix] • P: 29 5, 6, 2, +, *, 12, 4, /, -, ) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
  • 30. 30 5, 6, 2, +, *, 12, 4, /, -, ) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Symbol Scanned STACK (1) 5 5 (2) 6 5, 6 (3) 2 5, 6, 2 (4) + 5, 8 (5) * 40 (6) 12 40, 12 (7) 4 40, 12, 4 (8) / 40, 3 (9) - 37
  • 31. Infix to Postfix • Q is an arithmetic expression written in infix notation • î , * , / , + , - • Three level of precedence 31
  • 32. Infix to Postfix • Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix notation [1] Push “(“ onto STACK and “)” to the end of Q [2] Scan Q from Left to Right and Repeat Steps 3 to 6 for each element of Q until the STACK is empty 32
  • 33. [3] If an operand is encountered, add it to P [4] If a left parenthesis is encountered, push it onto STACK [5] If an operator is encountered, then: (a) Repeatedly pop from STACK and to P each operator (on the top of STACK) which has same precedence as or higher precedence than . 33
  • 34. (b) Add to STACK [6] If a right parenthesis is encountered, then (a) Repeatedly pop from the STACK and add to P each operator (on top of STACK) until a left parenthesis is encountered. (b) Remove the left parenthesis. [Do not add it to P] [7] Exit 34
  • 35. Example • Q : A + ( B * C – ( D / E î F ) * G ) * H 35 A + ( B * C - ( D / E î F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 20
  • 36. 36 A + ( B * C - ( D / E î F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 20 Symbol STACK Expression P Scanned 1 A ( A 2 + ( + A 3 ( ( + ( A 4 B ( + ( A B 5 * ( + ( * A B 6 C ( + ( * A B C 7 - ( + ( - A B C * 8 ( ( + ( - ( A B C *
  • 37. 37 A + ( B * C - ( D / E î F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 20 Symbol STACK Expression P Scanned 8 ( ( + ( - ( A B C * 9 D ( + ( - ( A B C * D 10 / ( + ( - ( / A B C * D 11 E ( + ( - ( / A B C * D E 12 î ( + ( - ( / î A B C * D E 13 F ( + ( - ( / î A B C * D E F 14 ) ( + ( - A B C * D E F î / 15 * ( + ( - * A B C * D E F î /
  • 38. 38 A + ( B * C - ( D / E î F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 20 Symbol STACK Expression P Scanned 15 * ( + ( - * A B C * D E F î / 16 G ( + ( - * A B C * D E F î /G 17 ) ( + A B C * D E F î / G * - 18 * ( + * A B C * D E F î / G * - 19 H ( + * A B C * D E F î / G * - H 20 ) A B C * D E F î / G * - H * +