Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Joe Meehean.  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2.

Similar presentations


Presentation on theme: "1 Joe Meehean.  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2."— Presentation transcript:

1 1 Joe Meehean

2  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2

3  empty is the stack empty  size  push add item to top of stack  pop remove and return item from top of stack  top return (do not remove) top of stack 3

4  Similar to List ADT with restricted operations implemented using arrays or linked nodes 4

5  Fields T *items_; // dynamically allocated array int size_; int capacity_ 0 0 size_: items_: 1 1 X X //Empty //After s.push(X) 5 4 4 capacity_: size_: items_: 4 4 capacity_:

6 2 2 size_: items: X X Y Y //After s.push(Y) 2 2 size_: items: Y Y X X //After s.push(Y) OR  Add to end or insert at the front? 6 4 4 capacity_: 4 4

7 MethodTop == FrontTop == Back push?? pop?? peek?? 7

8 MethodTop == FrontTop == Back pushO(N)O(N) avg: O(1) popO(N)O(1) peekO(1) 8

9  Fields Node *p_head_; int size_; 0 0 size_: p_head_: 1 1 //Empty //After s.push(A) A A 9 size_: p_head_:

10 2 2 //After s.push(B) A A B B 2 2 //After s.push(B) B B A A OR  Add to end or insert at the front? 10 size_: p_head_: size_: p_head_:

11 FrontSingle TailPtr Double TailPtr push???? pop???? peek???? BackSingle TailPtr Double TailPtr push???? pop???? peek???? 11

12 FrontSingle TailPtr Double TailPtr pushO(1) popO(1) peekO(1) BackSingle TailPtr Double TailPtr pushO(N)O(1)O(N)O(1) popO(N) O(1) peekO(N)O(1)O(N)O(1) 12

13 FrontSingle TailPtr Double TailPtr pushO(1) popO(1) peekO(1) BackSingle TailPtr Double TailPtr pushO(N)O(1)O(N)O(1) popO(N) O(1) peekO(N)O(1) O(N) 13  Least space  Easiest to implement

14  Restricted list  Best implemented with linked nodes singly-linked, no tail pointer unlike array implementation, no O(N) case 14

15  stack class in  methods empty size push pop top  Does not provide iterators 15

16 16


Download ppt "1 Joe Meehean.  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2."

Similar presentations


Ads by Google