Download presentation
Presentation is loading. Please wait.
Published byNoreen Patrick Modified over 6 years ago
1
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted, and not yet removed, will be the first (next) item dispensed. Real World Examples a spring-loaded cafeteria tray dispenser. a driveway wide that is one-car wide
2
Stack Stack Operations An Abstract Picture push
insert a new item into the stack pop remove one item from the container top inspect one item from the container, but don’t remove it An Abstract Picture
3
Stack - a Better Abstraction
(i.e. the class specification) Domain sequence of ItemType Constructor public Stack() post: this == sequence{} Query methods public boolean isEmpty() post: result == (this->size() == 0) public ItemType top() pre: ! isEmpty() post: result == this->first() Update methods public void push(ItemType z) post: this == public void pop() pre: ! isEmpty() (throws java.util.EmptyStackException) post: this == 2, ) Stack<ItemType>
4
Stack<ItemType>
Example Stack<ItemType> + Stack() + boolean isEmpty() + void push(ItemType z) + void pop() + ItemType top() Stack<String> s1, s2; String str; s1 = new Stack<String>(); s1.push( “humble” ); s1.push( “bumble” ); s1.push( “mumble” ); s1.pop(); s1.push( “tumble” ); str = (String) s1.top(); s1.push( “rumble” ); s1.push( “crumble” ); s1.push( s1.top() ); s2 = new Stack(); while ( !s1.isEmpty() ) { s2.push( s1.top() ); }
5
public class Stack<ItemType>
private java.util.LinkedList<ItemType> theList; /** post: theList.isEmpty() */ public Stack() { theList = new LinkedList<ItemType>(); } /** post: result == theList.isEmpty() */ public boolean isEmpty() { return theList.size() == 0; /** post: theList == with z appended to front*/ public void push(ItemType z) { theList.push(z); // theList.addFirst also works //continued …
6
public ItemType top() {
/** pre: ! theList.isEmpty() * post: theList == with first item removed */ public void pop() { theList.pop() ; // theList.removeFirst also works } * post: result == first item of theList */ public ItemType top() { return theList.peekFirst();
7
java.util.Stack<ItemType> Stack<ItemType>
The "Standard" Stack Class java.util.Stack<ItemType> + Stack() + boolean empty() + void push(ItemType z) + ItemType pop() + ItemType peek() Stack<ItemType> + Stack() + boolean isEmpty() + void push(ItemType z) + void pop() + ItemType top()
8
Stack Applications Each method call causes an activation record (local variables & parameters) to be pushed upon the run-time stack. When a method returns its activation record is popped off the run-time stack. Each time the compiler encounters a { it is pushed upon the parse stack. Each time the compiler encounters a } it is pops the matching { from the parse stack. The finite state machine can be augmented by adding a stack. Such a device is called a push-down automaton.
9
Evaluating RPN Expressions
Stacks are useful for evaluating many kinds of expressions. Reverse Polish notation In Reverse Polish Notation (RPN) operands occur before their associated operator, and operators occur as soon as possible. (Also called _____________.) Rules: 1) Operands occur left-to-right in the same order as infix expression 2) Operation follows immediately after its operand/subexpression Examples Infix RPN 7 - 3 7, 3, - 8 * 4 + 5 1 + (2*3) - 4 ((9+8)*7)-(6*(5+(4/3)))
10
RPN Evaluation Algorithm
Scan expression left to right, and… For each operand - push For each operator - replace two top operands by their subexpression value (Note: top of stack is right operand, second in stack is left.) Example 9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, - OperandStack
11
Evaluating Infix Expressions
An infix expression is one in which operators normally occur between their operands. How to evaluate an infix expression: 1) Use two stacks – one for operands and one for operators 2) Scan the expression left to right as follows for every operand push it on the operand stack for every operator (call it scannedOprtr) Repeatedly pop the operator stack until an operator of lesser precedence than scannedOprtr. For each popped operator - pop its operands, perform the operation, and push the result on the operand stack. Finally, push scannedOprtr. for every ( Push ( on the operator stack (treat as great precedence) for every ) Repeatedly pop the operator stack until ) is removed. For each popped operator - pop its operands, perform the operation, and push the result on the operand stack. for end of expression – like ) except no matching ( Pop and perform all operations remaining on the stacks.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.