stacks chapter 20. 2 chapter contents specifications of the adt stack using a stack to process...
Post on 20-Dec-2015
220 views
TRANSCRIPT
![Page 1: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/1.jpg)
Stacks
Chapter 20
![Page 2: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/2.jpg)
2
Chapter ContentsSpecifications of the ADT StackUsing a Stack to Process Algebraic Expressions• Checking for Balanced Parentheses, Brackets, and
Braces in an Infix Algebraic Expression• Transforming an Infix Expression to a Postfix
Expression• Evaluating Postfix Expressions• Evaluating Infix Expressions
The Program Stack• Recursive Methods
Using a Stack Instead of Recursion• An Iterative Binary Search
Java Class Library: The Class Stack
![Page 3: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/3.jpg)
3
Specifications of the ADT Stack
Organizes entries according to order in which added
Additions are made to one end, the top
The item most recently added is always on the top
Fig. 20-1 Some familiar stacks.
![Page 4: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/4.jpg)
4
Specifications of the ADT StackSpecification of a stack of objects
public interface StackInterface{ /** Task: Adds a new entry to the top of the stack.
* @param newEntry an object to be added to the stack */public void push(Object newEntry);
/** Task: Removes and returns the top of the stack.* @return either the object at the top of the stack or null if the stack was empty */public Object pop();
/** Task: Retrieves the top of the stack.* @return either the object at the top of the stack or null if the stack is empty */public Object peek();
/** Task: Determines whether the stack is empty.* @return true if the stack is empty */public boolean isEmpty();
/** Task: Removes all entries from the stack */public void clear();
} // end StackInterface
![Page 5: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/5.jpg)
5
Specifications of the ADT Stack
Fig. 20-1 A stack of strings after (a) push adds Jim; (b) push adds Jess; (c) push adds Jill; (d) push adds Jane; (e) push adds Joe; (f) pop retrieves and removes
Joe; (g) pop retrieves and removes Jane
![Page 6: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/6.jpg)
6
Using a Stack to Process Algebraic Expressions
Infix expressions• Binary operators appear between operands• a + b
Prefix expressions• Binary operators appear before operands• + a b
Postfix expressions• Binary operators appear after operands• a b +• Easier to process – no need for parentheses nor
precedence
![Page 7: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/7.jpg)
7
Checking for Balanced (), [], {}
Fig. 20-3 The contents of a stack during the scan of an expression that contains the balanced delimiters {[()]}
![Page 8: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/8.jpg)
8
Checking for Balanced (), [], {}
Fig. 20-4 The contents of a stack during the scan of an expression that contains the unbalanced delimiters {[(])}
![Page 9: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/9.jpg)
9
Checking for Balanced (), [], {}
Fig. 20-5 The contents of a stack during the scan of an expression that contains the unbalanced delimiters [()]}
![Page 10: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/10.jpg)
10
Checking for Balanced (), [], {}
Fig. 20-6 The contents of a stack during the scan of an expression that contains the unbalanced delimiters {[()]
![Page 11: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/11.jpg)
11
Checking for Balanced (), [], {}Algorithm checkBalance(expression)// Returns true if the parentheses, brackets, and braces in an expression are paired correctly.isBalanced = truewhile ( (isBalanced == true) and not at end of expression){ nextCharacter = next character in expression
switch (nextCharacter){ case '(': case '[': case '{':
Push nextCharacter onto stackbreak
case ')': case ']': case '}':if (stack is empty) isBalanced = falseelse{ openDelimiter = top of stack
Pop stackisBalanced = true or false according to whether openDelimiter and
nextCharacter are a pair of delimiters}break
}}if (stack is not empty) isBalanced = falsereturn isBalanced
![Page 12: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/12.jpg)
12
Transforming Infix to Postfix
Fig. 20-7 Converting the infix expression a + b * c to postfix form
![Page 13: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/13.jpg)
13
Transforming Infix to Postfix
Fig. 20-8(a) Converting infix expression to postfix form: a – b + c
![Page 14: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/14.jpg)
14
Transforming Infix to Postfix
Fig. 20-8(b) Converting infix expression to postfix form: a ^ b ^ c
![Page 15: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/15.jpg)
15
Infix-to-Postfix Algorithm
Symbol in Infix
Action
Operand Append to end of output expression
Operator ^ Push ^ onto stack
Operator +,-,*, or /
Pop operators from stack, append to output expression until stack empty or top has lower precedence than new operator. Then push new operator onto stack
Open parenthesis
Push ( onto stack
Close parenthesis
Pop operators from stack, append to output expression until we pop an open parenthesis. Discard both parentheses.
![Page 16: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/16.jpg)
16
Transforming Infix to Postfix
Fig. 20-9 Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form.
![Page 17: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/17.jpg)
17
Evaluating Postfix Expression
Fig. 20-10 The stack during the evaluation of the postfix expression a b / when a is 2 and b is 4
![Page 18: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/18.jpg)
18
Transforming Infix to Postfix
Fig. 20-11 The stack during the evaluation of the postfix expression a b + c / when a is 2, b is 4 and c is 3
![Page 19: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/19.jpg)
19
Transforming Infix to Postfix Algorithm evaluatePostfix(postfix) // Evaluates a postfix expression.valueStack = a new empty stackwhile (postfix has characters left to parse){ nextCharacter = next nonblank character of postfix
switch (nextCharacter){ case variable:
valueStack.push(value of the variable nextCharacter)break
case '+': case '-': case '*': case '/': case '^':operandTwo = valueStack.pop()operandOne = valueStack.pop()result = the result of the operation in nextCharacter and its operands
operandOne and operandTwovalueStack.push(result)break
default: break}
}return valueStack.peek()
![Page 20: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/20.jpg)
20
Evaluating Infix Expressions
Fig. 20-12 Two stacks during evaluation of a + b * c when a = 2, b = 3, c = 4; (a) after reaching end of expression;
(b) while performing multiplication; (c) while performing the addition
![Page 21: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/21.jpg)
21
The Program Stack
When a method is called• Runtime environment creates activation record• Shows method's state during execution
Activation record pushed onto the program stack (Java stack)• Top of stack belongs to currently executing
method• Next method down is the one that called
current method
![Page 22: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/22.jpg)
22
The Program Stack
Fig. 20-13 The program stack at 3 points in time; (a) when main begins execution; (b) when methodA begins
execution, (c) when methodB begins execution.
![Page 23: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/23.jpg)
23
Recursive Methods
A recursive method making many recursive calls• Places many activation records in the program
stack• Thus the reason recursive methods can use
much memory
Possible to replace recursion with iteration by using a stack
![Page 24: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/24.jpg)
24
Using a Stack Instead of Recursionpublic boolean contains(Object desiredItem){ return binarySearch(0, length-1, (Comparable)desiredItem); } // end contains
/** Task: Searches entry[first] through entry[last] for desiredItem, where the array entry is a data field.* @param first an integer index >= 0 and < length of list* @param last an integer index >= 0 and < length of list* @param desiredItem the object to be found in the array* @return true if desiredItem is found */private boolean binarySearch(int first, int last, Comparable desiredItem){ boolean found;
int mid = (first + last)/2;if (first > last) found = false;else if (desiredItem.equals(entry[mid])) found = true;else if (desiredItem.compareTo(entry[mid]) < 0)
found = binarySearch(first, mid-1, desiredItem);else
found = binarySearch(mid+1, last, desiredItem);return found;
} // end binarySearch
![Page 25: Stacks Chapter 20. 2 Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,](https://reader035.vdocuments.us/reader035/viewer/2022062320/56649d4d5503460f94a2cdd8/html5/thumbnails/25.jpg)
25
Java Class Library: The Class Stack
Methods in class Stack in java.util
public Object push(Object item);public Object pop();public Object peek();public boolean empty();public int search(Object desiredItem);public Iterator iterator();public ListIterator listIterator();
Note differences in StackInterface from this chapter.