Stacks Queues and Appetizers for...

20
Stacks Queues and Appetizers for Parties by Vinny K.

Transcript of Stacks Queues and Appetizers for...

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

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

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

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

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

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

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

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

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

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

“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

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

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

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

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

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

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

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

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

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

LIFOOperate only on topPush/Pop

Stacks Queues

FIFOOperate on both endsEnqueue/Dequeue

O(1) for all!