LAB EXPERIMENT # 4a
Objective: The objective of this lab is to implement Stack data
structure.
What is Stack?
The STL stack provides the functionality of a stack data structure in C++. The stack data
structure follows the LIFO (Last in First Out) principle. That is, the element added last will be
removed first.
Stack Methods
In C++, the stack class provides various methods to perform different operations on a stack.
Operation Description
push() adds an element into the stack
pop() removes an element from the stack
top() returns the element at the top of the stack
DSA Lab Manual
size() returns the number of elements in the stack
empty() returns true if the stack is empty
1. Create a Stack:
#include <iostream>
#include <stack>
using namespace std;
int main() {
// create a stack of strings
stack<string> languages;
// add element to the Stack
[Link]("C++");
[Link]("C#");
[Link]("HTML/CSS");
[Link]("Python");
// print top element
cout << [Link]();
return 0;
}
Output & Reason:
2
DSA Lab Manual
2. Add Element into the Stack
#include <iostream>
#include <stack>
using namespace std;
int main() {
// create a stack of strings
stack<string> colors;
// push elements into the stack
[Link]("Red");
[Link]("Pink");
cout << "Stack: ";
// print elements of stack
while(![Link]()) {
cout << [Link]() << ", ";
[Link]();
}
return 0;
}
Output & Reason:
3
DSA Lab Manual
3. Remove Elements from the Stack
#include <iostream>
#include <stack>
using namespace std;
// function prototype for display stack utility
void display_stack(stack<string> st);
int main() {
// create a stack of strings
stack<string> colors;
// push elements into the stack
[Link]("Red");
[Link]("Pink");
[Link]("Blue");
cout << "Initial Stack: ";
// print elements of stack
display_stack(colors);
// removes "Blue" as it was inserted last
[Link]();
cout << "Final Stack: ";
// print elements of stack
display_stack(colors);
return 0;
}
// utility function to display stack elements
void display_stack(stack<string> st) {
while(![Link]()) {
cout << [Link]() << ", ";
[Link]();
}
cout << endl;
}
4
DSA Lab Manual
Output & Reason:
4. Access Elements from the Stack
#include <iostream>
#include <stack>
using namespace std;
int main() {
// create a stack of strings
stack<string> colors;
// push element into the stack
[Link]("Red");
[Link]("Pink");
[Link]("Blue");
// get top element
string top = [Link]();
cout << "Top Element: " << top;
return 0;
}
Output & Reason:
5
DSA Lab Manual
5. Get the Size of the Stack
#include <iostream>
#include <stack>
using namespace std;
int main() {
// create a stack of int
stack<int> prime_nums;
// push elements into the stack
prime_nums.push(2);
prime_nums.push(3);
prime_nums.push(5);
// get the size of the stack
int size = prime_nums.size();
cout << "Size of the stack: " << size;
return 0;
}
Output & Reason:
6
DSA Lab Manual
6. Check if the Stack Is Empty
We use the empty () method to check if the stack is empty. This method returns:
1 (true) - if the stack is empty
0 (false) - if the stack is not empty
#include <iostream>
#include <stack>
using namespace std;
int main() {
// create a stack of double
stack<double> nums;
cout << "Is the stack empty? ";
// check if the stack is empty
if ([Link]()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
cout << "Pushing elements..." << endl;
// push element into the stack
[Link](2.3);
[Link](9.7);
cout << "Is the stack empty? ";
// check if the stack is empty
if ([Link]()) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
7
DSA Lab Manual
Output & Reason:
Lab Assignments:
Task: Write a program to design a stack that returns the minimum element
in constant time.
Code, Output, & Reason:
Student Registration No: ________________________
Teacher Signature: ________________________