Successfully reported this slideshow.
Upcoming SlideShare
×

# 12. Stack

Stacks in Java

• Full Name
Comment goes here.

Are you sure you want to Yes No

### 12. Stack

1. 1. Stack By Nilesh Dalvi Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College. http://www.slideshare.net/nileshdalvi01 Java and DataJava and Data StructuresStructures
2. 2. Stack • A Stack is a list of elements in which an element may be inserted or deleted at one end which is known as TOP of the stack. • Operation Performed On Array: 1. Push: add an element in stack 2. Pop: remove an element in stack 3. Peek :Current processed element Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). a b c TOP
3. 3. Array representation of stacks: • We can represent a stack in computer in various ways, by means of one-way-list or linear array. • Consider Linear array stack: • TOP : Location of the top element of the stack • MAXSTK : max elements that can be held by stack • If TOP = 0 or TOP = NULL indicates that stack is empty. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). XX YY ZZ 1 2 3 4 5 6 7 8 TOP MAXSTK
4. 4. Adding element: PUSH() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm PUSH (STACK, ITEM) { if (TOP = MAXSTK) then write ("Overflow"); else TOP := TOP + 1; STACK [TOP] := ITEM; }
5. 5. Deleting element: POP() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm POP (STACK, ITEM) { if (TOP = 0) then write ("Underflow"); else ITEM = STACK [TOP]; TOP := TOP - 1; }
6. 6. Accessing element: PEEK() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm PEEK (STACK) { if (TOP = 0) then write ("Underflow"); else Return STACK [TOP]; }
7. 7. Implementation of Stack operations Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
8. 8. Applications of Stack: • Recursion • Infix to postfix conversion • Postfix to infix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
9. 9. infixVect postfixVect ( a + b - c ) * d – ( e + f ) Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Infix to postfix conversion
10. 10. infixVect postfixVect a + b - c ) * d – ( e + f ) ( Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
11. 11. infixVect postfixVect + b - c ) * d – ( e + f ) ( a Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
12. 12. infixVect postfixVect b - c ) * d – ( e + f ) ( a + Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
13. 13. infixVect postfixVect - c ) * d – ( e + f ) ( a b + Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
14. 14. infixVect postfixVect c ) * d – ( e + f ) ( a b + - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
15. 15. infixVect postfixVect ) * d – ( e + f ) ( a b + c - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
16. 16. infixVect postfixVect * d – ( e + f ) a b + c - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
17. 17. infixVect postfixVect d – ( e + f ) a b + c - * Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
18. 18. infixVect postfixVect – ( e + f ) a b + c - d * Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
19. 19. infixVect postfixVect ( e + f ) a b + c – d * - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
20. 20. infixVect postfixVect e + f ) a b + c – d * - ( Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
21. 21. infixVect postfixVect + f ) a b + c – d * e - ( Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
22. 22. infixVect postfixVect f ) a b + c – d * e - ( + Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
23. 23. infixVect postfixVect ) a b + c – d * e f - ( + Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
24. 24. infixVect postfixVect a b + c – d * e f + - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
25. 25. infixVect postfixVect a b + c – d * e f + - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
26. 26. Algorithm for Infix to Postfix 1) Examine the next element in the input. 2) If it is operand, output it. 3) If it is opening parenthesis, push it on stack. 4) If it is an operator, then i) If stack is empty, push operator on stack. ii) If the top of stack is opening parenthesis, push operator on stack iii) If it has higher priority than the top of stack, push operator on stack. iv) Else pop the operator from the stack and output it, repeat step 4 5) If it is a closing parenthesis, pop operators from stack and output them until an opening parenthesis is encountered. pop and discard the opening parenthesis. 6) If there is more input go to step 1 7) If there is no more input, pop the remaining operators to output. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
27. 27. Q & A