Stack is a container adapter that provides a LIFO (Last-In, First-Out) data structure, where elements are added and removed from the top of the stack. It is part of the Standard Template Library (STL) in C++ and provides various member functions to manipulate the stack.
To use the stack container adapter, you need to include the
#include <iostream> #include <stack>
Now, let's dive into the various operations you can perform with the stack:
To create a stack, you need to declare an instance of the std::stack template class. Here's an example:
std::stack<int> myStack; This creates an empty stack of integers (int).
To insert elements into the stack, you can use the push() function. It adds an element to the top of the stack. Here's an example:
myStack.push(10); myStack.push(20); myStack.push(30);
After executing these statements, the stack will contain the elements 10, 20, and 30, with 30 being the topmost element.
To remove an element from the top of the stack, you can use the pop() function. It doesn't return any value. Here's an example:
myStack.pop();
After executing this statement, the topmost element (30) will be removed from the stack.
>To access the element at the top of the stack without removing it, you can use the top() function. Here's an example:
int topElement = myStack.top(); std::cout << "Top element: " << topElement << std::endl;
This will print the topmost element of the stack.
You can use the empty() function to check if the stack is empty. It returns a boolean value indicating whether the stack is empty or not. Here's an example:
if (myStack.empty()) { std::cout << "Stack is empty" << std::endl; } else { std::cout << "Stack is not empty" << std::endl; }
To determine the number of elements in the stack, you can use the size() function. It returns the number of elements present in the stack. Here's an example:
std::cout << "Stack size: " << myStack.size() << std::endl;
This will print the size of the stack.
The stack doesn't provide iterators like other containers in the STL. To traverse the elements of the stack, you'll typically need to use the top-down approach by repeatedly popping elements until the stack becomes empty. Here's an example:
while (!myStack.empty()) { int element = myStack.top(); std::cout << element << " "; myStack.pop(); } std::cout << std::endl;
This code will print and remove all the elements from the stack.
#include<iostream> #include <stack> int main() { std::stack<int> myStack; // Pushing elements onto the stack myStack.push(10); myStack.push(20); myStack.push(30); myStack.push(40); myStack.push(50); // Checking if the stack is empty if (myStack.empty()) { std::cout << "Stack is empty." << std::endl; } else { std::cout << "Stack is not empty." << std::endl; } // Accessing the top element of the stack std::cout << "Top element: " << myStack.top() << std::endl; // Popping elements from the stack std::cout << "Popping elements: "; while (!myStack.empty()) { std::cout << myStack.top() << " "; myStack.pop(); } std::cout << std::endl; // Checking the stack after popping elements if (myStack.empty()) { std::cout << "Stack is empty." << std::endl; } else { std::cout << "Stack is not empty." << std::endl; } return 0; }