Stacks - Outline and Reading The Stack ADT (4.2.1)...

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

View Full Document Right Arrow Icon
Stacks Stacks 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack Stacks 3 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: ± Data stored ± Operations on the data ± Error conditions associated with operations Example: ADT modeling a simple stock trading system ± The data stored are buy/sell orders ± The operations supported are ² order buy (stock, shares, price) ² order sell (stock, shares, price) ² void cancel (order) ± Error conditions: ² Buy/sell a nonexistent stock ² Cancel a nonexistent order Stacks 4 The Stack ADT The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme Think of a spring-loaded plate dispenser Main stack operations: ± push (object o): inserts element o ± pop (): removes and returns the last inserted element Auxiliary stack operations: ± top (): returns a reference to the last inserted element without removing it ± size (): returns the number of elements stored ± isEmpty (): returns a Boolean value indicating whether no elements are stored Stacks 5 Exceptions Attempting the execution of an operation of ADT may sometimes cause an error condition, called an exception Exceptions are said to be “thrown” by an operation that cannot be executed In the Stack ADT, operations pop and top cannot be performed if the stack is empty Attempting the execution of pop or top on an empty stack throws an EmptyStackException Stacks 6 Applications of Stacks Direct applications ± Page-visited history in a Web browser ± Undo sequence in a text editor ± Saving local variables when one function calls another, and this one calls another, and so on.
Background image of page 1

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

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

This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 4

Stacks - Outline and Reading The Stack ADT (4.2.1)...

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

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