Download - Stack Operation In Data Structure
![Page 1: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/1.jpg)
By:Divyesh JagatiyaHardik GopaniKeshu Odedara
![Page 2: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/2.jpg)
Stacks are linear lists.All deletions and insertions occur
at one end of the stack known as the TOP.
Data going into the stack first, leaves out last.
Stacks are also known as LIFO data structures (Last-In, First-Out).
Quick Introduction
![Page 3: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/3.jpg)
PUSH – Adds an item to the top of a stack.
POP – Removes an item from the top of the stack and returns it to the user.
PEEP – Find an item from the top of the stack.
Change(Update) – User can change the contents of the specific element.
Basic Stack Operations
![Page 4: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/4.jpg)
Reversing Data: We can use stacks to reverse data.(example: files, strings)Very useful for finding palindromes.
Consider the following pseudo code:1) read (data)2) loop (data not EOF and stack not full)
1) push (data)2) read (data)
3) Loop (while stack not Empty)1) pop (data)2) print (data)
Stack Applications
![Page 5: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/5.jpg)
Converting Decimal to Binary: Consider the following code 1) Read (number)2) Loop (number > 0)
1) digit = number modulo 22) print (digit)3) number = number / 2
// from Data Structures by Gilbert and Frozen
The problem with this code is that it will print the binarynumber backwards. (ex: 19 becomes 11001000 instead of 00010011.
)To remedy this problem, instead of printing the digit right away, we
canpush it onto the stack. Then after the number is done being
converted, we pop the digit out of the stack and print it.
Stack Applications
![Page 6: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/6.jpg)
Evaluating arithmetic expressions.
Prefix: + a b Infix: a + b (what we use in grammar
school) Postfix: a b +
In high level languages, infix notation cannot be used to evaluate expressions. We must analyze the expression to determine the order in which we evaluate it. A common technique is to convert a infix notation into postfix notation, then evaluating it.
Stack Applications
![Page 7: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/7.jpg)
PUSH OPERATION
![Page 8: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/8.jpg)
PUSH AlgorithmPUSH(S, Top, x)
Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit
Step 2: [Increment Top pointer By Value One]Top Top +1
Step 3: [Perform Insertion]s[Top] X
Step 4: [Finished]Exit
N
Top 0
![Page 9: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/9.jpg)
PUSH AlgorithmPUSH(S, Top, x)
Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit
Step 2: [Increment Top pointer By Value One]Top Top +1
Step 3: [Perform Insertion]s[Top] X
Step 4: [Finished]Exit
N
Top A
![Page 10: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/10.jpg)
PUSH AlgorithmPUSH(S, Top, x)
Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit
Step 2: [Increment Top pointer By Value One]Top Top +1
Step 3: [Perform Insertion]s[Top] X
Step 4: [Finished]Exit
N
Top
A
B
![Page 11: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/11.jpg)
PUSH AlgorithmPUSH(S, Top, x)
Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit
Step 2: [Increment Top pointer By Value One]Top Top +1
Step 3: [Perform Insertion]s[Top] X
Step 4: [Finished]Exit
N
A
B
Top C
![Page 12: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/12.jpg)
PUSH AlgorithmPUSH(S, Top, x)
Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit
Step 2: [Increment Top pointer By Value One]Top Top +1
Step 3: [Perform Insertion]s[Top] X
Step 4: [Finished]Exit
N Top
A
B
C
D
![Page 13: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/13.jpg)
PUSH AlgorithmPUSH(S, Top, x)
Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit
Step 2: [Increment Top pointer By Value One]Top Top +1
Step 3: [Perform Insertion]s[Top] X
Step 4: [Finished]Exit
N Top
A
B
C
D
Stack Overflow
![Page 14: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/14.jpg)
Pop OPERATION
![Page 15: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/15.jpg)
POP Algorithm
N Top
A
B
C
D
POP(S , Top)
Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]
if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit
Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1
Step 3: [Return From Top Element Of The Stack]
Write(value)
Step 4: [Finished]Exit
![Page 16: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/16.jpg)
POP Algorithm
N
A
B
C
POP(S , Top)
Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]
if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit
Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1
Step 3: [Return From Top Element Of The Stack]
Write(value)
Step 4: [Finished]Exit
Top
![Page 17: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/17.jpg)
POP Algorithm
N
A
B
POP(S , Top)
Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]
if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit
Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1
Step 3: [Return From Top Element Of The Stack]
Write(value)
Step 4: [Finished]Exit
Top
![Page 18: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/18.jpg)
POP Algorithm
N
A
POP(S , Top)
Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]
if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit
Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1
Step 3: [Return From Top Element Of The Stack]
Write(value)
Step 4: [Finished]Exit
Top
![Page 19: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/19.jpg)
POP Algorithm
N
POP(S , Top)
Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]
if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit
Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1
Step 3: [Return From Top Element Of The Stack]
Write(value)
Step 4: [Finished]ExitTop 0
![Page 20: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/20.jpg)
POP Algorithm
N
POP(S , Top)
Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]
if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit
Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1
Step 3: [Return From Top Element Of The Stack]
Write(value)
Step 4: [Finished]ExitTop 0
Stack Underflow
![Page 21: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/21.jpg)
PEEP OPERATION
![Page 22: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/22.jpg)
PEEP Algorithm
N Top
A
B
C
DPEEP(S, Top, i)
Step 1: [Check For Stack Underflow]if (Top - i + 1) <= 0then write("Stack Underflow")Exit
Step 2: [Return The ith Element From The Top Of The Stack]X S(Top - i + 1)
Step 3: [Finished]Exit
Top D
![Page 23: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/23.jpg)
CHANGE OPERATION
![Page 24: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/24.jpg)
CHANGE Algorithm
N Top
A
B
C
DCHANGE(S, Top, X, i)
Step 1: [Check For Stack Underflow]if (Top - i + 1) < 0then write("Stack Underflow")Exit
Step 2: [Change The Element Value From The Top Of The Stack]S(Top - i + 1) X
Step 3: [Finished]Exit
![Page 25: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/25.jpg)
CHANGE Algorithm
N Top
A
B
C
D1CHANGE(S, Top, X, i)
Step 1: [Check For Stack Underflow]if (Top - i + 1) < 0then write("Stack Underflow")Exit
Step 2: [Change The Element Value From The Top Of The Stack]S(Top - i + 1) X
Step 3: [Finished]Exit
![Page 26: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/26.jpg)
POLISH NOTATION
![Page 27: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/27.jpg)
Rules: Operands immediately go directly to output Operators are pushed into the stack (including parenthesis)
- Check to see if stack top operator is less than current operator- If the top operator is less, than push the current operator onto
stack- If the top operator is greater than the current, pop top operator
and push onto stack, push current operator onto stack- Priority 2: * /- Priority 1: + -- Priority 0: (
If we encounter a right parenthesis, pop from stack until we getmatching left parenthesis. Do not output parenthesis.
Infix to Postfix Conversion
![Page 28: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/28.jpg)
A + B * C - D / E
Infix Stack Postfix
(a)A ( Ab)+ (+ Ac)B (+ ABd)* (+* ABe)C (+* ABCf)- (- ABC*+g)D (- ABC*+Dh)/ (-/ ABC*+Di)E (-/ ABC*+DEj)) ABC*+DE/-
Infix to Postfix Example
![Page 29: Stack Operation In Data Structure](https://reader030.vdocuments.site/reader030/viewer/2022032513/55d0c7d3bb61eb7a358b45ef/html5/thumbnails/29.jpg)
A + B * C - D / E
Label No. Postfix Stack
1 A A2 B A B3 C A B C4 * A B * C5 + A + B * C6 D A + B * C D7 E A + B * C D E8 / A + B * C D / E9 - A + B * C – D / E
Postfix Evaluation