lec08 - CSE 12 The Stack Abstract Data Type The Stack ADT...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
08-1/33 The Stack ADT Designing a test plan for Stack The Adapter design pattern Implementing Stack using an array Implementing Stack using a linked list Time costs of Stack implementations CSE 12 The Stack Abstract Data Type
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stack : a L ast- I n- F irst- O ut ( LIFO ) data structure the last item put into the structure will be the first item to be taken out of it Values: a sequence of data items of type T Operations: push(T) T pop() T peek() boolean isEmpty() int size() 08-2/32 Introduction these are the essential ones!
Background image of page 2
08-3/32 Examples of Stack usage Browser “back” button: takes you to the last page visited before the current page Editor “undo” button: takes you to the last state of the document before the current state Operating system runtime stack: stores call frames in order to support method calls The call frame on top of the stack contains information about the currently executing method, the call frame below it represents the method executing when the current method was called, and so on
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
08-4/32 Stack Properties and Attributes Properties Stacks are LIFO data structures. All accesses are done to the element referenced by top. The top always refers to the topmost element in the stack. An insertion is done just “above” the top element; a deletion is done to the top element Attributes size : The number of elements in the stack: size >= 0 at all times. top : The topmost element of the stack, or null, a special value indicating the stack is empty
Background image of page 4
A typical stack Top of the stack
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
08-6/32 Stack Operations Stack() pre-condition: none responsibilities: constructor – initialize the stack attributes post-condition: size is 0 top refers to null (a special value not part of the stack) returns: nothing
Background image of page 6
08-7/32 Stack Operations push( Type ) pre-condition: none responsibilities: push element onto the top of the stack post-condition: element is placed on top of the stack size is incremented by 1 top refers to the element pushed returns: nothing
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
08-8/32 Stack Operations pop() pre-condition: isEmpty() is false responsibilities: remove and return the element at top post-condition: the top element is no longer in the stack size is decremented by 1 top refers to the element below the previous topmost element, or null if the stack is empty returns: the element removed throws: empty stack exception if pre-condition is not met
Background image of page 8
08-9/32 Stack Operations peek() pre-condition: isEmpty() is false responsibilities: return the element at top post-condition: the stack is unchanged returns: the element referenced by top throws: empty stack exception if pre-condition is not met
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
08-10/32 Behavior of Stack’s operations Method Purpose Object State Returned Value Stack<String> s = new LinkedStack<String>() Create an empty stack s size = 0 top = null a LinkedStack object for Strings s.push (“A”) Add“A” to stack s size = 1 “A” top s.push (“B”) Add “B” to stack s
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/15/2011 for the course ECON 1 taught by Professor Aben during the Fall '07 term at City College of San Francisco.

Page1 / 35

lec08 - CSE 12 The Stack Abstract Data Type The Stack ADT...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online