Stack implementation in c++ code using array | Data Structure
This program is the demonstration for implementing Simple Stack Program using array and Object Oriented Concept with basic functions of Stack push() and pop() with index called top which gives the current status of Stack! Stack has defined size so need two more function to check whether isEmpty() or isFull() to check stack is empty or not or is full or not. In this scenario top is integer type as default to top=-1,if top=-1 Mean Stack is Empty , if top=sizeOfStack-1 then stack is full.Solution Coding
#include<iostream> using namespace std; class stack { private: int top; int size; int st[]; public: stack(int s) { top=-1; size=s; int st[size]; } bool isEmpty() { if(top==-1) { return 1; } else return 0; } bool isFull() { if(top==size-1) return 1; else return 0; } void push(int v) { if(isFull()) { cout<<"Stack is Full...!!"; } else { top++; st[top]=v; } } void pop() { if(isEmpty()) { cout<<"Stack is Empty...!!"; } else { cout<<"\n"<<st[top]<<" is pop successfully..!!"<<endl; top--; } } }; int main() { int n; cout<<"Enter Size of Stack:-"<<endl; cin>>n; stack s[n]; while(1) { cout<<"\n-1 Push \n-2 Pop"<<endl; int vb; cin>>vb; if(vb==3) break; switch(vb) { case 1:int x;cout<<"Enter no in stack:"<<endl;cin>>x;s.push(x);break; case 2:s.pop();break; default:cout<<"Invalid Choice...!"<<endl; } } return 0; }
0 comments:
Post a Comment