Week 13 Stacks Flashcards

(5 cards)

1
Q

What kind of data structure is a stack?

A

LIFO - Last In First Out

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

Describe the push operation for a stack. It must account for any errors that may occur.

A
  1. Check if stack is full.
  2. If not, increase pointer by one
  3. Store new item at location indicated by pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe the pop operation for a stack. It must account for any errors that may occur.

A
  1. Check if stack is empty
  2. If not, decrease pointer by one
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain how a single stack can be used to reverse the order of the items in a queue.

A

(Until the queue is empty) repeatedly remove / delete (the front item) from the queue and push it on to the stack;

(Until the stack is empty) repeatedly pop items from the stack and add them to the (rear of the) queue;

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

Describe how a single stack could be used to evaluate an RPN expression.

A

(Starting at LHS of expression) push values/operands on to stack; R. if operators are also pushed onto stack, unless they are immediately popped off the stack

Each time operator reached pop top two values off stack (and apply operator to them) // Each time operator reached pop required number of values off stack (and apply operator to them);

Push result (of applying operator) to stack;

When end of expression is reached the top item of the stack is the result // when end of expression is reached pop one value off the stack;

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