Download presentation
Presentation is loading. Please wait.
Published byLilian Mustoe Modified over 10 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
© 2024 SlidePlayer.com. Inc.
All rights reserved.