반응형
main.cpp와 classDef.h를 만들어서 돌려보면 push와 pop을 할 수 있고 print가 가능하다.
Template를 이용하고 싶다면,
int value;의 int와 stact<int> S;의 int를 둘다 char로 바꾸던지, double로 바꾸는 식으로 하면 Template를 경험할 수 있다
- main.cpp -
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 | #include "classDef.h" int main() { int num; int value; stack<int> S; // 스택 생성 while (1) { cout << "번호를 입력하여 주십시오.\n"; cout << "1. Push" << "\n" << "2. Pop" << "\n" << "3. print" << "\n" << "4. exit" << endl; cin >> num; switch (num) { case 1: cout << "Push할 값을 입력하세요 : "; cin >> value; S.push(value); break; case 2: S.pop(); break; case 3: S.print(); break; case 4: return 0; } } return 0; } | Crocus |
- classDef.cpp -
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #include <iostream> using namespace std; template <typename T> class stack { private: T *arr; public: int max_size = 5; stack(); ~stack(); void push(int value); T pop(); void print(); }; int front = 0, rear = 0; // 생성자 template <typename T> stack<T>::stack() { arr = new T[max_size]; // 동적 할당 for (int j = 0; j < max_size; j++) { arr[j] = -1; // -1로 초기화 } } // 소멸자 template <typename T> stack<T>::~stack() { delete[] arr; } // Push template <typename T> void stack<T>::push(int value) { if (rear == max_size) // 꽉차면 { cout << "크기가 꽉 차서 2배로 늘립니다.\n" << endl; T *tmp = new T[max_size]; int t = max_size; // 이전 max_size보관 for (int j = 0; j < max_size; j++) { tmp[j] = arr[j]; // store } max_size = max_size * 2; // 2배 arr = new T[max_size]; // 할당 for (int j = 0; j < max_size; j++) { arr[j] = -1; // -1로 초기화 } for (int j = 0; j < t; j++) { arr[j] = tmp[j]; // restore } delete[]tmp; } arr[rear] = value; // 값 넣고 rear++; // rear + 1 }; // Pop template <typename T> T stack<T>::pop() { if (arr[front] == -1 || front + 1 > max_size) { cout << "스택에 값이 없습니다.\n"; return 0; // 실패 } cout << "Pop : " << arr[front] << endl; arr[front] = -1; front++; if (max_size >= 10 && max_size / 2 == (rear - front) + 1) { cout << "크기를 반으로 줄이겠습니다.\n"; int t = max_size; T *tmp = new T[max_size]; for (int j = 0; j < max_size; j++) { tmp[j] = arr[j]; // store } max_size = max_size / 2; // 1/2배 for (int j = 0; j < max_size; j++) { arr[j] = -1; // 초기화 } int temp = 0; // arr배열 0부터 시작해야되니 for (int j = 0; j < t; j++) // 원래 크기에서 판단 { if (tmp[j] == -1) { continue; } else { arr[temp] = tmp[j]; temp++; } } front = 0; rear = max_size - 1; delete[]tmp; } return 1; // 성공 }; // Print Stack template <typename T> void stack<T>::print() { for (int j = 0; j < max_size; j++) { cout << arr[j] << " "; } cout << endl; }; | Crocus |
반응형
'Applied > 자료구조' 카테고리의 다른 글
이중 연결 리스트 기반 데크(Dequeue) 소스 코드 (0) | 2016.04.13 |
---|---|
이중 연결 리스트 기반 데크(Dequeue) 개념 (0) | 2016.04.13 |
이진 탐색(Binary Search) 코드 구현 (0) | 2016.03.12 |
동적 배열 리스트 명단 만들기 (0) | 2016.03.12 |
Dummy 노드를 이용한 연결 리스트(Dummy Linked List) 소스코드 (0) | 2016.02.28 |