Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <assert.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define STACKSIZE 20
- using namespace std;
- class Stack{
- int totalSize;
- int currentSize;
- int * array; //pointer whitch will point to our stack memory
- public:
- //stack constructor
- Stack(int size){
- this->totalSize = size;
- this->currentSize = 0;
- this->array = new int[this->totalSize]; //create the dynamic array
- }
- bool isEmpty(){
- return this->currentSize == false;
- }
- bool isFull(){
- return this->currentSize == this->totalSize;
- }
- void push(int element){
- if(isFull()){
- //s->data = realloc(s->data, s->size*2*sizeof(int));
- /*
- if (!s->data)
- {
- free(s->data);
- perror("realloc");
- }
- s->size *= 2;*/
- cout << "OVERFLOWED" << endl;
- } else{
- this->array[this->currentSize] = element;
- //the stack will increase the urrentr size
- this->currentSize++;
- }
- }
- int pop(){
- if(isEmpty()){
- cout << "Stack is empty" << endl;
- abort();
- }
- else{
- this->currentSize--;
- return this->array[this->currentSize];
- }
- }
- };
- int main(int argc, char const *argv[])
- {
- Stack * newStack = new Stack(STACKSIZE);
- newStack->push(5);
- newStack->push(3);
- newStack->push(15);
- newStack->push(35);
- newStack->push(45);
- cout<< newStack->pop() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement