Arrays, Lists, Queues etc Flashcards

1
Q

Describe the properties of arrays

A

Contiguous: The elements are stored in consecutive memory locations
Homogeneous: All elements have the same type
Arrays have a fixed size which is defined when they are created

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

Describe the structure of a linked list

A

Made up of nodes, which store a piece of data and a pointer to the next node
The first node is called the head
The last node is called the tail and points to null
The nodes can be scattered across memory

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

Describe how a list would be implemented

A

L.head = pointer to head
L.tail = pointer to tail
L.size = length of list
N.data = data stored in node
N.next = pointer to the next node (or NULL)

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

What are the advantages/disadvantages of arrays/lists?

A

Arrays: Fast data access, slow data modification
Lists: Slow data access, fast data modification

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

What is a doubly-linked list?

A

A list where each node has a next and a prev pointer
Sentinel nodes at the start/end of the list
The size counter does not count sentinels

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

What is a circularly-linked list?

A

Like a singly-linked list but the last node points back to the first
The first node accessed when traversing the list is called the cursor

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

What is a stack?

A

Collection of objects that are inserted and removed according to the last-in-first-out (LIFO) principle

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

Which methods does a stack support?

A

push(e) inserts element e at the top of the stack
pop removes and returns the top element
size returns the number of elements
isEmpty returns 1 if the stack is empty
top returns the top element without removing it

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

How are stacks implemented using arrays?

A

They are stored as an N-element array S with an integer variable which stores the position of the top element (-1 when empty)

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

Advantages/disadvantages of representing stacks with arrays?

A

Time efficient, time taken does not depend on the size of the stack
Fixed capacity can cause errors

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

What is a queue?

A

Collection of objects that are inserted and removed according to the first-in-first-out (FIFO) principle
Element access and deletion are restricted to the first element in the sequence, which is called the front of the queue.Element insertion is restricted to the end of the sequence, which is called the rear of the queue.

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

What methods does a queue support?

A

enqueue(e) inserts element e at the back of the queue
dequeue removes and returns the element at the front of the queue
size returns the number of elements
front returns the element at the front without modifying it

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

How can a queue be represented by an array?

A

An array and two integers, f and r
f is the index of the cell storing the front of the queue
r is an index to the next available cell (the one after the rear)
Enqueue => increment r
Dequeue => increment f
r > f

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

How do we avoid errors when representing queues?

A

Let f and r wrap around using modulo N arithmetic, where N is the queue capacity
Limit the size of the queue to N-1 as a queue with N items would trigger isEmpty, as f = r

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

Queue size method

A

return (r - f) mod N

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

Queue isEmpty method

A

Returns true if f = r (= 0)

17
Q

Queue front method

A

if isEmpty then throw a EmptyQueueException
else return Q[f]

18
Q

Queue enqueue method

A

if size = N - 1 then throw a FullQueueException
Q[r] = e
r = r+1 mod N

19
Q

Queue dequeue method

A

if isEmpty then throw a EmptyQueueException
temp = Q[f]
Q[f] = NULL
f = f+1 mod N
return temp

20
Q

Stack size method

A

return t+1

21
Q

Stack isEmpty method

A

Return true if t<0

22
Q

Stack top method

A

if isEmpty then throw a EmptyStackException
else return S[t]

23
Q

Stack push method

A

if size = N then throw a FullStackException
t = t+1
S[t] = e

24
Q

Stack pop method

A

if isEmpty then throw a EmptyStackException
e = S[t]
S[t] = NULL
t = t-1
return e

25
Q

What is a hash table?

A

A data structure that consists of pairs of keys and values, consisting in memory of a bucket array and a hash function
The aim is to be able to look up data values using keys

26
Q

What is a bucket array?

A

An array of size N, where each cell is a bucket storing a collection of key-value pairs. The size N of the array is called the capacity of the hash table.

27
Q

What is a hash function?

A

A function mapping each key k to an integer in the range [0, N-1] where N is the capacity of the hash table

28
Q

How is the hash function h(k) used?

A

The key-value pair (k, v) is stored in the bucket A[h(k)]. If there are two keys with the same h(k) then a collision has occurred