Assignment for DS ( Stack and Recursion )

download Assignment for DS ( Stack and Recursion )

of 11

Transcript of Assignment for DS ( Stack and Recursion )

  • 8/8/2019 Assignment for DS ( Stack and Recursion )

    1/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 of11

  • 8/8/2019 Assignment for DS ( Stack and Recursion )

    2/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 sortedlists L1 and L2, write a program to compute L1L2 (Use Stack in the

    program).

    5. Given two sorted lists L1 and L2, write a program to compute L1L2 (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 expressionto Prefix expression. (NOTE: Prefix

    expression is not reverse of Postfix expression)

    Page 2 of11

  • 8/8/2019 Assignment for DS ( Stack and Recursion )

    3/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 Euclids 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 '

  • 8/8/2019 Assignment for DS ( Stack and Recursion )

    4/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 theirworst-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 of11

  • 8/8/2019 Assignment for DS ( Stack and Recursion )

    5/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 of11

  • 8/8/2019 Assignment for DS ( Stack and Recursion )

    6/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 = 3f(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 forpositions 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 of11

  • 8/8/2019 Assignment for DS ( Stack and Recursion )

    7/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 firststack 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 variablewhich 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 of11

  • 8/8/2019 Assignment for DS ( Stack and Recursion )

    8/11

    3. Program:

    #include

    void function(int *p);

    int main()

    {

    int i;

    function(&i);

    return 0;

    }

    void function( int *p)

    {

    int j;

    if(&j

  • 8/8/2019 Assignment for DS ( Stack and Recursion )

    9/11

  • 8/8/2019 Assignment for DS ( Stack and Recursion )

    10/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. Ifleft 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 rightparenthesis

    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 theentire 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 of11

  • 8/8/2019 Assignment for DS ( Stack and Recursion )

    11/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 of11