1 / 18

The Stack

The Stack. Research is what I'm doing when I don't know what I'm doing. Wernher Von Braun (1912-1977) . Container ADTs. container collection of objects list-oriented collections positional access relative to position e.g. list keyed collections have a key accessed by key

jersey
Télécharger la présentation

The Stack

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. The Stack Research is what I'm doing when I don't know what I'm doing. Wernher Von Braun (1912-1977)

  2. Container ADTs • container • collection of objects • list-oriented collections • positional • access relative to position • e.g. list • keyed collections • have a key • accessed by key • representations • contiguous • arrays • linked • linked-structures

  3. Pez Candy Dispenser Stack • a list (initially empty) of items of some type to which items may be added (pushed) at one end (called the top) and from which items may be removed (popped) only from the same end (i.e. the top) • examples • cafeteria plate stacker • Pez™ candy dispenser • behaviour • example • LIFO ordering • operations • push & pop • viewing • empty • error conditions: • underflow • overflow

  4. Character Stack ADT • item type? • say char • CharStack interface • methods • push • pop • top • empty • exceptions • NoItemException • NoSpaceException

  5. Character Stack ADTContiguous Implementation • representation • based on “variable-sized” array • bottom is base (0th element) • top is end of collection (nth element) • implementation • CharStacks package • instance variables • array (elts) and count (top) • constructors • empty stack • methods • exceptions • defensive programming • insertion/deletion don’t affect others O(1)

  6. Character Stack ADTLinked Implementation • representation • top is front • list pointer points to top (front) node • implementation • instance variables • top is list pointer • constructor • empty stack • methods • exceptions • overflow? • Node wrapper class • Serializable • insert/delete at front O(1)

  7. Postfix (RPN) Notation • algebraic expressions • infix notation • priority • parentheses • postfix notation aka Reverse Polish Notation (RPN) • Jan Lukasiewicz • no need for parentheses or operator priorities • operator follows operands • examples • evaluate left to right • hardware computation in ALU

  8. Infix to Postfix Translation • manual process • insert all parentheses • convert each sub-expression from inside out • remove parentheses • automated process? • desire single left to right pass over input • properties • operands in same order • operator after operands • save operator • cannot output operator until see next one • if saved is higher priority output else save next operator • at end must output saved operators in LIFO order • use a stack

  9. Rail Yard Algorithm • operands output as seen • operator • lower/equal priority than one on stack output stacked • higher priority than one on stack push • open parenthesis then push, and assume lowest priority • closed parenthesis then output operators on stack and discard open parenthesis (from stack) , discard closed parenthesis • when done • emit stacked operators • special cases • no operator on stack  push • have low priority operator ($) at bottom of stack • end of expression  pop • have low priority operator (#) at end of expression

  10. Example: InfToPost • client of CharStack • translate method • initialization • create stack (ConCharStack or LnkCharStack) • String as char array • append # • push $ • main loop • process 1 character from input at a time • operand  output • operator  output some stacked operators then push • operator priorities • relative • prio method • # & $

  11. Generic ADTs • behaviour of stack independent of type of item • generalize interface to allow any item type • generic Stack interface • based on hypothetical content type E • generalization (abstraction) over the content type • changes from CharStack • change in name of exceptions • change from char to E throughout • addition of generic parameter <E> after interface name • E is a type variable • Collections package • set of collection ADT (stack, queue, list, …) • common names for exceptions

  12. Client Classes • cost of genericity • little added complexity in the client class • stacked items implement the interface of Stack <E> • item type as E • push OK • automatic wrapping of primitive types • autoboxing • pop returns an object • primitive types are unwrapped automatically • autounboxing • Linked implementation • define Node with a generic formal type • Node<E> is the type for Node. • E is the type for the element stored within Node<E> • Serializable, in case we want to write the stack to disk

  13. Type Compatibility • implements clause declares that the ActualTypeParameter for Stack is whatever is supplied as ActualTypeParameter for ConStack • thus ConStack<Character> implements Stack<Character> and ConStack<Student> implements Stack<Student> • and so in Stack<Character> opStack; opStack = new ConStack<Character>(); is valid opStack = new ConStack<Student>(); is invalid

  14. Type Checking • how does the compiler type check within generic class, e.g. elts[top] = item; • with ConStack<Character>, Character is substituted for E so this is OK • how does compiler know? • ActualTypeParameter must be a ReferenceType • all ReferenceTypes are subtypes of Object • compiler type checks assuming TypeVariable is Object • thus only operations available on the TypeVariable are those available on Object • note: compiler can still handle the elts array since it knows it is an array of some reference type and thus each element is a reference (4 bytes)

  15. note that in the constructor: elts = (E[]) new Object[size]; is used instead of the expected elts = new E[size]; • Java does not allow a type parameter to be used in an array creation expression • creating an array of Object and downcast it to E[] achieves the desired effect • enabling garbage collection • setting array elements to null allows the nodes to be garbage collected • the generic Node class • in the linked version, the Node class must be parametric since the type of the contents is unknown. • only nodes which wrap the same generic type can be put onto a list which supports a nodes of that type. • Node<Character> and Node<Student> are different types • creation of a new Node is done via top = new Node<E>(item,top);

  16. Example: InfToPostG • version of InfToPost using generic Stack • Chararacter • wrapper for char primitive type • algorithm the same • wrapping and unwrapping • automatic in Java 1.5 and up. • generics and templates

  17. Java Collections Framework • Java library for data structure ADTs • java.util • stacks, queues, lists, sets, maps • Collection interface • generic in element type • operations common to all collections (except maps) • methods return boolean if the operation changed the collection (i.e. added or removed) • Stack class • generic in element type (E) • array implementation (Vector) grows to fit (no overflow) • some duplication of methods (legacy implementation) • push returns item pushed

  18. The End

More Related