Created
August 25, 2019 09:40
-
-
Save surinoel/e01588d11d92f7e259b1c9e9c3fdab5b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 "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; | |
} |
This file contains hidden or 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 "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]; | |
} |
This file contains hidden or 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
#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