Skip to content

Instantly share code, notes, and snippets.

@manonthemat
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manonthemat/49c6a2dadd57b0ae75b2 to your computer and use it in GitHub Desktop.
Save manonthemat/49c6a2dadd57b0ae75b2 to your computer and use it in GitHub Desktop.
simple stack data structure in C++
#include <iostream>
using namespace std;
class Stack_overflow {};
class Stack_underflow {};
class Stack {
public:
Stack(int sz) : sz{sz}, top{-1}, elem {new int[sz]} {};
~Stack() { delete[] elem; };
bool empty();
void push(int* x);
int* pop();
private:
int* elem;
int top;
int sz;
};
bool Stack::empty() {
if (top < 0) return true;
return false;
}
void Stack::push(int* x) {
if(top+1 > sz) throw Stack_overflow{};
++top;
elem[top] = *x;
}
int* Stack::pop() {
if (empty()) throw Stack_underflow{};
--top;
return &elem[top+1];
}
int main() {
Stack s{2};
int a = 5;
int b = 7;
s.push(&a);
s.push(&b);
int* y = s.pop();
cout << *y << endl;
y = s.pop();
cout << *y << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment