Skip to content

Instantly share code, notes, and snippets.

@Parassharmaa
Created October 5, 2016 02:50
Show Gist options
  • Save Parassharmaa/2686507692e9050aa78e77aab5bcd6e5 to your computer and use it in GitHub Desktop.
Save Parassharmaa/2686507692e9050aa78e77aab5bcd6e5 to your computer and use it in GitHub Desktop.
Stack implementation via dynamic array
#include <iostream>
#include <cstring>
using namespace std;
class stack {
int top;
int size;
int *arr;
public:
stack(int s) {
top = -1;
size = s;
arr = new int[size];
}
void push(int i);
int pop();
};
void stack::push(int i) {
if(top==size-1) {
cout<<"Overflow\n";
}
else {
top++;
arr[top] = i;
}
}
int stack::pop() {
if(top==-1) {
cout<<"Underflow\n";
return 0;
}
else {
int a = top;
top--;
return arr[a];
}
}
int main() {
stack s(3000);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment