1 / 36

COMP 121

COMP 121. Week 13: Stacks. Objectives. Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements a stack Learn how to implement a stack using an underlying array or a linked list

evadne
Download Presentation

COMP 121

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. COMP 121 Week 13: Stacks

  2. Objectives • Learn about the stack data type and how to use its four methods: push, pop, peek, and empty • Understand how Java implements a stack • Learn how to implement a stack using an underlying array or a linked list • See how to use a stack to perform various applications

  3. Stack Abstract Data Type • Can be compared to a Pez dispenser • Only the top item can be accessed (added or removed) • Can only extract one item at a time • LIFO: Last-In, First-Out Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  4. Specification of the Stack Abstract Data Type • Only the top element of a stack is visible, therefore the number of operations performed by a stack are few • Need the ability to: • Inspect the top element • Retrieve the top element • Push a new element on the stack • Test for an empty stack Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  5. Specification of the Stack Abstract Data Type (continued) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  6. Stack Application: Palindrome Finder • Palindrome: A string that reads the same in either direction • Example: “Able was I ere I saw Elba” Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  7. PalindromeFinder Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  8. PalindromeFinder public class PalindromeFinder { private String inputString; private Stack < Character > charStack = new Stack < Character > (); /** Store the argument string in a stack of characters. @param str String of characters to store in the stack */ public PalindromeFinder(String str) { inputString = str; fillStack(); } /** Method to fill a stack of characters from an input string. */ private void fillStack() { for (int i = 0; i < inputString.length(); i++) { charStack.push(inputString.charAt(i)); } } Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  9. PalindromeFinder (continued) /** Method to build a string containing the characters in a stack. @return The string containing the words in the stack */ private String buildReverse() { StringBuilder result = new StringBuilder(); while (!charStack.empty()) { // Remove top item from stack and append it to result. result.append(charStack.pop()); } return result.toString(); } public boolean isPalindrome() { return inputString.equalsIgnoreCase(buildReverse()); } } Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  10. Stack Application: Parentheses Checker • When analyzing arithmetic expressions, it is important to determine whether an expression is balanced with respect to parentheses • (a+b*(c/(d-e)))+(d/e) • Problem is further complicated if braces or brackets are used in conjunction with parentheses • Solution is to use stacks! Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  11. ParenChecker Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  12. ParenChecker (continued) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  13. Vector Implementation of Stack • The Java API includes a Stack class as part of the package java.util • The vector class implements a growable array of objects • Elements of a vector can be accessed using an integer index • Size of a vector can grow or shrink to accommodate adding and removing elements Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  14. Vector Implementation of Stack (continued) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  15. List Implementation of a Stack • Can use either the ArrayList, Vector, or the LinkedList classes as all implement the List interface • Name of class illustrated in text is ListStack<E> • ListStack is an adapter class as it adapts the methods available in another class to the interface its clients expect by giving different names to essentially the same operations Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  16. Array Implementation of a Stack • Need to allocate storage for an array with an initial default capacity when creating a new stack object • Need to keep track of the top of the stack • No size method Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  17. Array Implementation of a Stack (continued) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  18. LinkedList Implementation of a Stack • We can implement a stack using a linked list of nodes Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  19. Comparing Stack Implementations • Extending a Vector (as is done by Java) is a poor choice for stack implementation as all Vector methods are accessible • Easiest implementation would be to use a List component for storing the data • Can use any List, but ArrayList is simplest Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  20. Comparing Stack Implementations (continued) • Using an array implementation is slightly harder • The index to the top of the stack would have to be kept up-to-date • New array would have to be created when stack filled the allocated size Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  21. Comparing Stack Implementations (continued) • Using a linked data structure to implement the stack is fairly easy • Storage allocation/reallocation is not a problem • Flexibility provided by linked structure is not used Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  22. Comparing Efficiency • List Component Implementation • All insertions and deletions are done at one end, so stack operations are O(1) • Array Implementation • Again, all insertion and deletions are done at one end, so stack operations are O(1) • Linked Data Structure Implementation • Again, all insertion and deletions are done at one end, so stack operations are O(1) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  23. Additional Stack Applications • Consider two case studies that relate to evaluating arithmetic expressions • Postfix and infix notation • Expressions normally written in infix form • Binary operators inserted between their operands • A computer normally scans an expression string in the order that it is input; easier to evaluate an expression in postfix form Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  24. Additional Stack Applications (continued) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  25. Additional Stack Applications (continued) • Advantage of postfix form is that there is no need to group subexpressions in parentheses • No need to consider operator precedence Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  26. Evaluating Postfix Expressions Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  27. Evaluating Postfix Expressions (continued) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  28. Evaluating Postfix Expressions (continued) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  29. Converting Infix to Postfix Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  30. Converting Infix to Postfix (continued) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  31. Converting Infix to Postfix (continued) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  32. Converting Infix to Postfix (continued) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  33. Converting Infix to Postfix (continued) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  34. Summary • A stack is a simple, last-in, first-out (LIFO) data structure • Four operations: empty, peek, pop, and push • Stacks are useful to process information in the reverse of the order that it is encountered • Java.util.Stack is implemented as an extension of the Vector class Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  35. Summary (continued) • Three ways to implement a stack: • Using an object of a class that implements the List interface as a container • Using an array as a container • Using a linked list as a container • Stacks can be applied in programs for evaluating arithmetic expressions Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  36. Any Questions?

More Related