Skip to content

Instantly share code, notes, and snippets.

@achlendra
Created October 27, 2016 17:20
Show Gist options
  • Save achlendra/50a218a600160cc72b3eaab503efe9ef to your computer and use it in GitHub Desktop.
Save achlendra/50a218a600160cc72b3eaab503efe9ef to your computer and use it in GitHub Desktop.
stack implemented via array
#include <iostream>
#define msize 101
using namespace std;
class Stack{
private:
int A[msize];
int top;
public:
Stack(){
top=-1;
}
void push(int x){
if(top==msize-1){
cout<<"\nstack overflow";
return;
}
A[++top]=x;
}
void pop(){
if(top==-1){
cout<<"\n empty stack";
return;
}
top--;
}
int Top(){
return A[top];
}
int isEmpty(){
if(top==-1) return 1;
return 0;
}
void print(){
int i;
cout<<"\nstack ";
for (i=0;i<top;i++){
cout<<A[i]<<", ";
}
cout<<"\n";
}
};
int main() {
Stack s;
int n,in,topop;
cout<<"\nHow many elements: ";
cin>>n;
for(int i=0;i<n;i++){
cin>>in;
s.push(in);
}
s.print();
cout<<"\n how many elements to pop";
cin>>topop;
for(int i=0;i<topop;i++){
s.pop();
}
s.print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment