A-level Computing 2009/AQA/Problem Solving, Programming, Operating Systems, Databases and Networking/Programming Concepts/Stacks





A stack is an ADT that might involve a dynamic or static implementation. A stack is a last-in-first-out (LIFO) or first-in-last-out (FILO) ADT. Implementations should include two operations, pushing and popping, and a pointer to the top of the stack.

A real life example is a stack of books you might have on your desk:

In this example we keep adding (pushing) books to the stack. If we want to get at the bottom book about Cars, we must first remove (pop) all the books above it. Hence First In Last Out (FILO)

Let's take a look at a computer implementation of a stack: Stacks have several uses:
 * Pushing: Adds a new specified item to the top of the stack
 * Popping: Removes the item from the top of the stack.
 * Reversing queues (as seen above with the Alphabetised names)
 * Performing Reverse Polish Calculations (see ....)
 * Holding return addresses and system states for recursive function calls

Draw the stack after each of the following commands, starting with an empty stack. What does the stack achieve:
 * 1) Push 'Annabelle'
 * 2) Push 'Chris'
 * 3) Push 'Hemingway'
 * 4) Push 'James'
 * 5) Pop
 * 6) Pop
 * 7) Pop
 * 8) Pop

This set of instructions used the stack to input data in order and then output it in reverse order

Give the set of instructions required to get from State 1 to State 2 as shown below:


 * 1) Pop
 * 2) Pop
 * 3) Push 'Sand'
 * 4) Push 'Witches'