Share
Explore BrainMass

Stacks Problem

1. Suppose there is a program that reads a word and writes the reverse of the word to to output. For example, the program reads "faced" and writes "decaf". The program uses a stack to reverse the string. Please list all activation of the push and pop methods along with which letter is being pushed or popped at each step if the input word is "rain".

2. Show every stack operation and the state of both the numbers and operations stacks after every push and pop when evaluating the arithmetic expression: (((10 * 9) - 3) * (((8 * 2) + 16))).

3. Given a priority queue, list the order that items would be dequeued given the following set of queue operations. The items in the queue are prioritized by an integer value - higher values have a higher priority. Please show your work. (10 pts)
queue.push("lovelace", 1)
queue.push("knuth", 1)
queue.pop()
queue.push("babbage", 10)
queue.push("church", 7)
queue.push("turing", 8)
queue.pop()
queue.pop()
queue.push("dijkstra", 5)
queue.pop()
queue.pop()
queue.pop().

Solution Preview

1. For the input word "rain", the push-pop procedure is below.
push("r");
push("a");
push("i");
push("n");
pop(); // "n" is popup
pop(); // "i" is popup
pop(); // "a" is popup
pop(); // "r" is popup
Finally, we get "niar"

2. We need to use two stacks numbers and operations.
For the expression (((10 * 9) - 3) * (((8 * 2) + 16))), we have the following operations.
operations.push("(");
operations.push("(");
operations.push("(");
numbers.push(10);
operations.push("*");
numbers.push(9);
operations.pop(); // Because it meets ")" and a calculation should start. "*" is popup
numbers.pop(); // 9 is pop up
numbers.pop(); // 10 is pop up
Calculate(10*9) and get 90
operations.pop(); // "(" is popup to match ")"
...

Solution Summary

The solution solves a stacks problem.

$2.19