Aryaman Sharma Worksheet 8 and 9 (20bcs4206)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Department of AIT-CSE

Institutional Training
June-July, 2021
Department of AIT-CSE
Daily Worksheet - Day-8 and 9
STACK DATA STRUCTURES

Name: Aryaman Sharma


UID: 20BCS4206
Class: IT21_AITCC2_B

**************************************************************

Question 1: Following is C like pseudo code of a function that takes a number as


an argument, and uses a stack S to do processing.

void fun(int n)
{
    Stack S;  // Say it creates an empty stack S
    while (n > 0)
    {
      // This line pushes the value of n%2 to stack S
      push(&S, n%2);
 
      n = n/2;
    }
 
    // Run while Stack S is not empty
    while (!isEmpty(&S))
      printf("%d ", pop(&S)); // pop an element from S and print it
}
Department of AIT-CSE

Which one of the following is an application of Stack Data Structure?


A Managing function calls
B The stock span problem
C Arithmetic expression evaluation
D All of the above
E None of the above

Answer: The correct option is D i.e. All of the above.

**************************************************************

Question 2: Assume that the operators +, -, × are left associative and ^ is right
associative. The order of precedence (from highest to lowest) is ^, x , +, -. The
postfix expression corresponding to the infix expression a + b × c - d ^ e ^ f is

Answer: Since ^ is right associative


a+b×c-d^ef^
a+b×c-def^^
Now x has the highest precedence
a+bc×-def^^
Now + and – has the same precedence, so we start from left
abc×+-def^^
abc×+def^^- (This is the answer to the question)

**************************************************************

Question 3: Pankaj and Mythili were both asked to write the code to evaluate the
following expression:

a - b+ c/(a-b) + (a-b)^2

Pankaj writes the following code statements (Code A):

print (a-b) + c/(a-b) + (a-b)*(a-b)

Mythili writes the following code statements (Code B):

d = (a-b)
Department of AIT-CSE

print d + c/d + d*d

If the time taken to load a value in a variable, for addition, multiplication or


division between two operands is same, which of the following is true?

a) Code A uses lesser memory and is slower than Code B

b) Code A uses lesser memory and is faster than Code B

c) Code A uses more memory and is faster than Code B

d) Code A uses more memory and is slower than Code B

Answer: The correct option is A i.e. Code A uses lesser memory and is slower than
Code B.

Code A uses less memory as it has only 3 variables whereas in Code B there are 4
variables and Code A is slower because compiler has to calculate ( a – b ) every
time whereas in Code B ( a – b ) value is stored in another variable, so compiler
does not have to calculate every time.

**************************************************************

Question 4: A pseudo-code is used. Assume that when two data-types are


processed through an operator, the answer maintains the same data-type as the
input data-types. Assume that all data-types have enough range to accommodate
any number. If two different data-types are operated on, the result assumes the
more expressive data-type.

What will be the output of the following pseudo-code statements?

Integer a = 456, b, c, d =10

b = a/d

c=a-b

print c

a) 410
Department of AIT-CSE

b) 410.4

c) 411.4

d) 411

Answer: The correct answer is A i.e. 410

As b = 456/10 = 45.6 = 45 ( a and d are int variables and b is also int


variable so answer has to come in int only )
Then c = 456 - 45 = 410

**************************************************************

Question 5: Implement Queue using Stacks

Answer:
Department of AIT-CSE

**************************************************************

Question 6: Implement a stack using single queue

Answer:
Department of AIT-CSE

**************************************************************

You might also like