5.2 Stacks, Queues and Lists Flashcards

1
Q

What is a dynamic Set M

A

Manages elements of a changing set M

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

Stack is LIFO or FIFO?

A

LIFO

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

Queue is LIFO or FIFO?

A

FIFO

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

How is Boolean Empty() operation implemented using a Stack?

A

if top == 0 then return true else false

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

How is Push(key k) operation implemented using a Stack?

A

top++
A[top] - k

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

How is key Pop() operation implemented using a Stack?

A

if Empty() then error

else

top–;
return A[top + 1]

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

How is key Top() operation implemented using a Stack?

A

if Empty() then error
else return A[top]

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

How is a stack created

A

A = new key[1 … n]
top = 0

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

What is the time complexity for the 5 operations when using a stack

A

O(1)

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

How is a queue created?

A

A = new key[1 … n]
head = tail = 1

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

How is the Boolean Empty() operation implemented using a queue

A

if head == tail them return true
else fals

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

How is the Enqueue(key k) operation implemented using a queue

A

A[tail] = k
if tail == A.Length then tail = 1
else tail++

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

How is the Dequeue operation implemented using a queue

A

k = A[head]
if head == A.length then head = 1
else head++
return k

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

How is a list created

A

head = nil;

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

How is the ptr Search(key k) operation implemented using a List

A

x = head
while x != nil and x.key != k
x = x.next
if x = nil then error else return x

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

How is the ptr Insert(key k) operation implemented using a List

A

x = new Item()
x.key = k; x.prev = nil; x.next = head
if head != nil then head.prev = x
head = x; return x

17
Q

What are the complexities for the three operations when implementing using a List

A

Creating List - O(1)
Search - O(n)
Insert - O(1)

18
Q

What are the complexities for the 5 operations when implementing using a Queue

19
Q

Why would you use a queue/stack over a list

A

Simplicity/security. If you don’t want people pulling things out of the middle of your data structure, don’t let them.