Skip to content

Instantly share code, notes, and snippets.

@miladabc
Created July 7, 2018 16:35
Show Gist options
  • Save miladabc/f49b79e3d957f55094b3ffb24fab60b1 to your computer and use it in GitHub Desktop.
Save miladabc/f49b79e3d957f55094b3ffb24fab60b1 to your computer and use it in GitHub Desktop.
Stack implementation with Array
//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