Created
October 5, 2016 02:50
-
-
Save Parassharmaa/2686507692e9050aa78e77aab5bcd6e5 to your computer and use it in GitHub Desktop.
Stack implementation via dynamic array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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