-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0401queue.cpp
More file actions
114 lines (108 loc) · 2.93 KB
/
0401queue.cpp
File metadata and controls
114 lines (108 loc) · 2.93 KB
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
#include <iostream>
#include <string>
using namespace std;
template <class T>
class Queue {
private:
T *queue; //queue 원소를 위한 배열
int front, //첫번째 원소로부터 반시계방향으로 한 위치 뒤
rear, //마지막 원소의 위치
capacity; //큐의 배열
public:
Queue(int queueCapacity = 5);
bool IsEmpty();
bool IsFull();
T& Front();
T& Rear();
void Push(const T& item);
void Pop();
void Print();
};
template <class T>
Queue<T>::Queue(int queueCapacity) : capacity(queueCapacity) {
if (capacity < 1) throw "Queue capacity must be > 0";
queue = new T[capacity];
front = rear = 0;
}
//size 멤버변수 만들어서
template <class T>
void Queue<T>::Pop() {
if(IsEmpty()) throw "Queue is empty. Cannot delete.";
front = (front+1)%capacity;
queue[front].~T();
}
template <class T>
inline bool Queue<T>::IsEmpty() { return front == rear; }
template <class T>
inline bool Queue<T>::IsFull() { return front == (rear + 1) % capacity; }
template <class T>
inline T& Queue<T>::Front() {
if(IsEmpty()) throw "Queue is empty. No front element";
return queue[(front + 1) % capacity];
}
template <class T>
inline T& Queue<T>::Rear() {
if(IsEmpty()) throw "Queue is empty. No front element";
return queue[rear];
}
template <class T>
void Queue<T>::Push(const T& x) {
if (IsFull()) {
T *newQueue = new T[capacity*2];
int start = (front + 1) % capacity;
int j = 0;
for (int i =start; i != (rear + 1) % capacity; i = (i + 1) % capacity) {
newQueue[j] = queue[i];
j++;
}
//멤버변수 변화시켜주기
front = 2 * capacity - 1; //front 위치 조정
rear = capacity - 2; //rear 위치 조정
capacity *= 2; //capacity 크기 2배 조정
delete[] queue; //기존 queue 삭제
queue = newQueue; //2배 변경된 queue를 queue에 연결
}
rear = (rear + 1) % capacity; queue[rear] = x;
}
template <class T>
void Queue<T>::Print() {
cout << "capacity=" << capacity << " " << "front=" << front <<" "<<"rear=" << rear << " " << endl;
for (int i = (front + 1) % capacity; i != (rear + 1) % capacity; i = (i + 1) % capacity) //중요!
cout << " " << "queue[" << i << "]=" << queue[i] << endl;
cout << endl;
}
/*
int main() {
try {
Queue<int> Q_queue(5);
Q_queue.Push(5); Q_queue.Push(40);
Q_queue.Print();
Q_queue.Push(3);
Q_queue.Pop(); Q_queue.Pop();
Q_queue.Push(43);
Q_queue.Push(49);
Q_queue.Push(490);
Q_queue.Print();
}
catch (char* str) { cout << str << endl; }
}
*/
int main() {
try {
Queue<int> Q_queue(4);
Q_queue.Push(5); Q_queue.Push(40);
Q_queue.Push(3); //Q_queue.Pop(); Q_queue.Pop();
Q_queue.Push(43); Q_queue.Push(49);
Q_queue.Push(490); Q_queue.Print();
Q_queue.Push(55); Q_queue.Push(45);
Q_queue.Pop(); Q_queue.Pop();
Q_queue.Pop(); Q_queue.Pop();
Q_queue.Push(55); Q_queue.Push(45);
Q_queue.Print();
Queue<string> aqueue(10);
aqueue.Push("sky"); aqueue.Push("sky1");
aqueue.Push("sky2");
aqueue.Print();
}
catch (char* str) { cout << str << endl; }
}