COSC 1 P 03 The Stack Research is

  • Slides: 18
Download presentation
COSC 1 P 03 The Stack Research is what I'm doing when I don't

COSC 1 P 03 The Stack Research is what I'm doing when I don't know what I'm doing. Wernher Von Braun (1912 -1977) Data Structures and Abstraction 7. 1

COSC 1 P 03 Container ADTs · container - collection of objects · list-oriented

COSC 1 P 03 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 Data Structures and Abstraction 7. 2

COSC 1 P 03 Stack · a list (initially empty) of items of some

COSC 1 P 03 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 Pez Candy Dispenser Data Structures and Abstraction 7. 3

COSC 1 P 03 Character Stack ADT · item type? - say char ·

COSC 1 P 03 Character Stack ADT · item type? - say char · Char. Stack interface - methods ° push ° pop ° top ° empty - exceptions ° No. Item. Exception ° No. Space. Exception Data Structures and Abstraction 7. 4

COSC 1 P 03 Character Stack ADT Contiguous Implementation · representation - based on

COSC 1 P 03 Character Stack ADT Contiguous Implementation · representation - based on “variable-sized” array ° bottom is base (0 th element) ° top is end of collection (nth element) · implementation - Char. Stacks package - instance variables ° array (elts) and count (top) - constructors ° empty stack - methods ° exceptions ° defensive programming · insertion/deletion don’t affect others O(1) Data Structures and Abstraction 7. 5

COSC 1 P 03 Character Stack ADT Linked Implementation · representation - top is

COSC 1 P 03 Character Stack ADT Linked 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) Data Structures and Abstraction 7. 6

COSC 1 P 03 Postfix (RPN) Notation · algebraic expressions · infix notation -

COSC 1 P 03 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 Data Structures and Abstraction 7. 7

COSC 1 P 03 Infix to Postfix Translation · manual process - insert all

COSC 1 P 03 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 Data Structures and Abstraction 7. 8

COSC 1 P 03 Rail Yard Algorithm · operands output as seen · operator

COSC 1 P 03 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 Data Structures and Abstraction 7. 9

COSC 1 P 03 Example: Inf. To. Post · client of Char. Stack ·

COSC 1 P 03 Example: Inf. To. Post · client of Char. Stack · translate method - initialization ° create stack (Con. Char. Stack or Lnk. Char. Stack) ° 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 ° #&$ Data Structures and Abstraction 7. 10

COSC 1 P 03 Generic ADTs · behaviour of stack independent of type of

COSC 1 P 03 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 Char. Stack ° 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 Data Structures and Abstraction 7. 11

COSC 1 P 03 Client Classes · cost of genericity - little added complexity

COSC 1 P 03 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 Data Structures and Abstraction 7. 12

COSC 1 P 03 Type Compatibility · implements clause declares that the Actual. Type.

COSC 1 P 03 Type Compatibility · implements clause declares that the Actual. Type. Parameter for Stack is whatever is supplied as Actual. Type. Parameter for Con. Stack · thus Con. Stack<Character> implements Stack<Character> and Con. Stack<Student> implements Stack<Student> · and so in Stack<Character> op. Stack; op. Stack = new Con. Stack<Character>(); is valid op. Stack = new Con. Stack<Student>(); is invalid Data Structures and Abstraction 7. 13

COSC 1 P 03 Type Checking · how does the compiler type check within

COSC 1 P 03 Type Checking · how does the compiler type check within generic class, e. g. elts[top] = item; · with Con. Stack<Character>, Character is substituted for E so this is OK · how does compiler know? - Actual. Type. Parameter must be a Reference. Type - all Reference. Types are subtypes of Object - compiler type checks assuming Type. Variable is Object · thus only operations available on the Type. Variable 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) Data Structures and Abstraction 7. 14

COSC 1 P 03 · note that in the constructor: elts = (E[]) new

COSC 1 P 03 · 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); Data Structures and Abstraction 7. 15

COSC 1 P 03 Example: Inf. To. Post. G · version of Inf. To.

COSC 1 P 03 Example: Inf. To. Post. G · version of Inf. To. Post 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 Data Structures and Abstraction 7. 16

COSC 1 P 03 Java Collections Framework · Java library for data structure ADTs

COSC 1 P 03 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 Data Structures and Abstraction 7. 17

COSC 1 P 03 The End Data Structures and Abstraction 7. 45

COSC 1 P 03 The End Data Structures and Abstraction 7. 45