- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Data Structures Stack Primitive Operations
Stack is Last In First Out data structure. The stack is used in different area for evaluating expressions, call and recursion strategy etc. The stack has some primitive operations. Here we will see those operations of stack, and see one example using the stack ADT.
The ADT (abstract datatype) is special kind of datatype, whose behavior is defined by a set of values and set of operations. The keyword “Abstract” is used as we can use these datatypes, we can perform different operations. But how those operations are working that is totally hidden from the user. The ADT is made of with primitive datatypes, but operation logics are hidden.
These are few operations or functions of the Stack ADT.
- isFull(), This is used to check whether stack is full or not
- isEmpry(), This is used to check whether stack is empty or not
- push(x), This is used to push x into the stack
- pop(), This is used to delete one element from top of the stack
- peek(), This is used to get the top most element of the stack
- size(), this function is used to get number of elements present into the stack
Example
#include<iostream> #include<stack> using namespace std; main(){ stack<int> stk; if(stk.empty()){ cout << "Stack is empty" << endl; } else { cout << "Stack is not empty" << endl; } //insert elements into stack stk.push(10); stk.push(20); stk.push(30); stk.push(40); stk.push(50); cout << "Size of the stack: " << stk.size() << endl; //pop and dispay elements while(!stk.empty()) { int item = stk.top(); // same as peek operation stk.pop(); cout << item << " "; } }
Output
Stack is empty Size of the stack: 5 50 40 30 20 10
- Related Articles
- Stack ADT in Data Structures
- Operations on Queue in Data Structures
- Difference between Stack and Queue Data Structures
- Operations on an Array in Data Structures
- What are the specifications and operations of data structures in compiler design?
- Java primitive data types
- Kernel Data Structures
- Kinetic Data Structures
- What are Primitive and Non-Primitive Data Types in JavaScript?
- Abstract Data Type in Data Structures
- Data objects and Structures
- Correspondence Based Data Structures
- Inbuilt Data Structures in C#
- Inbuilt Data Structures in Python
- Tail Recursion in Data Structures

Advertisements