0% found this document useful (0 votes)
30 views4 pages

CCVB

This document contains 4 questions and code snippets that demonstrate using stacks in C++. Question 1 shows code to print the contents of a stack. Question 2 finds the maximum value in a stack. Question 3 reverses a string by pushing characters to a stack and popping them off. Question 4 separates integers in a stack into even and odd stacks.
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)
30 views4 pages

CCVB

This document contains 4 questions and code snippets that demonstrate using stacks in C++. Question 1 shows code to print the contents of a stack. Question 2 finds the maximum value in a stack. Question 3 reverses a string by pushing characters to a stack and popping them off. Question 4 separates integers in a stack into even and odd stacks.
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/ 4

LAB2:

BY:

Q1:

Write a c++ program to print the contents of the stack?

THIS CODE WILL WORK ON ANY GIVEN STACK

#include <iostream>

#include <stack>

using namespace std;

template<typename T>

void printStack(stack<T> st) {

while (!st.empty()) {

cout << st.top() << " ";

st.pop();

cout << endl;

int main() {

cout << "Contents of the stack: ";

printStack(myStack);

return 0;

Q2:

#include <iostream>

#include <stack>

using namespace std;


template <typename T>

T findMaxInStack(stack<T> st) {

if (st.empty()) {

cout << "Stack is empty.\n";

return T();

T maxVal = st.top();

st.pop();

while (!st.empty()) {

if (st.top() > maxVal) {

maxVal = st.top();

st.pop();

return maxVal;

int main() {

int maxElement = findMaxInStack(myStack);

cout << "Maximum element in the stack: " << maxElement << endl;

return 0;

Q3:

Write a c++ program to print the word “ Welcome in FET” in revere order?

#include <iostream>

#include <iostream>

#include <stack>

#include <string>

using namespace std;


int main() {

string message = "Welcome in FET";

cout << "Original Message: " << message << endl;

stack<char> charStack;

for (char ch : message) {

charStack.push(ch);

cout << "Reversed Message: ";

while (!charStack.empty()) {

cout << charStack.top();

charStack.pop();

cout << endl;

return 0;

Q4:

Write a program in C++ to separate integers stored in a stack into two stacks one for the even integers
and the other for odd ones.

#include <iostream>

#include <stack>

using namespace std;

int main() {

stack<int> mainStack;

stack<int> evenStack;

stack<int> oddStack;

mainStack.push(10);

mainStack.push(5);

mainStack.push(8);
mainStack.push(15);

mainStack.push(12);

while (!mainStack.empty()) {

if (mainStack.top() % 2 == 0) {

evenStack.push(mainStack.top());

} else {

oddStack.push(mainStack.top());

mainStack.pop();

cout << "Even integers: ";

while (!evenStack.empty()) {

cout << evenStack.top() << " ";

evenStack.pop();

cout << "\nOdd integers: ";

while (!oddStack.empty()) {

cout << oddStack.top() << " ";

oddStack.pop();

} cout << endl;

return 0;

You might also like