0% found this document useful (0 votes)
110 views

Activity5 - Stacks - Answers

The document provides examples and explanations of operations that can be performed on stacks, including: 1. Converting infix, postfix, and prefix expressions. 2. Writing functions to print all elements in a stack, count the number of elements in a stack, clone one stack to another, and check if two stacks are equal. 3. Combining two sorted stacks into a single sorted stack. 4. Writing functions to remove a specified number of elements from a stack, and to insert a new element into a stack while maintaining descending element order. Pseudocode solutions are provided for each problem using stacks.

Uploaded by

Erwin Chen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Activity5 - Stacks - Answers

The document provides examples and explanations of operations that can be performed on stacks, including: 1. Converting infix, postfix, and prefix expressions. 2. Writing functions to print all elements in a stack, count the number of elements in a stack, clone one stack to another, and check if two stacks are equal. 3. Combining two sorted stacks into a single sorted stack. 4. Writing functions to remove a specified number of elements from a stack, and to insert a new element into a stack while maintaining descending element order. Pseudocode solutions are provided for each problem using stacks.

Uploaded by

Erwin Chen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

P a g e |1

Activity Sheet -- On Stacks

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.

a) A * D – B + C b) A / B * C + D c) (E + A+B) * C – D * F + C d) (A-2 * (B+C) – D * E)*F

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.

Precondition: Given Stack S


pop (S);
pop (S);
printf(“%c”, pop (S);

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.

void printOrder (Stack S)


{ Stack tempS;
create (tempS);
while (!isEmpty(S))
push (tempS, pop (S));
while (!isEmpty (tempS))
{ printf(“%d”, top (tempS);
push (S, pop (tempS));
}
}

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)

int countElements (Stack S1)


{ Stack tempS;
int count = 0;
create (tempS);
while (!isEmpty(S1))
{ push (tempS, pop (S1));
count++;
}
while (!isEmpty (tempS))
push (S1, pop (tempS));
return count;
}
P a g e |2

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.

void cloneStack(Stack Original, Stack Clone)


{ Stack tempS;
create (tempS);
while (!isEmpty (Original))
push (tempS, pop (Original));
while (!isEmpty (tempS))
{
push (Original, top (tempS));
push (Clone, pop (tempS));
}
}

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.

Precondition: Stacks contain integer values


int equalStack (Stack S1, Stack S2)
{ int equal = 1;
Stack TS1, TS2;
int x, y;
int c1 = 0, c2 =0;
//transfer two stacks to temporary stacks
while (!isEmpty (S1))
{ push (TS1, pop (S1));
c1++;
}
while (!isEmpty (S2))
{ push (TS2, pop (S2));
c2++;
}
if (c1 != c2)
equal =0;
//placing back and still check
while (!isEmpty(TS1) && !iEmpty(TS2))
{ x = pop (TS1);
y = pop (TS2);
if (x != y)
equal =0;
push (S1, x);
push (S2, y);
}
while (!isEmpty (TS1))
push (S1, pop (TS1));
while (!isEmpty (TS2))
push (S2, pop (TS2));
return equal;
}
P a g e |3

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.

Consider the diagram below for your reference:


Use the following function template:
Stack CombineTwoSorted (Stack S1, Stack S2)
{ //place your declarations

//place your codes


}

public static java.util.Stack <Integer> CombineTwoSorted (java.util.Stack <Integer>


stack1, java.util.Stack <Integer> stack2)
{ int item1, item2;
java.util.Stack <Integer> returnStack = new Stack();
java.util.Stack <Integer> tempStack = new Stack();
if (stack1.empty())
return stack2;
if (stack2.empty())
return stack1;
while(!stack1.empty() && !stack2.empty()){
item1 = stack1.peek(); //top
item2 = stack2.peek();
if (item1 <= item2){
tempStack.push(stack2.pop());
}else tempStack.push (stack1.pop());
}
if (!stack1.empty()){
while (!stack1.empty())
tempStack.push(stack1.pop());
}
if (!stack2.empty()){
while (!stack2.empty())
tempStack.push(stack2.pop());
}
//return back to rseultStack
while (!tempStack.empty())
returnStack.push(tempStack.pop());
return returnStack;
P a g e |4

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:

7 10 Top void PopModified (Stack S1, int X)


5 7 { //place your declarations
3 7 //place your codes
}

public static void PopModified (java.util.Stack <Integer> stack, int X){


java.util.Stack <Integer> tempStack = new Stack();
if (X <=stack.size()){
while(!stack.empty()) //transfer to a temporary stack
tempStack.push(stack.pop());
for (int i =1; i <= X ; i++)
tempStack.pop();
while (!tempStack.empty())
stack.push(tempStack.pop());
}
}
8. Write a function InsertOrder that inserts an element in the stack and still maintain the order of values in the
stack after the insertion (i.e., elements are arranged in descending order, where the highest value is placed at
the bottom of the stack). Insertion can only be done if stack is not yet full. Otherwise, no insertion is made.
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. Study the diagrams below.

Use the following function template:

void InsertOder (Stack S1, int


newValue)
{ //place your declarations

//place your codes


}

public static void InsertOrder (java.util.Stack <Integer> stack, int newValue){


java.util.Stack <Integer> tempStack = new Stack();
int item;
if ((stack.empty()) || (newValue <= stack.peek()))
stack.push(newValue);
else
if (!stack.empty())
{
item = stack.peek();
while (newValue >=item && !stack.empty()){
tempStack.push(stack.pop());
P a g e |5

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());
}
}

You might also like