![Page 1: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/1.jpg)
StacksQueues
andAppetizers for Parties
byVinny K.
![Page 2: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/2.jpg)
Abstract Data Types They are collections
“Moe” “Larry” “Curly”
They group related dataPerform different operations
The Three Musketeers
![Page 3: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/3.jpg)
Buffalo Chicken DipIngredients
Cream Cheese 16oz
Shredded Chicken 20oz
Hot Sauce ¾ cup
Ranch 1 cup
Celery + Chives Optional
Modified Fromhttps://www.allrecipes.com/recipe/68461/buffalo-chicken-dip/
![Page 4: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/4.jpg)
A StackLIFO Access – Last In; First Out
Only operating on top of the stack
We push (add) and pop (remove) items
![Page 5: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/5.jpg)
Pushing and Popping
Curly
Larry
Moe
push(“Moe”);push(“Larry”);push(“Curly”);
Larry
Moe
pop();
Bob
Larry
Moe
push(“Bob”);
![Page 6: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/6.jpg)
Use of A StackStore a history of actions in word processing software.
Simple undo featurePasted Block
Indent
Delete Line
History
![Page 7: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/7.jpg)
Spinach Tomato PinwheelsIngredients
Cream Cheese 8oz
Diced Tomatoes ¾ cup
Chopped Spinach ½ cup
Salt + Pepper
Basil
Oregano
Olive Oil
![Page 8: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/8.jpg)
Stack-array : item[]-top : int
+create(size)+push(item)+pop() : item+peek() : item+isEmpty() : boolean+size() : int
![Page 9: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/9.jpg)
A QueueFIFO Access - First In; First Out
Operating on both ends of the queue
We enqueue (add) and dequeue (remove)
![Page 10: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/10.jpg)
“Moe” “Larry” “Curly”enqueue(“Moe”);enqueue(“Larry”);enqueue(“Curly”);
“Larry” “Curly”
“Larry” “Curly” “Bob”
dequeue();
enqueue(“Bob”);
![Page 11: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/11.jpg)
OnigiriIngredients
Japanese Rice 2 cups
Nori Sheets 12
Salt ¾ cup
Water 2 ½ cup
Modified Fromhttps://www.justonecookbook.com/onigiri-rice-balls/
![Page 12: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/12.jpg)
Uses of a QueueServing files from a website to users
Processing documents on a printer
Anywhere where “first come; first serve” applies
![Page 13: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/13.jpg)
Queue-array : item[]-front : int-rear : int-size : int
+create(size)+enqueue(item)+dequeue() : item+peek() : item+isEmpty() : boolean+size() : int
![Page 14: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/14.jpg)
Cost of Stacks and QueuesAll operations are O(1).
How? We are only operating on the ends!
We never traverse through the middle.
![Page 15: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/15.jpg)
Stuffed Jalapeno PoppersIngredients
Jalapenos 1lb.
Ground Pork Sausage 1lb.
Cream Cheese 8oz
Bacon Slices 12 slices
Blood Pressure Meds Contact your doctor for info.
Modified Fromhttps://www.allrecipes.com/recipe/83500/sausage-stuffed-jalapenos/
![Page 16: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/16.jpg)
Implementing a Stack
Keep track of the index of the top of the stack and its size.
Array Linked List
The head already acts as the top of the stack. Easy!
![Page 17: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/17.jpg)
Implementing a Queue
Keep track of the index of the front and rear of the queue, and its size
Array Linked List
The head and tail already act as the front and the rear!
![Page 18: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/18.jpg)
Why Even Bother?Arrays give us access to everything; why restrict ourselves??
Stacks/Queues give us structure we can organize our data with
Prevents potential error
![Page 19: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/19.jpg)
Homemade RanchIngredients
Mayonnaise ¾ cup
Buttermilk ¼ cup
Fresh Garlic
Cayenne Pepper
Salt/Pepper
Dill Weed
Modified Fromhttps://www.thedailymeal.com/api-driven/web_recipe/755573
![Page 20: Stacks Queues and Appetizers for Partieswtkrieger.faculty.noctrl.edu/csc210-spring2020/docs/pk02_stack_que… · A Stack LIFO Access – Last In; First Out Only operating on topof](https://reader034.vdocuments.mx/reader034/viewer/2022042402/5f13ee060414a6799e30ed35/html5/thumbnails/20.jpg)
LIFOOperate only on topPush/Pop
Stacks Queues
FIFOOperate on both endsEnqueue/Dequeue
O(1) for all!