Stack.h
#pragma once #include<iostream> using namespace std; class Stack { public: int* elements; int top; int maxSize; Stack(int size = 5) { maxSize = size; elements = new int[maxSize]; top = -1; } bool isFull() { return top == maxSize - 1; } void stackFull() { int *arr; arr = new int[maxSize * 2]; for (int i = 0; i < maxSize; i++) { arr[i] = elements[i]; } elements = arr; maxSize *= 2; } void push(int e) { if (isFull() == true) { stackFull(); } elements[++top] = e; } bool pop(int& e) { bool res=true; if (top == -1) { res = false; } else { e = elements[top--]; } return res; } };
main.cpp
#include"Stack.h" int main() { Stack s; int arr[10] = { 1,2,3,4,5,6,7,8,9 }; for (int i = 0; i < 9; i++) { s.push(arr[i]); } int temp; while (s.pop(temp)) { cout << temp << " "; } return 0; }