DS Quiz 2 - Written - Solution.docx

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Data Structures (BNUxITM) Instructor: Huda Sarfraz

Quiz No. 3 Max Marks: 15


Date: 28-Dec-2024 Time Allowed: 30 min

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;

bool isSomething(char ch) {


return (ch == 'a' || ch == 'e' || ch == 'i' || ch ==
'o' || ch == 'u');
}

void doSomething(string& inputString) {


stack<char> aStack;

for (char ch : inputString) {


if (isSomething(ch)) {
aStack.push(ch);
}
}

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]);
}

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 = "data structures";
isSomething(a);
cout << charStack.top();
return 0;
}
#include <iostream> e
#include <stack>
#include <string>
using namespace std;
stack<char> charStack;
bool isSomething(string& str) {
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 = "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();

cout << myQueue.front() + myStack.top();


return 0;
}
Write down the output of the following program (6 marks).

stack<int> foo(stack<int>& input)


{
stack<int> tmpStack;

while (!input.empty())
{
int tmp = input.top();
input.pop();

while (!tmpStack.empty() && tmpStack.top() > tmp)


{
input.push(tmpStack.top());
tmpStack.pop();
}
tmpStack.push(tmp);
}

return tmpStack;
}

int main()
{
stack<int> input;
input.push(34);
input.push(75);
input.push(92);
input.push(23);

stack<int> tmpStack = foo(input);

while (!tmpStack.empty())
{
cout << tmpStack.top() << " ";
tmpStack.pop();
}
}

92 75 34 23 (sorts the stack)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy