stack::push() and stack::pop() in C++ STL


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 and (top) an element is removed from that end only.

stack::push()

push() function is used to insert an element at the top of the stack. The element is added to the stack container and the size of the stack is increased by 1.

Syntax :

stackname.push(value)
Parameters :
The value of the element to be inserted is passed as the parameter.
Result :
Adds an element of value same as that of 
the parameter passed at the top of the stack.

Examples:

Input :   mystack
          mystack.push(6);
Output :  6
 
Input :   mystack
          mystack.push(0);
          mystack.push(1);
Output :  0, 1

Errors and Exceptions



1. Shows error if the value passed doesn’t match the stack type.
2. Shows no exception throw guarantee if the parameter doesn’t throw any exception.

// CPP program to illustrate
// Implementation of push() function
#include <iostream>
#include <stack>
using namespace std;

int main()
{
    // Empty stack
    stack<int> mystack;
    mystack.push(0);
    mystack.push(1);
    mystack.push(2);

    // Printing content of stack
    while (!mystack.empty()) {
        cout << ' ' << mystack.top();
        mystack.pop();
    }
}

Output:

2 1 0
Note that output is printed on the basis of LIFO property
stack::pop()

pop() function is used to remove an element from the top of the stack(newest element in the stack). The element is removed to the stack container and the size of the stack is decreased by 1.

Syntax :

stackname.pop()
Parameters :
No parameters are passed.
Result :
Removes the newest element in the stack
or basically the top element.

Examples:

Input :   mystack = 0, 1, 2
          mystack.pop();
Output :  0, 1
 
Input :   mystack = 0, 1, 2, 3, 4, 5
          mystack.pop();
Output :  0, 1, 2, 3, 4

Errors and Exceptions

1. Shows error if a parameter is passed.
2. Shows no exception throw guarantee.

// CPP program to illustrate
// Implementation of pop() function
#include <iostream>
#include <stack>
using namespace std;

int main()
{
    stack<int> mystack;
    mystack.push(1);
    mystack.push(2);
    mystack.push(3);
    mystack.push(4);
    // Stack becomes 1, 2, 3, 4

    mystack.pop();
    mystack.pop();
    // Stack becomes 1, 2

    while (!mystack.empty()) {
        cout << ' ' << mystack.top();
        mystack.pop();
    }
}

Output:

2 1
Note that output is printed on the basis of LIFO property

Application :
Given a number of integers, add them to the stack and find the size of the stack without using size function.

Input : 5, 13, 0, 9, 4
Output: 5

Algorithm
1. Push the given elements to the stack container one by one.
2. Keep popping the elements of stack until it becomes empty, and increment the counter variable.
3. Print the counter variable.

// CPP program to illustrate
// Application of push() and pop() function
#include <iostream>
#include <stack>
using namespace std;

int main()
{
    int c = 0;
    // Empty stack
    stack<int> mystack;
    mystack.push(5);
    mystack.push(13);
    mystack.push(0);
    mystack.push(9);
    mystack.push(4);
    // stack becomes 5, 13, 0, 9, 4

    // Counting number of elements in queue
    while (!mystack.empty()) {
        mystack.pop();
        c++;
    }
    cout << c;
}

Output:

5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Practice Tags :

Recommended Posts:



2.5 Average Difficulty : 2.5/5.0
Based on 2 vote(s)






User Actions