1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <iostream> #include <string> using namespace std; class BaseArray { private: int capacity; int *mem; protected: BaseArray(int capacity = 100) { this->capacity = capacity; mem = new int[capacity]; } ~BaseArray() { delete[]mem; } void put(int index, int val) { mem[index] = val; } int get(int index) { return mem[index]; } int getCapacity() { return capacity; } }; class MyStack :public BaseArray { int index; public: MyStack(int capacity) :BaseArray(capacity) { index = 0; } void push(int n) { put(index, n); index++; } int pop() { index--; return get(index); } int capacity() { return getCapacity(); } int length() { return index; } }; int main() { MyStack mStack(100); int n; cout << "스택에 삽입할 5개의 정수를 입력하라>> "; for (int i = 0; i < 5; i++) { cin >> n; mStack.push(n); } cout << "스택의 용량:" << mStack.capacity() << ", 스택의 크기:" << mStack.length() << '\n'; cout << "스택의 원소를 순서대로 제거하여 출력한다>> "; while (mStack.length() != 0) { cout << mStack.pop() << ' '; } cout << '\n' << "스택의 현재 크기 : " << mStack.length() << '\n'; } | cs |
'Programming > 명품 C++ Programming' 카테고리의 다른 글
| 명품 C++ Programming 실습문제 8장 8번 (0) | 2018.06.21 |
|---|---|
| 명품 C++ Programming 실습문제 8장 7번 (0) | 2018.06.21 |
| 명품 C++ Programming 실습문제 8장 5번 (0) | 2018.06.19 |
| 명품 C++ Programming 실습문제 8장 4번 (0) | 2018.06.19 |
| 명품 C++ Programming 실습문제 8장 3번 (0) | 2018.06.19 |