StackQueuel Questions With Solutions
StackQueuel Questions With Solutions
CO1: Articulate the design, use and associated algorithms of fundamental and abstract data structures
CO2: Examine various operations performed on and using Stack and Queue data structures.
CO4: Build optimized solutions for real-word programming problems using efficient data structures.
Question1. If the elements “A”, “B”, “C” and “D” are placed in a queue and are deleted one at a
time, in what order will they be removed?
a) ABCD
b) DCBA
c) DCAB
d) ABDC
Answer: a
Explanation: Queue follows FIFO approach. i.e. First in First Out Approach. So, the order of
removal elements are ABCD.
Question2. A normal queue, if implemented using an array of size MAX_SIZE, gets full when?
a) Rear = MAX_SIZE – 1
b) Front = (rear + 1)mod MAX_SIZE
c) Front = rear + 1
d) Rear = front
Answer: a
Explanation: When Rear = MAX_SIZE – 1, there will be no space left for the elements to be
added in queue. Thus queue becomes full.
Question3. What is the value of the postfix expression 6 3 2 4 + – *?
a) 1
b) 40
c) 74
d) -18
Answer: d
Explanation: Postfix Expression is (6*(3-(2+4))) which results -18 as output.
Example:
Input: str = “GeeksQuiz”
Output: ziuQskeeG
Solution:
Code:
// C program to reverse a string using stack
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
reverse(str);
printf("Reversed string is %s", str);
return 0;
}
Question2. Given an expression string exp, write a program to examine whether the pairs and
the orders of “{“, “}”, “(“, “)”, “[“, “]” are correct in the given expression.
Example:
Input: exp = “[()]{}{[()()]()}”
Output: Balanced
Explanation: all the brackets are well-formed
Solution:
Code:
// Driver code
int main()
{
string expr = "{()}[]";
// Function call
if (areBracketsBalanced(expr))
cout << "Balanced";
else
cout << "Not Balanced";
return 0;
}
Question 3. Given a stack of integers, sort it in ascending order using another temporary stack.
Examples:
Input : [3, 5, 1, 4, 2, 8]
Output : [1, 2, 3, 4, 5, 8]
Solution:
Code:
// C++ program to sort a stack using an
// auxiliary stack.
#include <bits/stdc++.h>
using namespace std;
// This function return the sorted stack
stack<int> sortStack(stack<int> &input)
{
stack<int> tmpStack;
while (!input.empty())
{
// pop out the first element
int tmp = input.top();
input.pop();
return tmpStack;
}
// main function
int main()
{
stack<int> input;
input.push(34);
input.push(3);
input.push(31);
input.push(98);
input.push(92);
input.push(23);
while (!tmpStack.empty())
{
cout << tmpStack.top()<< " ";
tmpStack.pop();
}
}
Question1. Given a stack, the task is to reverse the stack using the queue data structure.
Examples:
Solution:
Pseudocode:
// C++ code to reverse a stack using queue
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
stack<int> stk;
stk.push(40);
stk.push(30);
stk.push(20);
stk.push(10);
// Function Call
reverse(stk);
// 40 30 20 10 (top to bottom)
cout << "After Reverse : (Top to Bottom)"
<< "\n";
while (!stk.empty()) {
cout << stk.top() << " ";
stk.pop();
}
cout << "\n";
return 0;
}
Question2. Given a postfix expression, the task is to evaluate the postfix expression.
Input: str = “2 3 1 * + 9 -“
Output: -4
Output: 757
Solution:
Pseudocode:
// C++ program to evaluate value of a postfix expression
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
string exp = "231*+9-";
// Function call
cout << "postfix evaluation: " << evaluatePostfix(exp);
return 0;
}
Input: A + B * C + D
Output: ABC*+D+
Solution
Pseudocode:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
result[resultIndex] = '\0';
printf("%s\n", result);
}
// Driver code
int main() {
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
// Function call
infixToPostfix(exp);
return 0;
}