2.3.2 Algorithms for the Main Data Structures Flashcards

1
Q

Describe stacks

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name the operations that can be performed on a stack.

A

size()
isEmpty()
peek()
push(element)
pop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Stacks: Size operation

A

Returns the number of elements in the stack (top pointer + 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Stacks: isEmpty operation

A

Checks if the stack is empty (topPointer = -1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Stacks: peek operation

A

Returns the item at the top of the stack, after checking if the stack is empty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Stacks: push operation

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Stacks: pop operation

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe queues

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly