Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include "Stack.h"
- #define STACKSIZE 3
- #define INIT -1
- using namespace std;
- Stack::Stack()
- {
- totalSize = STACKSIZE;
- top = INIT;
- array = (int*)malloc(sizeof(int)*totalSize);
- if(array == NULL)
- {
- printf("Memory not allocated\n");
- abort();
- }
- }
- void Stack::push(int element)
- {
- if(top == totalSize - 1)
- {
- totalSize *= 2;
- int *temp = (int*)realloc(array, sizeof(int)*totalSize);
- if(temp == NULL)
- {
- cout<<"Realloc failed"<<endl;
- abort();
- } else {
- array = temp;
- }
- }
- array[++top] = element;
- }
- int Stack::pop()
- {
- if(isEmpty())
- {
- cout<<"Stack is empty"<<endl;
- abort();
- }
- return array[top--];
- }
- bool Stack::isEmpty()
- {
- if(top == -1)
- {
- return true;
- } else {
- return false;
- }
- }
- void Stack::getTotatSize()
- {
- printf("Size of stack is: %d\n", totalSize);
- }
- Stack::~Stack()
- {
- free(array);
- }
- #ifndef STACK_H
- #define STACK_H
- class Stack{
- int totalSize;
- int top;
- int * array;
- public:
- void push(int elemennt);
- int pop();
- bool isEmpty();
- void getTotatSize();
- Stack();
- ~Stack();
- };
- #endif
- #include <iostream>
- #include <string>
- #include <string.h>
- #include "Stack.h"
- #include "Stack.cpp"
- #define STACKSIZE 3
- using namespace std;
- int main(int argc, char const *argv[])
- {
- Stack newStack;
- newStack.push(5);
- newStack.push(3);
- newStack.push(15);
- newStack.getTotatSize();
- newStack.push(35);
- newStack.push(45);
- newStack.getTotatSize();
- cout << newStack.pop() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement