Applications of Stack
Infix Notation
- The operator is written in between the operands, e.g. A+B.
- The reason why this notation is called infix is the place of operator in the expression.
Prefix Notation
- In which the operator is written before the operands, e.g. +AB.
- It is also called as polish notation.
Postfix Notation
- The operator is written after the operands, e.g. AB+.
- It is also known as suffix or reverse polish notation.
- Postfix notation is the type of notation which is most suitable for a computer to calculate any expression. It is universally accepted notation for designing arithmetic and logical unit (ALU) of the CPU.
- Any expression entered into the computer is first converted into postfix notation, stored in stack and then claculated.
- Convert the following infix expression into its equivalent postfix expression:
(A+B^D)/(E-F)+G - ABD^+EF-/G+
- ABD+^EF-/G+
- ABD+^EF/-G+
- ABD^+EF/-G+
- Convert the following infix to postfix expression:
W+X*Y+(Z*A) - WXY+*ZA*+
- WXY*+(za)*+
- WXY*+ZA*+
- W+XY*ZA
- Evaluate the given postfix expression:
2 3 + 5 * 2 3 + 4 + * - 210
- 225
- 220
- 200
- What is the output of the prefix expression:
+ - * 7 2 / 9 3 1 - 13
- 10
- 11
- 12
Recursion
- Recursion is one of the most powerful tool in a programming language all it requires is to specify a reasonable condition and instruction set from right logic to solve a problem.
- A recursive function with the following two properties is said to be well defined.
- There must be certain arguments, called base values, for which the function does not refer to itself.
- Each time the function does refer to itself, the argument of the function must be closer to a base value.
- Examples of recursion:
- Factorial function
- Fibonacci sequence
- Divide and conquer algorithms
- Find the output of the following pseudo code.
- Find the output of the following pseudo code.
- Find the output of the following pseudo code.
- Find the output of the following pseudo code on n = 6.
- Consider the following recursive C function. If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main()?
- 15
- 25
- 35
- 45
- Consider the following recursive C function that takes two arguments. What is the return value of the function foo() when it is called as foo(345, 10)?
- 345
- 12
- 5
- 3
- Consider the following recursive C function that takes two arguments. What is the return value of the function foo() when it is called as foo(513, 2)?
- 9
- 8
- 5
- 2
- Consider the following recursive function, what is the value of fun(3,4)?
- 13
- 12
- 9
- 10
- What does the following function do?
- x + y
- x + x * y
- x * y
- x ^ y
- What does the following function do?
- It returns 1 when n is a multiple of 3, otherwise returns 0
- It returns 1 when n is a power of 3, otherwise returns 0
- It returns 0 when n is a multiple of 3, otherwise returns 1
- It returns 0 when n is a power of 3, otherwise returns 1
void fun(int x)
{
if (x > 0)
{
printf("%d ", x);
fun(x - 1);
}
}
void main()
{
fun(4);
}
void fun(int x)
{
if (x > 0)
{
fun(x - 1);
printf("%d ", x);
}
}
void main()
{
fun(4);
}
void fun(int x)
{
if (x > 0)
{
printf("%d ", x);
fun(x - 1);
printf("%d ", x);
fun(x - 1);
printf("%d ", x);
}
}
void main()
{
fun(3);
}
int x(int n)
{
if (n < 3)
return 1;
else
return x(n - 1) + x(n - 1) + 1;
}
void get(int n)
{
if (n < 1)
return;
get(n - 1);
get(n - 3);
printf("%d", n);
}
unsigned int foo(unsigned int n, unsigned int r)
{
if (n > 0)
return (n % r + foo(n / r, r));
else
return 0;
}
unsigned int foo(unsigned int n, unsigned int r)
{
if (n > 0)
return (n % r + foo(n / r, r));
else
return 0;
}
int fun(int x, int y)
{
if (x == 0)
return y;
return fun(x - 1, x + y);
}
int fun(int x, int y)
{
if (y == 0)
return 0;
return (x + fun(x, y - 1));
}
int fun(unsigned int n)
{
if (n == 0 || n == 1)
return n;
if (n % 3 != 0)
return 0;
return fun(n / 3);
}
Comments
Post a Comment