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 MyQueue :public BaseArray { int index; public: MyQueue(int capacity) :BaseArray(capacity) { index = 0; } void enqueue(int n) { put(index, n); index++; } int dequeue() { index--; return get(index); } int capacity() { return getCapacity(); } int length() { return index; } }; int main() { MyQueue mQ(100); int n; cout << "큐에 삽입할 5개의 정수를 입력하라>> "; for (int i = 0; i < 5; i++) { cin >> n; mQ.enqueue(n); } cout << "큐의 용량:" << mQ.capacity() << ", 큐의 크기:" << mQ.length() << '\n'; cout << "큐의 원소를 순서대로 제거하여 출력한다>> "; while (mQ.length() != 0) { cout << mQ.dequeue() << ' '; } cout << '\n' << "큐의 현재 크기 : " << mQ.length() << '\n'; } | cs |
'Programming > 명품 C++ Programming' 카테고리의 다른 글
명품 C++ Programming 실습문제 8장 7번 (0) | 2018.06.21 |
---|---|
명품 C++ Programming 실습문제 8장 6번 (0) | 2018.06.19 |
명품 C++ Programming 실습문제 8장 4번 (0) | 2018.06.19 |
명품 C++ Programming 실습문제 8장 3번 (0) | 2018.06.19 |
명품 C++ Programming 실습문제 8장 2번 (0) | 2018.06.19 |