![]() |
VOOZH | about |
The stack::push() and stack::pop() method in stack container is used to insert and delete the element from the top of stack. They are the member functions of std::stack container defined inside <stack> header file. In this article, we will learn how to use stack::push() and stack::pop() methods in C++.
The stack::push() function is used to insert or 'push' an element at the top of stack container. This increases the size of stack container by 1.
st.push(val);
where, st is the name of the stack
Parameters
Return Value
Managing data with stacks is essential for various applications.
2 1 0
Time Complexity: O(1)
Auxiliary Space: O(1)
The stack::pop() function to remove or 'pop' the element from the top of stack. As we are only inserting and removing from the top, the most recently inserted element will be removed first. This decreases the size of stack container by 1.
st.pop()
Parameters
Return value
2 1
Time Complexity: O(1)
Auxiliary Space: O(1)