Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct iStack
- {
- int capacity;
- int top;
- int* data;
- void init(int capacity);
- void destroy();
- void push(int value);
- bool pop(int*);
- };
- bool iStack::pop(int* value)
- {
- if (this->data != nullptr && this->top >= 0)
- {
- *value = this->data[this->top--];
- return true;
- }
- cout << "Something went wrong!" << endl;
- return false;
- }
- void iStack::push(int value)
- {
- if (this->data != nullptr && this->top < this->capacity - 1)
- {
- this->data[++(this->top)] = value;
- return;
- }
- cout << "Error pushin value!";
- return;
- }
- void iStack::init(int capacity)
- {
- this->capacity = capacity;
- this->top = -1;
- this->data = new int[capacity];
- }
- void iStack::destroy()
- {
- delete[] this->data;
- this->data = nullptr;
- this->capacity = 0;
- this->top = -1;
- }
- int main()
- {
- iStack stack;
- stack.init(3);
- stack.push(20);
- stack.push(30);
- stack.push(40);
- int value;
- stack.pop(&value);
- cout << value << endl;
- stack.pop(&value);
- cout << value << endl;
- stack.pop(&value);
- cout << value << endl;
- stack.destroy();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement