Stack

C++ Programming

C++ Programming Examples

#include<iostream>
#include<stack>
using namespace std;

int main() {

	/* Create a stack */
	stack<int> data;

	/* Push data into the stack */
	data.push(10);
	data.push(20);
	data.push(30);
	data.push(40);
	
	cout << "Stack size: " << data.size() << endl;
	cout << "Data on top of the stack: " << data.top() << endl;

	/* Pop data from the stack */
	data.pop();

	cout << "Stack size after pop: " << data.size() << endl;
	cout << "Now top data on stack: " << data.top() << endl;
	
	return 0;
}


          /****** Output ******/
          

Stack size: 4


Data on top of the stack: 40


Stack size after pop: 3


Now top data on stack: 30

Learn more about Stack Data Structure