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

Stack and Queue Coding Examples

The document contains code examples for stack and queue operations in C++. It includes simulations for stack operations, string reversal using stacks, balanced parentheses checking, and basic queue operations such as pushing, popping, and checking size. Each section provides a clear implementation with comments explaining the functionality.

Uploaded by

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

Stack and Queue Coding Examples

The document contains code examples for stack and queue operations in C++. It includes simulations for stack operations, string reversal using stacks, balanced parentheses checking, and basic queue operations such as pushing, popping, and checking size. Each section provides a clear implementation with comments explaining the functionality.

Uploaded by

mashrafianam99
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

STACK & QUEUE SOLUTIONS (LINE-BY-LINE CODE)

------------------------------------------
1. Stack Operation Simulation
------------------------------------------
#include <iostream>
#include <stack>
using namespace std;

int main() {
stack<int> st; // create empty stack
[Link](5); // push 5
[Link](10); // push 10
[Link](); // pop top (10)
[Link](7); // push 7
[Link](9); // push 9
[Link](); // pop 9
cout << "Final Stack: [5, 7]";
}

------------------------------------------
2. Reverse a String Using Stack
------------------------------------------
#include <iostream>
#include <stack>
using namespace std;

int main() {
string s = "HELLO"; // original string
stack<char> st; // create stack of characters

for(char c : s) // push all characters


[Link](c);

string rev=""; // reversed string


while(![Link]()) { // pop characters to reverse
rev += [Link]();
[Link]();
}

cout << "Reversed String: " << rev;


}

------------------------------------------
3. Balanced Parentheses Check
------------------------------------------
#include <iostream>
#include <stack>
using namespace std;

int main() {
string exp = "((a+b)*(c+d))"; // given expression
stack<char> st;

for(char c : exp) {
if(c=='(') [Link](c); // push opening bracket
else if(c==')') { // if closing bracket
if([Link]()) { // no matching open
cout<<"Not Balanced";
return 0;
}
[Link](); // match found
}
}

if([Link]()) cout<<"Balanced";
else cout<<"Not Balanced";
}

------------------------------------------
4. Find the Top Element
------------------------------------------
#include <iostream>
#include <stack>
using namespace std;

int main() {
stack<int> st;
[Link](2); // push 2
[Link](4); // push 4
[Link](6); // push 6
[Link](); // remove 6
[Link](10); // push 10

cout << "Top Element: " << [Link]();


}

------------------------------------------
5. Infix to Postfix (A + B)
------------------------------------------
#include <iostream>
using namespace std;

int main() {
cout << "Postfix: AB+";
}

------------------------------------------
QUEUE SOLUTIONS
------------------------------------------
1. Basic Queue Operations
------------------------------------------
#include <iostream>
#include <queue>
using namespace std;

int main() {
queue<int> q;
[Link](10); // push 10
[Link](20); // push 20
[Link](30); // push 30
[Link](); // pop front (10)
[Link](40); // push 40

cout << "Final Queue (front → rear): ";


while(![Link]()) { // print all
cout << [Link]() << " ";
[Link]();
}
}

------------------------------------------
2. Print Front and Back
------------------------------------------
#include <iostream>
#include <queue>
using namespace std;

int main() {
queue<int> q;
[Link](5);
[Link](15);
[Link](25);
[Link](35);

cout << "Front: " << [Link]() << endl;


cout << "Back: " << [Link]();
}

------------------------------------------
3. Queue Size + Empty Check
------------------------------------------
#include <iostream>
#include <queue>
using namespace std;

int main() {
queue<int> q;
[Link](100);
[Link](200);
[Link](300);

cout << "Queue Size: " << [Link]() << endl;


cout << "Is Empty: " << ([Link]() ? "Yes" : "No");
}

------------------------------------------
4. Print While Popping
------------------------------------------
#include <iostream>
#include <queue>
using namespace std;

int main() {
queue<int> q;
[Link](11);
[Link](22);
[Link](33);
[Link](44);

cout << "Queue Contents: ";


while(![Link]()) {
cout << [Link]() << " ";
[Link]();
}
}

------------------------------------------
5. Count Elements Greater Than X
------------------------------------------
#include <iostream>
#include <queue>
using namespace std;

int main() {
queue<int> q;
[Link](7);
[Link](14);
[Link](3);
[Link](20);
[Link](9);

int x = 10, cnt = 0;

queue<int> temp = q; // copy queue for scanning

while(![Link]()) {
if([Link]() > x)
cnt++;
[Link]();
}

cout << "Elements greater than 10: " << cnt;


}

You might also like