A-level Computing/AQA/Paper 1/Fundamentals of data structures/Stacks

PAPER 1 - ⇑ Fundamentals of data structures ⇑

Queues Stacks Graphs
Category:Book:A-level Computing#AQA/Paper%201/Fundamentals%20of%20data%20structures/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:

  • Pushing: Adds a new specified item to the top of the stack
  • Popping: Removes the item from the top of the stack.
State 1State 2State 3State 4State 5
Push 'elephant'Push 'hippo'Push 'zebra'PopPush 'flamingo'
memlocdataTopOfStack
4
3
2
1elephant<--
memlocdataTopOfStack
4
3
2hippo<--
1elephant
memlocdataTopOfStack
4
3zebra<--
2hippo
1elephant
memlocdataTopOfStack
4
3zebra
2hippo<--
1elephant

Note the data isn't 'deleted'
The TopOfStack pointer moves
to show it is no longer in
the stack

memlocdataTopOfStack
4
3flamingo<--
2hippo
1elephant

Stacks have several uses:

  • Reversing queues (as seen above with the Alphabetised names)
  • Performing Reverse Polish Calculations (see ....)
  • Holding return addresses and system states for recursive function calls
Exercise: Stacks

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

Answer:

Command 1Command 2Command 3Command 4
memlocdataTopOfStack
4
3
2
1Annabelle<--
memlocdataTopOfStack
4
3
2Chris<--
1Annabelle
memlocdataTopOfStack
4
3Hemingway<--
2Chris
1Annabelle
memlocdataTopOfStack
4James<--
3Hemingway
2Chris
1Annabelle
Command 5Command 6Command 7Command 8
memlocdataTopOfStack
4James
3Hemingway<--
2Chris
1Annabelle
Output: James
memlocdataTopOfStack
4James
3Hemingway
2Chris<--
1Annabelle
Output: Hemingway
memlocdataTopOfStack
4James
3Hemingway
2Chris
1Annabelle<--
Output: Chris
memlocdataTopOfStack
4James
3Hemingway
2Chris
1Annabelle
Output: Annabelle

Top of Stack = 0 The stack is empty

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:

State 1State 2
memlocdataTopOfStack
4
3Sharks<--
2Chalk
1Cheese
memlocdataTopOfStack
4
3Witches<--
2Sand
1Cheese

Answer:

  1. Pop
  2. Pop
  3. Push 'Sand'
  4. Push 'Witches'

If we have an empty Stack what do we set the pointer TopOfStack to?

Answer:

0 or null

Describe the Stack data type:

Answer:

A stack is a First In Last Out or Last In First Out data type

Give two uses for a Stack in a computer:

Answer:


  • Reversing queues (as seen above with the Alphabetised names)
  • Performing Reverse Polish Calculations (see ....)
  • Holding return addresses of recursive function calls
Dim myStack(10) as string
Dim ToS as integer = 0 'this is our pointer
Dim item as string

While item <> #
  Console.writeline(please insert item  & ToS &  into the stack: )
  item = Console.readline()
  myStack(ToS) = item
  ToS = ToS + 1
End While

For x = ToS to 0 step -1
  Console.writeline(Stack item:  & x &  =   & myStack(x) )
Next

console.readline()
Category:Book:A-level Computing#AQA/Paper%201/Fundamentals%20of%20data%20structures/Stacks%20
Category:Book:A-level Computing