DATA STRUCTURE AND ALGORITHM (Stack)
STACK
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only.
![]() |
| Stack |
Stack Syntax:-
| template <class Type, class Container = deque<Type> > class stack; |
Type – is the Type of element contained in the std::stack. It can be any valid C++ type or even a user-defined type.
Container – is the Type of underlying container object.
Member Types:-
- value_type- The first template parameter, T. It denotes the element types.
- container_type- The second template parameter, Container. It denotes the underlying container type.
- size_type- Unsigned integral type.
The functions associated with stack are:
- empty() – Returns whether the stack is empty – Time Complexity : O(1)
- size() – Returns the size of the stack – Time Complexity : O(1)
- top() – Returns a reference to the top most element of the stack – Time Complexity : O(1)
- push(g) – Adds the element ‘g’ at the top of the stack – Time Complexity : O(1)
- pop() – Deletes the top most element of the stack – Time Complexity : O(1)
A simple CPP program to introduce a stack:
#include <iostream>
#include <stack>
using namespace std;
#include <stack>
using namespace std;
int main()
{
stack<int> stack;
stack.push(6);
stack.push(24);
stack.push(4);
stack.push(5);
stack.pop(); // deletes 5
stack.pop(); // deletes 4
stack<int> stack;
stack.push(6);
stack.push(24);
stack.push(4);
stack.push(5);
stack.pop(); // deletes 5
stack.pop(); // deletes 4
while (!stack.empty()) {
cout << ' ' << stack.top();
stack.pop();
}
return 0;
}
}


Comments
Post a Comment