CSC 216/s08/wholesome discipline

From Expertiza_Wiki
Jump to navigation Jump to search

Stacks & Queues

The problem

In this exercise, students will learn the difference between stacks and queues, mainly which end to push and pop from. In a stack, the items are added and removed from the top of a stack (LIFO); where as in a queue the items are added to the end and removed from the front (FIFO).

Participants and props

For this exercise about seven students will be needed to act as objects. The props will be plates, and candy.

The script

Each participant will be given a plate to write their name on, and will then stack them in a pile (Push). Afterwards, a certain name will have to be found, and the only way to get to it will be by taking off the top plate (Pop), until the desired name is found. This demonstrates the Last In First Out concept of a stack.

Then another group of students will demonstrate how queues work. Each student will line up to get a piece of candy. The first person to line up will be in the front and everyone else in the line will line up behind him (Push). Once the first person gets his piece of candy and leaves (Pop), the next person in line will then be able to get a piece of candy (which will most definitely raise class participation). This demonstrates the First In First Out concept of a queue.

After having done the exercise, give some examples of where queues and stacks are used in real programming. For example, stacks are used to keep track of method calls. Queues are used whenever using a buffer, or keeping track of things to do.