Activity5 - Stacks - Answers
Activity5 - Stacks - Answers
1. Transform the following infix expressions in their postfix and prefix forms.
Note: Multiplication and Division operators have higher priority over Addition and Subtraction operators. All
these have left to right associativity.
2. Change the following postfix or prefix expressions to infix. Remove any unnecessary parenthesis.
a) A B * C – D + b) A B C + * D - b) + - * A B C D d) - * A + B C D
Stack Programming
1. If a stack contains 5 items, A, B, C, D, E, with E as the last element inserted, write statement(s) that will remove
and display the 3rd item inserted in the stack.
2. Write a statement or a set of statements that will print all the elements stored in a stack, following the order as
to how these elements were inserted in the first place.
3. Write a function that returns the number of elements in a given stack S1. After the function, S1 should be
unchanged (i.e. original elements must be retained and in its original order)
4. Write function cloneStack that makes another copy of a given stack, i.e., all the items in the original stack are
copied to another stack. The values in the cloned stack should have the same values exactly at the same position
as the original stack.
5. Write function equalStacks that checks if the given two stacks are equal. The function returns 1 if the stacks are
equal, otherwise, the function returns 0. Stacks are considered as equal if they are of the same length and they
have identical elements.
6. Consider 2 stacks, each can contain integers arranged in ascending order (the smallest value is placed at the
bottom of the stack, whilst the largest value is the topmost element). Both stacks do not necessary have the same
number of elements stored. Write a function CombineTwoSorted that accepts 2 stacks as its parameters, and
returns a stack containing the combined values coming from its parameters. The combined values must also be
in ascending order, where the smallest value is placed at the bottom while the largest is the topmost element.
Assumptions:
(a) The resulting stack can have at most the combined number of elements.
(b) Either of the 2 stacks in the parameter can be empty.
(c) You are free to declare any additional stack objects, variables and use any of stack operations to facilitate in
constructing your solution.
(d) Your solution can be written either closely following C syntax or pseudocode. Make sure that every statement
in your pseudocode is clear.
(e) At the end of your function, two stacks, S1 and S2 can be empty.
7. Write a function PopModified that remove X elements from the stack. If the number of elements in the stack
is less than X (or X =0), nothing is to be done in the stack. Otherwise, remove X elements from the bottom of the
stack. You are free to declare any additional stack objects, variables and use any of stack operations to facilitate
in constructing your solution. More so, you can assume that function stackLength(Stack S) exists wherein this
function returns the number of elements in S and S remains unchanged. Consider the diagram below where X
is = 2
Top 10
7 Use the following function template:
if (!stack.empty())
item = stack.peek();
}
tempStack.push(newValue);
while (!stack.empty())
tempStack.push(stack.pop());
//return the proper order
while (!tempStack.empty())
stack.push(tempStack.pop());
}
}