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

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

View Full Document Right Arrow Icon
11 The Stack ADT Stack attributes and operations Designing a test plan for Stack Implementing the Stack ADT The Adapter design pattern 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() Introduction these are the essential ones!
Background image of page 2
Examples of Stack applications 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
Stack Properties and Attributes Properties A Stack is a LIFO structure. Considered as a linear structure, any operation is done at only one end of the structure (referred to as the ‘top’ of the Stack). Attributes size : The number of elements in the stack. Invariant: size >= 0 top : Indicates the end of the stack where operations are performed.
Background image of page 4
A picture of a 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
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
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 all other elements of the stack remain in their previous positions size is incremented by 1 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
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 all other elements of the stack remain in their previous positions size is decremented by 1 returns: the element removed throws: empty stack exception if pre-condition is not met
Background image of page 8
Stack Operations peek()
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

Page1 / 23

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

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

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