Stacks and Queues
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
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…
Example (pushing) 5 Note: could use a linked list also
Example (pushing) 5 11
Example (pushing)
Example (pushing)
Example (current stack)
Example (peeking)
Example (popping)
Example (pushing)
Example (popping)
Example (popping)
Example (popping) 5 11
Example (popping) 5 FILO
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
Example
Example (enqueue a 5) 5
Example 5
Example (enqueue an 11) 5 11
Example 511
Example (enqueue a -6)
Example
Example (dequeue)
Example (dequeue)
Example (dequeue) FIFO
Example (dequeue) 11 -6
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