140k views
2 votes
Question 4

This question concerns data structures.

In this question, <..> and [..] represent stacks and queues respectively. The top/head
of the stack/queue is to the left. For example
denotes a stack of elements a, b, c with top element a
[a, b, c] denotes a queue of elements a, b, c with head element
print(x) prints x in the above representation followed by a newline


(a)
What is printed by this pseudocode program?
new STACK s
s.push("Hello")
s.push("World!")
print(s)
new QUEUE q
q.enqueue("Hello")
q.enqueue("World!")
print(q)


(b)
Write a pseudocode function REVERSE(s) that takes a stack s and returns a new
stack with elements in the reverse order.
For example, REVERSE(<3, 4, 5>) = <5, 4, 3>.


(c)
Write a pseudocode function TOSTACK(q) that takes a queue q and returns a
stack such that the head of the queue is the same element as the top of the stack.
For example, TOSTACK([a, b, c]) = .
UL23/0210 Page 7 of 8


(d)
CALCULATE, in the following pseudocode, accepts a stack, with parameter s.
Tokens +, -, * and / have the normal arithmetical meaning.
function CALCULATE(s)
new STACK(t)
while !EMPTY(s)
print(s)
while TOP(s) is an integer
t.push(POP(s))
end while
op = POP(s)
if op = +
PUSH(POP(t) + POP(t), s)
else if op = -
PUSH(POP(t) - POP(t), s)
else if op = *
PUSH(POP(t) * POP(t), s)
else if op = /
x = POP(t)
y = POP(t)
PUSH(y / x, s)
end if
end while
s.push(t.pop())
end function
What is printed by the following code?
new Queue(q)
ENQUEUE(3, q)
ENQUEUE(4, q)
ENQUEUE(+, q)
ENQUEUE(5, q)
ENQUEUE(*, q)
CALCULATE(TOSTACK(q))

(e)

(i) What calculation is performed in part (d)?

(ii) What input stack to CALCULATE would enable the calculation 3 + (4 * 5)?

(f) Explain how CALCULATE performs arithmetical calculations

1 Answer

3 votes

Final answer:

This detailed response covers how stacks and queues operate, how to reverse a stack, convert a queue to a stack, and how the CALCULATE function performs arithmetic operations using pseudocode.

Thus the corret opction is:a.b.c.d

Step-by-step explanation:

The pseudocode provided for stacks and queues represents fundamental data structures in programming.

Part (a)

A stack operates on a Last In, First Out (LIFO) basis. Hence, when "Hello" followed by "World!" are pushed onto the stack, and then printed, the stack shows as . With the queue, operating on a First In, First Out (FIFO) basis, elements are enqueued as "Hello" and "World!" in this order, so when printed, it shows as [Hello, World!].

Part (b)

The REVERSE function for a stack iterates through the input stack and pushes each popped element into a new stack, effectively reversing the order due to the LIFO property of stacks, resulting in <5, 4, 3> when applied to <3, 4, 5>.

Part (c)

The TOSTACK function for converting a queue to a stack involves dequeuing elements from the queue and pushing them onto a new stack. Since a queue is FIFO, the head of the queue will end up being the top of the stack. For instance, TOSTACK([a, b, c]) yields .

Part (d)

The CALCULATE function is designed to perform arithmetic operations based on tokens within a stack, operating with a secondary stack to store intermediate values. The example uses the CALCULATE function with <3, 4, +, 5, *>, which under the calculation will print each stage of the stack as elements are popped, operated upon, and pushed back.

Part (e)

For part (d), the CALCULATE function performs the calculation (3 + 4) * 5. To perform the calculation 3 + (4 * 5), you would need the input stack to be <3, 4, 5, *, +>

Part (f)

The CALCULATE function performs arithmetic operations by popping numbers from a secondary stack and applying the operation defined by the next token to these numbers, then pushing the result back on the primary stack until a single value remains.

The complete question is:Question 4

This question concerns data structures.

In this question, <..> and [..] represent stacks and queues respectively. The top/head

of the stack/queue is to the left. For example

denotes a stack of elements a, b, c with top element a

[a, b, c] denotes a queue of elements a, b, c with head element

print(x) prints x in the above representation followed by a newline

(a)

What is printed by this pseudocode program?

new STACK s

s.push("Hello")

s.push("World!")

print(s)

new QUEUE q

q.enqueue("Hello")

q.enqueue("World!")

print(q)

(b)

Write a pseudocode function REVERSE(s) that takes a stack s and returns a new

stack with elements in the reverse order.

For example, REVERSE(<3, 4, 5>) = <5, 4, 3>.

(c)

Write a pseudocode function TOSTACK(q) that takes a queue q and returns a

stack such that the head of the queue is the same element as the top of the stack.

For example, TOSTACK([a, b, c]) = .

UL23/0210 Page 7 of 8

(d)

CALCULATE, in the following pseudocode, accepts a stack, with parameter s.

Tokens +, -, * and / have the normal arithmetical meaning.

function CALCULATE(s)

new STACK(t)

while !EMPTY(s)

print(s)

while TOP(s) is an integer

t.push(POP(s))

end while

op = POP(s)

if op = +

PUSH(POP(t) + POP(t), s)

else if op = -

PUSH(POP(t) - POP(t), s)

else if op = *

PUSH(POP(t) * POP(t), s)

else if op = /

x = POP(t)

y = POP(t)

PUSH(y / x, s)

end if

end while

s.push(t.pop())

end function

What is printed by the following code?

new Queue(q)

ENQUEUE(3, q)

ENQUEUE(4, q)

ENQUEUE(+, q)

ENQUEUE(5, q)

ENQUEUE(*, q)

CALCULATE(TOSTACK(q))

(e)

(i) What calculation is performed in part (d)?

(ii) What input stack to CALCULATE would enable the calculation 3 + (4 * 5)?

(f) Explain how CALCULATE performs arithmetical calculations

User Louis Singer
by
8.0k points