Assignment For DS (Stack and Recursion)
Assignment For DS (Stack and Recursion)
NAME: S V NITHIN
SEMESTER: 3rd
Page 1 of 11
QUESTIONS
2. Write a Program to implement three stacks in a single dimension array in the most
efficient way.
3. How to find if the stack machine of your computer grows upwards or downwards?
(Stack machine: When a Program is written and executed, all the local variables of the
function are pushed into stack machine or stack (i.e., are allocated space on stack) after
the return statement of the function is encountered these elements are popped (i.e., the
space allocated to them is freed).
4. Given two sorted lists L1 and L2, write a program to compute L1∩L2 (Use Stack in the
program).
5. Given two sorted lists L1 and L2, write a program to compute L1∪L2 (Use Stack in the
program).
Page 2 of 11
8. Write a Program to evaluate Prefix Expression. (NOTE: Reversing Prefix to Postfix and
then applying the Algorithm to evaluate Postfix expression gives a wrong answer)
10. Implement Euclid’s Algorithm to find GCD of two numbers ‘a’ and ‘b’ using Recursion
11. Entries in a stack are "ordered". What is the meaning of this statement?
12. Consider the usual algorithm to convert an infix expression to a postfix expression.
Suppose that you have read 10 input characters during a conversion and that the stack
now contains these symbols:
| |
| + |
| ( |
Bottom of stack |___*___ |
Now, suppose that you read and process the 11th symbol of the input. Draw the stack for
the case where the 11th symbol is:
a. A number:
b. A left parenthesis:
c. A right parenthesis:
d. A minus sign:
e. A division sign:
Page 3 of 11
13. Consider the implementation of the Stack using a partially-filled array. What goes wrong
if we try to store the top of the Stack at location [0] and the bottom of the Stack at the last
used position of the array?
14. In the array version of the Stack class, which operations require linear time for their
worst-case behavior?
a. is_empty
b. peek
c. pop
15. In the linked-list version of the Stack class, which operations require linear time for their
worst-case behavior?
a. is_empty
b. peek
c. pop
d. push
16. Which one of the following is NOT shared by the threads of the same process?
Page 4 of 11
a. Stack
b. Address Space
d. Message Queue
ANSWERS
1. Correct Answer: c) 3
Solution:
Here, f(push(S,i)) indicates the value of f(S) after inserting the element i in to the stack.
Page 5 of 11
f(push(S,2)) = max(0,0) + 2 = 2 (This is f(S)after inserting element 2 on to the stack)
2. Algorithm
i. Implement the first stack in index values of type 3n, Second Stack in index
Values 3n+1 , Third Stack in index Values 3n+2. Three of them starting from 0,
1,2 index values of the array.
> If not then put the elements of the first in the position allocated
for second stack from the end, until first non-empty element of
array is encountered. Also, count the number of elements of first
stack entered in the positions allocated to second stack
>If the second stack is full then follow the above procedure for
positions allocated to third stack
b. Else
> Insert element in the node 3 nodes after the current node
> If not then put the elements of the first in the position allocated
for third stack from the end, until first non-empty element of
array is encountered. Also, count the number of elements of first
stack entered in the positions allocated to second stack
Page 6 of 11
>If the second stack is full then follow the above procedure for
positions allocated to first stack
b. Else
> Insert element in the node 3 nodes after the current node
> If not then put the elements of the first in the position allocated
for third stack from the end, until first non-empty element of
array is encountered. Also, count the number of elements of first
stack entered in the positions allocated to second stack
>If the second stack is full then follow the above procedure for
positions allocated to first stack
b. Else
> Insert element in the node 3 nodes after the current node
vi. While printing print the elements of the first stack in 3n node, followed by elements in
the nodes allocated to second stack (by using the count variable which has counted the
number of elements of first stack in nodes allocated to second stack), in nodes allocated
to third stack.
vii. Similarly, While printing print the elements of the second stack in 3n+1 node,
followed by elements in the nodes allocated to third stack (by using the count variable
which has counted the number of elements of first stack in nodes allocated to second
stack), in nodes allocated to first stack.
Page 7 of 11
3. Program:
#include<stdio.h>
int main()
int i;
function(&i);
return 0;
int j;
if(&j<p)
else
Page 8 of 11
return;
4. Algorithm:
ii. Repeat the following steps until all elements of L1 are scanned:
b. Compare the above element with elements of the L2 that are less than the above
element. If any of them matches then pop the element on the top of stack. If
another element in L2 resembles that on Stack then DO NOT pop.
iii. Print the elements of the stack. These correspond to the elements Unique to L1 and L2
5. Algorithm:
ii. Repeat the following steps until all elements of L1 are scanned:
b. Compare the above element with elements of the L2 that are less than the
above element and greater than the previous element of L1. The elements that
don’t match with the element considered in a. should be pushed into stack.
iii. Print the elements of the stack. These correspond to union of elements in L1 and L2
ii. Scan the Postfix expression from left to right and Repeat the following steps till the
end of postfix expression is encountered:
Page 9 of 11
of the string and then store the character in temporary variable in the
String.
iii. Pop the elements of the stack and store it into infix string.
7. Algorithm:
ii. Scan the elements of the infix expression from Right to Left until the stack is empty
> Repeatedly pop from the stack and add to character array each
operator (on the top of the stack) which has the same precedence as
or higher precedence than the above operator #
> Repeatedly pop form stack and add to p each operator ( on the
top of stack ) until a left parenthesis is encountered.
8. Algorithm:
i. Scan the Prefix expression from right to left and repeat the following steps until the
entire Prefix expression is scanned:
a. If an operand is encountered then ask the user to enter its value and then push
the value onto the stack
b. If an operator is encountered then pop two times from the stack and perform the
required operation and then pop the result onto the stack
Page 10 of 11
ii. Print the result
9. Similar to algorithm of 6. But the Prefix expression should be scanned from Right to left
10. Algorithm:
a= qb +r , where q is the quotient of a/b and r is the remainder of a/b ( i.e., a%b)
b. Else
> Replace ‘a’ by ‘b’ and ‘b’ by ‘r’ and goto step i :
11. Answer: d
13. Answer: b
14. Answer: e
15. Answer: e
16. Answer: a
Page 11 of 11