1 / 67

Chapter 3

Chapter 3. Stacks. Chapter Objectives. Stack and its methods push pop peek empty Java Stack implementation Array/Linked List implementation Applications finding palindromes balanced parentheses arithmetic expressions. Stack Abstract Data Type. Section 3.1. Stack Abstract Data Type.

bruce-weiss
Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Stacks

  2. Chapter Objectives • Stack and its methods • push • pop • peek • empty • Java Stack implementation • Array/Linked List implementation • Applications • finding palindromes • balanced parentheses • arithmetic expressions

  3. Stack Abstract Data Type Section 3.1

  4. Stack Abstract Data Type • Widely used in computer science • Only the top item can be accessed • extract only one item at a time • Storage policy • Last-In, First-Out, or LIFO

  5. Specification • Top element is visible • We need the ability to • empty • peek • pop • push

  6. Stack Interface • StackInt Interface

  7. A Stack Operations • btf=name.empty(); • last = names.peek(); • temp = names.pop(); • names.push(“Philip”);

  8. Stack Applications Section 3.2

  9. Palindromes-Application 1 • Palindrome • a string that reads identically in either direction • Examples • kayak • "I saw I was I" • “Able was I ere I saw Elba” • "Level madam level" • Problem: • Use a program to determine if a string is a palindrome

  10. PalindromeFinder class

  11. Finding Palindromes Code import java.util.*; public class PalindromeFinder { private String inputString; private Stack<Character> charStack = new Stack<Character>(); public PalindromeFinder(String str) { inputString = str; fillStack(); // fills the stack with the characters in inputString } ...

  12. Implement fillStack() • Push each string character onto a stack k a k y a k a y a k k a y a a k y private void fillStack() { for(int i = 0; i < inputString.length(); i++) { charStack.push(inputString.charAt(i)); } } k a y a k a k k

  13. Implement buildReverse() • Pop each character off the stack k a a k k a k y a y k k a y a k a y a private String buildReverse(){ StringBuilder result = new StringBuilder(); while(!charStack.empty()) { result.append(charStack.pop()); } return result.toString(); } y a k a k k

  14. Implement isPalindrome() ... public boolean isPalindrome() { return inputString.equalsIgnoreCase(buildReverse()); } }

  15. Finding Palindromes (cont.) • PalindromeFinder Class

  16. Testing • Test with the following inputs: • a single character (always a palindrome) • multiple characters in a word • multiple words • different cases • even-length strings • odd-length strings • the empty string (considered a palindrome)

  17. Balanced Parentheses-Application 2 • Analyzing arithmetic expressions ( a + b * ( c / ( d – e ) ) ) + ( d / e ) • The solution is to use stacks!

  18. Methods

  19. Idea for “isBalanced”

  20. Demonstration (w * [x + y] / z) Expression: 0 1 2 3 4 5 6 7 8 9 10 ( w * [ x + y ] / z ) ( ( balanced : true index : 0

  21. Balanced Parentheses (cont.) (w * [x + y] / z) Expression: 0 1 2 3 4 5 6 7 8 9 10 ( w * [ x + y ] / z ) ( ( balanced : true index : 1

  22. Balanced Parentheses (cont.) (w * [x + y] / z) Expression: 0 1 2 3 4 5 6 7 8 9 10 ( w * [ x + y ] / z ) ( ( balanced : true index : 2

  23. Balanced Parentheses (cont.) (w * [x + y] / z) Expression: 0 1 2 3 4 5 6 7 8 9 10 ( w * [ x + y ] / z ) [ [ ( ( ( balanced : true index : 3

  24. Balanced Parentheses (cont.) (w * [x + y] / z) Expression: 0 1 2 3 4 5 6 7 8 9 10 ( w * [ x + y ] / z ) [ ( ( balanced : true index : 4

  25. Balanced Parentheses (cont.) (w * [x + y] / z) Expression: 0 1 2 3 4 5 6 7 8 9 10 ( w * [ x + y ] / z ) [ ( ( balanced : true index : 5

  26. Balanced Parentheses (cont.) (w * [x + y] / z) Expression: 0 1 2 3 4 5 6 7 8 9 10 ( w * [ x + y ] / z ) [ ( ( balanced : true index : 6

  27. Balanced Parentheses (cont.) (w * [x + y] / z) Expression: 0 1 2 3 4 5 6 7 8 9 10 ( w * [ x + y ] / z ) [ ( ( ( Matches! Balanced still true balanced : true index : 7

  28. Balanced Parentheses (cont.) (w * [x + y] / z) Expression: 0 1 2 3 4 5 6 7 8 9 10 ( w * [ x + y ] / z ) ( ( balanced : true index : 8

  29. Balanced Parentheses (cont.) (w * [x + y] / z) Expression: 0 1 2 3 4 5 6 7 8 9 10 ( w * [ x + y ] / z ) ( ( balanced : true index : 9

  30. Balanced Parentheses (cont.) (w * [x + y] / z) Expression: 0 1 2 3 4 5 6 7 8 9 10 ( w * [ x + y ] / z ) ( ( Matches! Balanced still true balanced : true index : 10

  31. Testing • Provide a variety of input expressions • Try several levels of nested parentheses • Try nested parentheses where corresponding parentheses are not of the same type • Try unbalanced parentheses • No parentheses at all!

  32. ParenChecker.java code • Listing 3.3-ParenChecker Code

  33. Implementing a Stack Section 3.3

  34. First way-using Vector public class Stack<E> extends Vector<E> • a growable array of objects • Elements of a Vector • can be accessed using an index • the size can grow or shrink

  35. Implement push and pop • To implement push public E push(obj E) { add(obj); return obj; } • To implement pop public E pop throws EmptyStackException { try { return remove (size() – 1); } catch (ArrayIndexOutOfBoundsException ex) { throw new EmptyStackException(); } }

  36. The problem is… • all Vector operations can be applied to a Stack • All the elements can be indexed • Violate information hiding rule • since only the top element of a stack should be accessible

  37. Second way-using ArrayList • ArrayList • Implement push method public E push(E obj) { theData.add(obj); return obj; } • Adapter class • Method delegation

  38. Code with a List Component • List 3.4-ListStack

  39. Third way-Using an Array • If we implement a stack as an array, we would need . . . public class ArrayStack<E> implements StackInt<E> { private E[] theData; int topOfStack = -1; private static final int INITIAL_CAPACITY = 10; @SupressWarnings("unchecked") public ArrayStack() { theData = (E[])new Object[INITIAL_CAPACITY]; } Allocate storage for an array with a default capacity Keep track of the top of the stack (subscript of the element at the top of the stack; for empty stack = -1) There is no size variable or method

  40. Implementation using Array Character Character Character Character value = 'a' value = 'J' value = 'v' value = 'a' ArrayStack Object[] theData = topOfStack = -1 [0] = null [1] = null [2] = null [3] = null [4] = null [5] = null [6] = null [7] = null [8] = null [9] = null 3 0 2 1 public E push(E obj) { if (topOfStack == theData.length - 1){ reallocate(); } topOfStack++; theData[topOfStack] = obj; return obj; }

  41. Implementation using Array @Override public E pop() { if (empty()) { throw new EmptyStackException(); } return theData[topOfStack--]; }

  42. The efficiency of using Array • This implementation is O(1), in contrast to the “kayak” example, which is O(n)

  43. Fourth Way-using Linked List • We can also implement a stack using a linked list of nodes It is easiest to insert and delete from the head of a list push inserts a node at the head and pop deletes the node at the head when the list is empty, pop returns null

  44. Using Linked-List: code • Listing 3.5 LinkedStack

  45. Comparison among the Stack Implementations • Extending a Vectoris a poor choice • Because all Vector methods are accessible • The easiest implementation • uses a List component- ArrayList is the simplest) for storing data • Arraylist-requires reallocation of space • linked list-requires allocating storage for links • insertions and deletions • constant time, O(1)

  46. More Stack Applications Section 3.4

  47. Postfix evaluation-stack application • Postfix and infix notation • infix is popular • Postfix is also useful • No parentheses needed

  48. How to Evaluate Postfix Expressions • Write a class that evaluates a postfix expression • Use the space as a delimiter between tokens

  49. The Idea to Evaluate Postfix Expressions 4 4 7 * 20 - 4 1. create an empty stack of integers 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result

  50. Evaluate Postfix Expressions 4 4 7 7 * 20 - 4 7 4 1. create an empty stack of integers 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result

More Related