Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks.

Similar presentations


Presentation on theme: "Stacks."— Presentation transcript:

1 Stacks

2 What is a stack? Last-in first-out data structure (LIFO)
New objects are placed on top Removal restricted to top object Examples?

3 Stack ADT Operations push(o): Insert o at top of stack
Input: Object; Output: None pop(): Remove top object; error if empty Input: None; Output: Object removed size(): Return number of objects in stack Input: None; Output: Integer isEmpty(): Return a boolean indicating stack empty Input: None; Output: Boolean top(): Return top object without removing; error if empty Input: None; Output: Object

4 Example push(5) push(3) pop() push(7) top() isEmpty() push(9) push(7)
size() pop() push(8)

5 Implementing a Stack Implementation involves writing one or more classes which provide functions to accomplish stack operations Define the interface function prototypes return types constant references

6 Stack Interface int size() const; bool isEmpty() const;
Object& top() throw (StackEmptyException); void push(const Object& obj); Object pop() throw(StackEmptyException);

7 Underlying Representation
Array versus Linked List Pros and cons? Running time? size isEmpty push pop top

8 Algorithms – Array size push

9 Implementation – Array

10 Algorithms – Linked List
size push

11 Using a Stack


Download ppt "Stacks."

Similar presentations


Ads by Google