Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created August 25, 2019 09:40
Show Gist options
  • Save surinoel/e01588d11d92f7e259b1c9e9c3fdab5b to your computer and use it in GitHub Desktop.
Save surinoel/e01588d11d92f7e259b1c9e9c3fdab5b to your computer and use it in GitHub Desktop.
#include "stack.h"
using namespace std;
int main()
{
Stack s1(10);
Stack s2(100);
s1.push(100);
s1.push(200);
s1.push(300);
cout << "s1 1st pop(): " << s1.pop() << '\n';
cout << "s1 2nd pop(): " << s1.pop() << '\n';
cout << "s1 3rd pop(): " << s1.pop() << '\n';
return 0;
}
#include "stack.h"
Stack::Stack(int size)
{
pArr = new int[size];
assert(pArr);
this->tos = 0;
this->size = size;
}
Stack::~Stack()
{
delete [] this->pArr;
}
void Stack::push(int data)
{
assert(this->tos != this->size);
pArr[tos++] = data;
}
int Stack::pop()
{
assert(this->tos != 0);
return pArr[--tos];
}
#ifndef STACK_H
#define STACK_H
#include <iostream>
#include <cassert>
class Stack {
private:
int *pArr;
int tos;
int size;
public:
Stack(int size = 0);
~Stack();
void push(int data);
int pop();
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment