Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stack.h
- #include <iostream>
- #include <cstdlib>
- class Stack {
- private:
- int* _stack;
- int _size;
- public:
- Stack() {
- this->_size = 0;
- this->_stack = (int *)malloc(0);
- }
- ~Stack() { this->empty(); }
- void push(int item);
- int peek();
- int pop();
- int getSize();
- void empty();
- private:
- void _extend();
- void _shrink();
- void _allocate();
- };
- ------------------------
- stack.cpp
- #include "Stack.h"
- void Stack::push(int item) {
- this->_extend();
- this->_stack[this->_size-1] = item;
- }
- int Stack::peek() {
- return this->_stack[this->_size-1];
- }
- int Stack::pop() {
- int item = this->_stack[this->_size-1];
- this->_shrink();
- return item;
- }
- int Stack::getSize() {
- return this->_size;
- }
- void Stack::empty() {
- while (this->_size) this->_shrink();
- }
- void Stack::_extend() {
- this->_size++;
- this->_allocate();
- }
- void Stack::_shrink() {
- this->_size--;
- this->_allocate();
- }
- void Stack::_allocate() {
- this->_stack = (int *)realloc(this->_stack,
- this->_size * sizeof(int));
- }
- ---------
- main.cpp
- #include "Stack.h"
- #include <cstdio>
- using namespace std;
- int main(void) {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- int size, x, y, tmp;
- cin >> size >> x >> y;
- Stack* s = new Stack();
- for (int i = 0; i < size; i++) {
- cin >> tmp;
- s->push(tmp);
- }
- Stack *result = new Stack();
- while(s->getSize()) {
- int item = s->pop();
- if (item == x) {
- result->push(y);
- }
- result->push(item);
- }
- while(result->getSize()) {
- cout << result->pop() << " ";
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment