Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <string.h>
- #define STACKSIZE 3
- using namespace std;
- class Stack{
- int totalSize;
- int top;
- int * array;
- public:
- void push(int a);
- int pop();
- bool isEmpty();
- bool isFull();
- int GetTotatSize();
- Stack(int size);
- Stack();
- ~Stack();
- };
- Stack::Stack(int size){
- this->totalSize = size;
- this->top = 0;
- this->array = new int[this->totalSize]; //create the dynamic array
- }
- Stack::Stack(){
- this->totalSize = 0;
- this->top = 0;
- }
- Stack::~Stack(){
- delete[] this->array;
- }
- int Stack::GetTotatSize(){
- return this->totalSize;
- }
- bool Stack:: isEmpty(){
- return this->top == -1;
- }
- bool Stack:: isFull(){
- return this->top == this->totalSize;
- }
- void Stack::push(int element){
- if(isFull()){
- int * new_arr = new int[this->totalSize * 2];
- memcpy( new_arr, this->array, this->totalSize * sizeof(int) );
- this->totalSize *= 2;
- delete[] this->array;
- this->array = new_arr;
- } 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(STACKSIZE);
- newStack.push(5);
- newStack.push(3);
- newStack.push(15);
- cout << "Size of the stack: " << newStack.GetTotatSize() << endl;
- newStack.push(35);
- newStack.push(45);
- cout << "Size of the stack: " << newStack.GetTotatSize() << endl;
- cout << newStack.pop() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement