Created
July 7, 2018 16:35
-
-
Save miladabc/f49b79e3d957f55094b3ffb24fab60b1 to your computer and use it in GitHub Desktop.
Stack implementation with 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
//Milad Abbasi | |
//06-07-2018 | |
//Stack implementation with Array | |
#include <iostream> | |
using namespace std; | |
#define MAX_SIZE 1000 | |
class Stack | |
{ | |
int top; | |
public: | |
int a[MAX_SIZE]; | |
Stack() { top = -1; } | |
bool push(int x); | |
int pop(); | |
bool isEmpty(); | |
}; | |
bool Stack::push(int x) | |
{ | |
if (top >= MAX_SIZE) | |
{ | |
cout << "Stack Overflow"; | |
return false; | |
} | |
else | |
{ | |
a[++top] = x; | |
return true; | |
} | |
} | |
int Stack::pop() | |
{ | |
if (top < 0) | |
{ | |
cout << "Stack Underflow"; | |
return 0; | |
} | |
else | |
{ | |
int x = a[top--]; | |
return x; | |
} | |
} | |
bool Stack::isEmpty() | |
{ | |
return (top < 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment