0% found this document useful (0 votes)
20 views3 pages

Unit2 Solutions

The document discusses stacks and queues, highlighting their real-world applications, such as function call management and undo operations. It explains how to implement stacks using arrays and linked lists, and provides an algorithm and example code for evaluating postfix expressions. Additionally, it outlines the differences between queues and deques, and describes how to implement a queue using two stacks.

Uploaded by

jkk3586
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Unit2 Solutions

The document discusses stacks and queues, highlighting their real-world applications, such as function call management and undo operations. It explains how to implement stacks using arrays and linked lists, and provides an algorithm and example code for evaluating postfix expressions. Additionally, it outlines the differences between queues and deques, and describes how to implement a queue using two stacks.

Uploaded by

jkk3586
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Solutions to Important Questions of Data Structures

**Unit 2: Stacks and Queues**

Stacks

1. What are the real-world applications of stacks?


Stacks are used in various real-world scenarios, including:
- Function call management in recursion.
- Undo operations in text editors.
- Parsing expressions and syntax.
- Backtracking in algorithms like maze solving and depth-first search.
- Balancing symbols in compilers.

2. How can you implement a stack using arrays and linked lists?
Implementation of a stack:

- **Using Arrays:**
1. Use a fixed-size array to store elements.
2. Maintain a variable `top` to track the index of the top element.
3. Push operation: Increment `top` and add element.
4. Pop operation: Remove element and decrement `top`.

- **Using Linked Lists:**


1. Use a linked list where each node contains a value and a pointer to the next node.
2. Push operation: Insert a node at the beginning.
3. Pop operation: Remove the first node.

3. Write a program to evaluate a postfix (Reverse Polish Notation) expression.


Algorithm:
1. Create a stack to store operands.
2. Traverse the expression:
- If the character is an operand, push it onto the stack.
- If the character is an operator, pop two operands, perform the operation, and push the
result.
3. The final value in the stack is the result.

Example in C:
```c
int evaluatePostfix(char* expr) {
Stack stack = createStack();
for (int i = 0; expr[i]; i++) {
if (isdigit(expr[i])) {
push(stack, expr[i] - '0');
} else {
int b = pop(stack);
int a = pop(stack);
switch (expr[i]) {
case '+': push(stack, a + b); break;
case '-': push(stack, a - b); break;
case '*': push(stack, a * b); break;
case '/': push(stack, a / b); break;
}
}
}
return pop(stack);
}
```

Queues

9. What is the difference between a queue and a deque?


Key Differences:
- **Queue:**
- Follows FIFO (First In, First Out) principle.
- Insertion at the rear and deletion from the front.
- **Deque:**
- Supports insertion and deletion at both ends.
- Can operate as a queue or stack.

10. How can you implement a queue using stacks?


Implementation:
- Use two stacks (stack1 and stack2).
- **Enqueue Operation:** Push element onto stack1.
- **Dequeue Operation:**
1. If stack2 is empty, transfer all elements from stack1 to stack2.
2. Pop the top element from stack2.

Example:
```c
void enqueue(Stack* stack1, int x) {
push(stack1, x);
}
int dequeue(Stack* stack1, Stack* stack2) {
if (isEmpty(stack2)) {
while (!isEmpty(stack1)) {
push(stack2, pop(stack1));
}
}
return pop(stack2);
}
```

You might also like