
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Stack
In this program we will see how to implement stack using C++. A stack is an abstract data structure that contains a collection of elements. Stack implements the LIFO mechanism i.e. the element that is pushed at the end is popped out first. Some of the principle operations in the stack are −
Push - This adds a data value to the top of the stack.
Pop - This removes the data value on top of the stack
Peek - This returns the top data value of the stack
A program that implements a stack using array is given as follows.
Input: Push elements 11, 22, 33, 44, 55, 66 Output: Pop elements 66, 55, 44, 33, 22, 11
Algorithm
push(item)
Begin increase the top pointer by 1 insert item into the location top End
pop()
Begin item = top element from stack reduce top pointer by 1 return item End
peek()
Begin item = top element from stack return item End
Example Code
#include <iostream> using namespace std; int stack[100], n = 100, top = -1; void push(int val) { if(top >= n-1) cout<<"Stack Overflow"<<endl; else { top++; stack[top] = val; } } void pop() { if(top <= -1) cout<<"Stack Underflow"<<endl; else { cout<<"The popped element is "<< stack[top] <<endl; top--; } } void display() { if(top>= 0) { cout<<"Stack elements are:"; for(int i = top; i>= 0; i--) cout<<stack[i]<<" "; cout<<endl; } else cout<<"Stack is empty"; } int main() { int ch, val; cout<<"1) Push in stack"<<endl; cout<<"2) Pop from stack"<<endl; cout<<"3) Display stack"<<endl; cout<<"4) Exit"<<endl; do { cout<<"Enter choice: "<<endl; cin>>ch; switch(ch) { case 1: { cout<<"Enter value to be pushed:"<<endl; cin>>val; push(val); break; } case 2: { pop(); break; } case 3: { display(); break; } case 4: { cout<<"Exit"<<endl; break; } default: { cout<<"Invalid Choice"<<endl; } } }while(ch! = 4); return 0; }
Output
1) Push in stack 2) Pop from stack 3) Display stack 4) Exit Enter choice: 1 Enter value to be pushed: 2 Enter choice: 1 Enter value to be pushed: 6 Enter choice: 1 Enter value to be pushed: 8 Enter choice: 1 Enter value to be pushed: 7 Enter choice: 2 The popped element is 7 Enter choice: 3 Stack elements are:8 6 2 Enter choice: 5 Invalid Choice Enter choice: 4 Exit
- Related Articles
- C++ Program to Implement Stack using array
- C++ Program to Implement Graph Structured Stack
- C++ Program to Implement Stack in STL
- C++ Program to Implement Stack using linked list
- C++ Program to Implement Stack Using Two Queues
- Python Program to Implement a Stack
- C# Program to Implement Stack with Push and Pop operations
- Golang program to implement stack data structure
- Python Program to Implement Stack using One Queue
- Python Program to Implement Stack Using Two Queues
- Python Program to Implement a Stack using Linked List
- Implement Stack using Queues in C++
- Program to construct Frequency Stack in C++
- C++ Program to Implement Vector
- C++ Program to Implement Treap

Advertisements