DS Quiz 2 - Written - Solution.docx
DS Quiz 2 - Written - Solution.docx
DS Quiz 2 - Written - Solution.docx
Write the output of the following code snippets. The std queue uses “push” for its
enqueue operation and “pop” for its dequeue operation (2+7 marks).
#include <iostream> 15 20
#include <queue>
using namespace std;
int main() {
queue<int> myQueue;
myQueue.push(5);
myQueue.push(10);
myQueue.push(15);
myQueue.pop();
myQueue.push(20);
myQueue.pop();
while (!myQueue.empty()) {
cout << myQueue.front() << " ";
myQueue.pop();
}
return 0;
}
#include <iostream> 10
#include <stack>
#include <queue>
using namespace std;
int main() {
stack<int> myStack;
queue<int> myQueue;
myStack.push(20);
myStack.push(5);
myStack.push(15);
myStack.push(10);
myStack.push(25);
while (!myStack.empty()) {
int element = myStack.top();
myStack.pop();
if (element % 10 == 0) {
myQueue.push(element);
}
}
cout << myQueue.front();
return 0;
}
#include <iostream> 9
#include <stack>
using namespace std;
int main() {
stack<int> stack1;
stack<int> stack2;
stack1.push(9);
stack1.push(8);
stack1.push(10);
stack1.push(7);
stack1.push(2);
while (!stack1.empty()) {
int element = stack1.top();
stack1.pop();
stack2.push(element);
}
cout << stack2.top();
return 0;
}
#include <iostream> euuaa
#include <stack>
#include <string>
using namespace std;
while (!aStack.empty()) {
cout << aStack.top() << " ";
aStack.pop();
}
}
int main() {
string input = "data structures";
doSomething(input);
return 0;
}
#include <iostream> True
#include <stack>
#include <string>
using namespace std;
bool isSomething(string& str) {
stack<char> charStack;
for (int i = 0; i < str.length(); i++) {
charStack.push(str[i]);
}
for (int i = 0; i < str.length(); i++) {
char ch = str[i];
if (ch != charStack.top()) {
return false;
}
charStack.pop();
}
return true;
}
int main() {
string a = "racecar";
if (isSomething(a))
cout << "True!";
else
cout << "False!";
return 0;
}
#include <iostream> s
#include <stack>
#include <string>
using namespace std;
stack<char> charStack; // global stack declaration
bool isSomething(string& str) {
for (int i = 0; i < str.length(); i++) {
charStack.push(str[i]);
}
int main() {
string a = "structures";
isSomething(a);
cout << charStack.top();
return 0;
}
#include <iostream> 7
#include <stack>
#include <queue>
using namespace std;
int main() {
stack<int> myStack;
queue<int> myQueue;
myStack.push(1);
myStack.push(2);
myStack.push(3);
myQueue.push(4);
myQueue.push(5);
myQueue.push(6);
myStack.pop();
myStack.pop();
myQueue.pop();
myQueue.pop();
while (!input.empty())
{
int tmp = input.top();
input.pop();
return tmpStack;
}
int main()
{
stack<int> input;
input.push(34);
input.push(75);
input.push(92);
input.push(23);
while (!tmpStack.empty())
{
cout << tmpStack.top() << " ";
tmpStack.pop();
}
}