2.3.2 Algorithms for the Main Data Structures Flashcards
Describe stacks
A first in last out data structure. They use a single pointer to keep track of the element which is currently at the top of the stack.
Why might someone choose to initialise the top pointer of the stack at -1, where the top pointer represents the position of the top item?
The first element in the stack will be stored at position 0. If the pointer was 0, this would imply there is an item in the stack.
Name the operations that can be performed on a stack.
size()
isEmpty()
peek()
push(element)
pop()
Stacks: Size operation
Returns the number of elements in the stack (top pointer + 1)
Stacks: isEmpty operation
Checks if the stack is empty (topPointer = -1)
Stacks: peek operation
Returns the item at the top of the stack, after checking if the stack is empty.
Stacks: push operation
Adds an item to the top of the stack, by updating the top pointer then inserting the item at the top pointer position. The new item must be passed as a parameter.
Stacks: pop operation
Removes the item at the top of the stack, after checking if the stack is empty. The item at the top is recorded before being removed. The top pointer decrements by 1, and the removed item is returned.
Describe queues
A type of first in first out data structure. It uses a front and back pointer to hold the position of the first element and the position of the next available space.