Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #define STACKSIZE 20
- using namespace std;
- class Stack{
- int totalSize;
- int top;
- int * array;
- public:
- void push(int a);
- int pop();
- Stack(int size);
- ~Stack();
- bool isEmpty();
- bool isFull();
- int getSize();
- };
- Stack::Stack(int size){
- this->totalSize = size;
- this->top = 0;
- this->array = new int[this->totalSize]; //create the dynamic array
- }
- Stack::~Stack(){
- free(this-> array);
- }
- bool Stack:: isEmpty(){
- return this->top == false;
- }
- bool Stack:: isFull(){
- return this->top == this->totalSize -1;//-1
- }
- void Stack::push(int element){
- if(isFull()){
- this->array = (int*)malloc(sizeof(int)* this->totalSize * 2);
- this->totalSize *= 2;
- } else{
- this->array[this->top] = element;
- this->top++;
- }
- }
- int Stack:: pop(){
- if(isEmpty()){
- cout << "Stack is empty" << endl;
- abort();
- }
- else{
- this->top--;
- return this->array[this->top];
- }
- }
- int main(int argc, char const *argv[])
- {
- Stack * newStack = new Stack(STACKSIZE);
- Stack newStack
- newStack->push(5);
- newStack->push(3);
- newStack->push(15);
- // cout <<"Size of the stack: "; this->totalSize << endl;// chce tu sprawdzic czy sie tablica powiekszyłaXD
- newStack->push(35);
- newStack->push(45);
- // cout <<"Size of the stack: ", newStack->totalSize << endl;
- cout<< newStack->pop() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement