Download presentation

Presentation is loading. Please wait.

Published byLilian Mustoe Modified over 4 years ago

1
Stacks and Queues

2
Not really data structures – More of an enforcement of policy – Can be implemented using an array or linked list – Can store just about any kind of data Queues – First In, First Out (FIFO) – Like waiting in line Stacks – First In, Last Out (FILO) – Like a stack of trays Stacks and Queues

3
Stacks Three primary operations – Push() – put new data on the top of the stack – Pop() – remove data from the top of the stack – Peek() – get a copy of the data on the top of the stack Useful – our function stack! – stack of cards, tiles, loot, etc…

4
Example (pushing) 5 Note: could use a linked list also

5
Example (pushing) 5 11

6
Example (pushing) 5 11 -6

7
Example (pushing) 5 11 -6 45

8
Example (current stack) 5 11 -6 45

9
Example (peeking) 5 11 -6 45

10
Example (popping) 5 11 -6 45

11
Example (pushing) 5 11 -6 31

12
Example (popping) 5 11 -6 31

13
Example (popping) 5 11 -6

14
Example (popping) 5 11

15
Example (popping) 5 FILO

16
Queues Two primary operations – Enqueue() – put new data at the end of the queue – Dequeue() – remove data from the beginning of the queue – Peek() – yes, its still there… Useful – Enforcing fairness (waitlist at SPSU) – Player turns during a round

17
Example

18
Example (enqueue a 5) 5

19
Example 5

20
Example (enqueue an 11) 5 11

21
Example 511

22
Example (enqueue a -6) 511 -6

23
Example 511 -6

24
Example (dequeue) 511 -6

25
Example (dequeue) 511 -6

26
Example (dequeue) 5 11 -6 FIFO

27
Example (dequeue) 11 -6

28
Not really data structures – More of an enforcement of policy – Can be implemented using an array or linked list Queues are FIFO Stacks are FILO Which data structure is LILO? Which one is LIFO? Summary

Similar presentations

OK

ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google