You are on page 1of 11

DATA STRUCTURES ASSIGNMENT

TOPIC: STACK AND RECURSION

NAME: S V NITHIN

ROLL NO: 09CO81

SEMESTER: 3rd

DATE OF SUBMISSION: 26 Oct 2010

Page 1 of 11
QUESTIONS

1. A function f defined on stacks of integers satisfies the following properties. f(fi)=0 ( I


think fi is the Greek alphabet indicating NULL set) and f(push(S,i)) =max(f(S),0)+i for
all stacks S and integers i. If a stack S contains the integers 2, -3, 2, -1, 2 in order from
bottom to top, what is f(S)?
a) 6
b) 4
c) 3
d) 2

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).

6. Write a Program to convert Postfix expression to Infix expression. (Note: It is converting


from Infix to Postfix)

7. Write a Program to convert Infix expression to Prefix expression. (NOTE: Prefix


expression is not reverse of Postfix expression)

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)

9. Write a Program to convert Prefix Expression to Infix Expression.

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?

a. Collection of Stacks can be sorted.

b. Stack entries may be compared with the '<' operation.

c. The entries must be stored in a linked list.

d. There is a first entry, a second entry, and so on.

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?

a. Both peek and pop would require linear time.

b. Both push and pop would require linear time.

c. The Stack could not be used to check balanced parentheses.

d. The Stack could not be used to evaluate postfix expressions.

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

d. push when the stack is below capacity

e. None of these operations require linear time

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

e. None of these operations require linear time

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

c. File Descriptor Table

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.

Initially when no element is inserted in to the stack f(S) = 0

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)

f(push(S,-3)) = max(2,0) + (-3) = -1


f(push(S,2)) = max(-1,0) + 2 = 2
f(push(S,-1)) = max(2,0) + (-1) = 1
f(push(S,2)) = max(1,0) + 2 = 3
f(S) after inserting all the elements is 3.

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.

ii. Definition of INSERT1( ): [Function to insert element in the first stack ]

a. If First Stack is full

> Check if the second stack if full or not

> 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

iii. Definition of INSERT2( ): [Function to insert element in the second stack ]

a. If Second Stack if full

> Check if the third stack if full or not

> 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

iv. Definition of INSERT3( ): [Function to insert element in the second stack ]

a. If Third Stack is full

> Check if the first stack if full or not

> 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

v. Definition of DELETE1( ) , DELETE2( ) , DELETE3( ) are simple

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.

vii. Similarly, for Third stack

Page 7 of 11
3. Program:

#include<stdio.h>

void function(int *p);

int main()

int i;

function(&i);

return 0;

void function( int *p)

int j;

if(&j<p)

printf(“Stack grows upwards\n”);

else

printf(“Stack grows downwards\n”);

Page 8 of 11
return;

4. Algorithm:

i. Read the elements of the linked list L1 and L2

ii. Repeat the following steps until all elements of L1 are scanned:

a. Push the element of the L1 onto a stack

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:

i. Read the elements of L1 and L2

ii. Repeat the following steps until all elements of L1 are scanned:

a. Push the element of L1 onto a Stack

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

6. i. Read the Postfix Expression

ii. Scan the Postfix expression from left to right and Repeat the following steps till the
end of postfix expression is encountered:

a. If the element encountered is an Alphabet then push it into a Character


Stack.

b. If the element encountered is an operator then pop the first element of


stack and store it in a temporary char variable. Pop the second element of
the stack and put it into a string. Store the operator in the next index value

Page 9 of 11
of the string and then store the character in temporary variable in the
String.

c. Push the entire string onto a stack

iii. Pop the elements of the stack and store it into infix string.

iv. Print the infix string.

7. Algorithm:

i. Read the infix expression from user

ii. Scan the elements of the infix expression from Right to Left until the stack is empty

a. If the operand is encountered, add to a character array

b. If a right parenthesis is encountered, push it onto STACK.

c. If operator # is encountered then

> 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 #

> Add the operator # to the stack

d. If left parenthesis is encountered, then:

> Repeatedly pop form stack and add to p each operator ( on the
top of stack ) until a left parenthesis is encountered.

> remove the right parenthesis

iv. Reverse the character array

v. The reversed character array is the Prefix expression

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:

i. Express ‘a’ in terms of ‘b’ as follows:

a= qb +r , where q is the quotient of a/b and r is the remainder of a/b ( i.e., a%b)

ii. a. Check If r = 0, stop then goto step iii.

b. Else

> Replace ‘a’ by ‘b’ and ‘b’ by ‘r’ and goto step i :

iii. GCD = r, Print r

11. Answer: d

12. Direct method

13. Answer: b

14. Answer: e

15. Answer: e

16. Answer: a

Page 11 of 11

You might also like