Stack Questions

These questions are generally asked in university exam

infix, prefix based stack question

1. Convert the infix expression (a-b/c)*(a/c-d) to prefix expression.

Answer:

To convert the infix expression to prefix, we first convert it to postfix and then reverse it. The given infix expression is:


(a-b/c)*(a/c-d).

Thus, the prefix expression is *-a/bc-/acd .

2. Evaluate the postfix expression "5 6 2 + * 12 4 / -".

Answer:

To evaluate a postfix expression, we scan from left to right and apply operators to operands.

Final result is 37.

3. Convert the prefix expression +A*BCD to infix.

Answer:

To convert prefix to infix, we process from right to left.

Thus, the infix expression is (A+(B*C)).

4. What is the result of pushing the elements 3, 5, and 7 into a stack and then popping two elements?

Answer:

When elements 3, 5, and 7 are pushed into the stack, the stack looks like this:
Top -> 7, 5, 3.
If we pop two elements, 7 and 5 will be removed, leaving 3 at the top. So, the final state of the stack is:
Top -> 3.

5. Convert the infix expression A*(B+C)/D to postfix expression.

Answer:

The given infix expression is A*(B+C)/D.

To convert to postfix:

Thus, the postfix expression is ABC+*D/.

6. Implement a stack using arrays in C.

Answer:

In C, a stack can be implemented using arrays as follows:

          #define MAX 100
          int stack[MAX], top = -1;
          
          void push(int x) {
              if (top == MAX - 1) {
                  printf("Stack Overflow\n");
              } else {
                  stack[++top] = x;
              }
          }
          
          int pop() {
              if (top == -1) {
                  printf("Stack Underflow\n");
                  return -1;
              } else {
                  return stack[top--];
              }
          }
          

This code defines a stack with push and pop operations using arrays.

7. What are the time complexities of push and pop operations in a stack?

Answer:

Both the push and pop operations in a stack take constant time because inserting or deleting an element from the top of the stack does not depend on the number of elements in the stack.

8. Convert the postfix expression "AB+C*D-" to infix.

Answer:

To convert a postfix expression to infix, we process from left to right.

Thus, the infix expression is (((A+B)*C)-D).

9. Explain how to check for balanced parentheses using a stack.

Answer:

To check for balanced parentheses, we use a stack:

10. Convert the prefix expression "*+AB-CD" to infix.

Answer:

To convert prefix to infix, we process from right to left.

Thus, the infix expression is ((A+B)*(C-D)).

11. How can a stack be used to reverse a string?

Answer:

To reverse a string using a stack:

Since the stack is LIFO (Last In First Out), the order of characters will be reversed.

12. Convert the infix expression "A/B+C*(D-E)" to postfix.

Answer:

The given infix expression is A/B+C*(D-E).
To convert to postfix:

Thus, the postfix expression is AB/CDE-*+.

13. What is the main difference between stack and queue?

Answer:

The main difference between a stack and a queue lies in how elements are removed:

14. Convert the postfix expression "ABC/-AK/L-*" to infix.

Answer:

To convert postfix to infix, process from left to right.

Thus, the infix expression is ((A-(B/C))*((A/K)-L)).

15. Explain stack overflow and how it occurs.

Answer:

A stack overflow occurs when there is an attempt to push an element onto a stack that is already full. Since a stack has a finite size, trying to add more elements than it can hold causes an overflow, which can result in program crashes or unexpected behavior.

16. Convert the prefix expression "-*+ABC/DE" to infix.

Answer:

To convert prefix to infix, process from right to left.

Thus, the infix expression is ((A+B*C)-(D/E)).

17. Write a program to check for balanced parentheses using a stack in C.

Answer:

The following C program checks for balanced parentheses using a stack:

          #include <stdio.h>
          #include <string.h>
          
          #define MAX 100
          char stack[MAX];
          int top = -1;
          
          void push(char c) {
              stack[++top] = c;
          }
          
          char pop() {
              return stack[top--];
          }
          
          int is_balanced(char* expr) {
              for (int i = 0; i < strlen(expr); i++) {
                  if (expr[i] == '(') {
                      push('(');
                  } else if (expr[i] == ')') {
                      if (top == -1) return 0;
                      pop();
                  }
              }
              return (top == -1);
          }
          
          int main() {
              char expr[] = "(A+B)*(C-D)";
              if (is_balanced(expr)) {
                  printf("Balanced\n");
              } else {
                  printf("Not Balanced\n");
              }
              return 0;
          }
        

18. How can a stack be used in function recursion?

Answer:

In function recursion, each function call is pushed onto a call stack. When the function returns, its call is popped from the stack. This enables the program to track where it left off and resume from that point when the recursion unwinds.

19. Convert the postfix expression "AB+CD+*" to infix.

Answer:

To convert postfix to infix, process from left to right.

Thus, the infix expression is (A+B)*(C+D).

20. Describe how to implement a queue using two stacks.

Answer:

To implement a queue using two stacks: