Stacks

Stacks - Stacks 2004 Goodrich, Tamassia Abstract Data Types...

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

View Full Document Right Arrow Icon
© 2004 Goodrich, Tamassia Stacks
Background image of page 1

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

View Full DocumentRight Arrow Icon
  Stacks 2 © 2004 Goodrich, Tamassia 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
Background image of page 2
  Stacks 3 © 2004 Goodrich, Tamassia The Stack ADT (§4.2) 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): inserts an  element object  pop (): removes and  returns the last inserted  element Auxiliary stack  operations: object  top (): returns the  last inserted element  without removing it integer  size (): returns the  number of elements  stored boolean  isEmpty ():  indicates whether no  elements are stored
Background image of page 3

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

View Full DocumentRight Arrow Icon
  Stacks 4 © 2004 Goodrich, Tamassia Stack Interface in Java Java interface  corresponding to  our Stack ADT Requires the  definition of class  EmptyStackExcept ion Different from the  built-in Java class  java.util.Stack public interface Stack { public int size() ; public boolean isEmpty() ; public Object top() throws EmptyStackException ; public void push(Object o) ; public Object pop() throws EmptyStackException ; }
Background image of page 4
  Stacks 5 © 2004 Goodrich, Tamassia 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  EmptyStackExceptio n
Background image of page 5

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

View Full DocumentRight Arrow Icon
  Stacks 6 © 2004 Goodrich, Tamassia Applications of Stacks Direct applications Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in the Java Virtual  Machine Indirect applications Auxiliary data structure for algorithms Component of other data structures
Background image of page 6
  Stacks 7 © 2004 Goodrich, Tamassia Method Stack in the JVM The Java Virtual Machine  (JVM) keeps track of the chain  of active methods with a stack When a method is called, the  JVM pushes on the stack a  frame containing Local variables and return value Program counter, keeping track of  the statement being executed  When a method ends, its frame  is popped from the stack and 
Background image of page 7

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

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

Page1 / 20

Stacks - Stacks 2004 Goodrich, Tamassia Abstract Data Types...

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

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