Chapter 36
> FUNCTION peek -> STRING AS IF isEmpty DO OUTPUT Stack is empty ELSE DO RETURN s[top] END IF END FUNCTION
Stack operation Stack contents Return value - |
['Blue', 'Red']
| - Pop |['Blue']
|'Red'
Pop |[]
|'Blue'
Push 'Yellow' |['Yellow']
|None
- |
Exercises
-
-
Stack
-
Overflow, where the code attempts to add an item to an already-full stack, such that it overwrites other bits of memory (or itself)
-
Stacks can be used for holding return addresses during execution, which are popped off during return
> PROCEDURE reverse_queue <- Queue queue AS SET stack TO CREATE Stack WHILE ! (CALL queue.isEmpty) DO CALL stack.push WITH CALL queue.pop END WHILE WHILE ! (CALL stack.isEmpty) DO CALL queue.push WITH CALL stack.pop END WHILE END PROCEDURE
-