2. 3. 2 Main Data Structures Algorithms Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Stacks

A
  • First In Last Out (FILO) data structure, implemented as an array using single pointer (top pointer)
  • The top pointer points to the element currently at the top of the stack, initialised at -1 because first position in stack is position 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Pseudocode for size()

A

Returns number of elements in stack, does this by increasing top pointer by 1 (First element in position 0)

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

Pseudocode for isEmpty()

A

Checks if the stack is empty, does this by checking if the pointer is less than 0 (If its 0 there is one item in the stack)

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

Pseudocode for peek()

A
  • Checks what the top pointer value is without removing the value
  • First check if the stack is empty to avoid an error
  • A is our array representing a stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Pseudocode for push()

A
  • New item to be added to stack passed as a parameter
  • Top pointer is updated prior to addition so that no data is overwritten
  • The new element is then inserted at the position of the top pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Pseudocode for pop()

A
  • Before removing an item, a check is done to make sure the stack is not empty
  • The element at the top of the pointer is recorded before being removed
  • The top pointer is decremented by one before the removed item is returned
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Example of a Stack

A
  • What would the result be of the following 3-element stack?
  • Keep in mind that 3 is not added to the stack because it is only a 3-element array, therefore attempting to add another item when the stack was full lead to an error
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Queues

A
  • First In First Out (FIFO) data structure, implemented as an array, make use of two pointers (front and back)
  • Front points to (stores) position of first element
  • Back points to (stores) position of the next available space
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Pseudocode for size()

A
  • Work out size of queue by subtracting the back pointer from the top pointer
  • For example, if front is 0 and back is 5, there are 5 elements in the queue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Pseudocode for isEmpty()

A

When queue is empty, front and back should be the same value, check if this is the case to determine if the queue is empty

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

Pseudocode for peek()

A

Peek returns element at the front of the queue without removing it

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

Pseudocode for enqueue(element)

A

To add an element, element placed in position of back pointer, back pointer is then incremented by 1

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

Pseudocode for dequeue()

A

Items removed from queue from position of front pointer, important to check if queue is empty before doing so to avoid an error

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

Example of a Queue

A

What happens if the following operations are ran on the queue above?

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

Linked Lists

A
  • Composed of nodes, each node has a pointer to the next item in the list
  • Lets say a node is N, the node after than would be N.next
  • The first item/element is the head, the last item/element is the tail
  • Lists are searched using a linear search, sequential “next” operation till desired element found
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Trees

A
  • Formed from nodes and edges, cannot contain cycles and are not directed
  • Trees are useful because they can be traversed
  • The two types of traversals to cover are depth first (post-order) and breadth first
  • Both can be implemented recursively, differ in order nodes are visited
  • Pre-order and in-order traversal are not required in the exam