Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.

Similar presentations


Presentation on theme: "CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square."— Presentation transcript:

1 CSC 212 – Data Structures Lecture 17: Stacks

2 Question of the Day Move one matchstick to produce a square

3 Midterm Review Midterms were good, but not great  Individual questions answered after class

4 Abstract Data Types (ADTs) Main focus of remainder of class Each ADT defined by an Interface  Detail application programming interface…  … but in implementation-independent manner More than guarantee of min. functionality  Data able to be stored  Operations performed on/with data  Error conditions that may arise

5 Is Linked Lists an ADT? NO! This data type is not abstract  There are singly, doubly & circular versions  But cannot use array or other data type Does not specify functionality  No standard way of accessing or using data Arrays & linked lists are basis of different implementations

6 Collection Classes Data structures with which we begin  All these ADTs hold data for processing  But each has different access pattern Define a super-interface for these ADTs public interface Collection { public int size(); public boolean isEmpty(); }

7 Stacks Our first collection class Works like PEZ dispenser: Push data onto stack  Added to top of stack Pop data off of stack  Removes top item  Only top object can be accessed

8 Applications of Stacks Stacks used in many applications  Back & Forward buttons in web browser  Undo & Redo commands in editor  Handle method frames in program  Process operations in Java Also used in many other data structures

9 Stack ADT ADT defines two vital methods…  push(obj)  push objonto stack  pop()  pop item off of stack & return it … and 2 methods from Collection…  size()  return count of elements on stack  isEmpty()  true when stack is empty … and an accessor method  top()  return (but do not remove) top item

10 Stack ADT (2) Requires defining a new exception public class EmptyStackException extends RuntimeException { public EmptyStackException(String err) { super(err); } } EmptyStackException is unchecked  Need not be listed in throws, but should be  try-catch block not required either, but uncaught exception will crash program

11 Stack Interface public interface Stack extends Collection { public E top() throws EmptyStackException; public E pop() throws EmptyStackException; public void push(E element); } Generics allows use with any object type Defines minimum set of exceptions  Implementations could define more unchecked exceptions

12 Array-based Stack Insert from low to high entry Keep field to record index of top element S 012 t … Algorithm size() return t + 1 Algorithm pop() if isEmpty() then throw EmptyStackException else t  t  1 return S[t + 1]

13 Array-based Stack (cont.) Array may become full Push operation could not succeed  Must throw exception  Limitation of array- based implementation  Not all stack implementations have this problem S 012 t … Algorithm push(o) if t = S.length  1 then throw FullStackException else t  t + 1 S[t]  o

14 Your Turn Get back into groups and do activity Picture of array-based stack from Carrano/Savitch, Data Structures & Abstractions with Java, Prentice Hall, 2003.

15 Before Next Lecture… Start week #7 assignment Start programming assignment #2  Will be available tonight/tomorrow


Download ppt "CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square."

Similar presentations


Ads by Google