728x90
๋ฐ์ํ
๐ฅ ๋ค์ด๊ฐ๊ธฐ ์ ์
- 8์ฅ์ ์์์ ๋ํ ๋ด์ฉ , ์ฌ๋งํ๋ฉด public์ผ๋ก ํ์ํด๋์ค์ ์์ํด์ฃผ์!
- MyQueue::MyQueue(int capacity):BaseArray(capacity)
-> ์ด๋ฐ์์ ์์ฑ์ ์์(?) ์ ์จ์ฃผ๊ธฐ ์ ์ฉํ๋ค!
5๋ฒ
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
|
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
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 head, tail;
int size;
public:
MyQueue(int capacity);
void enqueue(int num);
int capacity();
int dequeue();
int length();
};
MyQueue::MyQueue(int capacity):BaseArray(capacity) {
head = 0;
tail = -1;
size = 0;
}
void MyQueue::enqueue(int num) {
if (size == capacity()) {
return;
}
put(head, num);
head++;
head = head % capacity();
size++;
}
int MyQueue::dequeue() {
if (size == 0) {
return -1;
}
tail++;
size--;
tail = tail % capacity();
return get(tail);
}
int MyQueue::capacity() {
return getCapacity();
}
int MyQueue::length() {
return size;
}
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() << endl;
cout << "ํ์ ์์๋ฅผ ์์๋๋ก ์ ๊ฑฐํ์ฌ ์ถ๋ ฅํ๋ค>>";
while (mq.length() != 0) {
cout << mq.dequeue() << " ";
}
cout << endl << "ํ์ ํ์ฌ ํฌ๊ธฐ: " << mq.length() << endl;
}
|
cs |
6๋ฒ
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
|
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
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 tos;//top of stack
public:
MyStack(int capacity);
void push(int num);
int capacity();
int length();
int pop();
};
MyStack::MyStack(int capacity) :BaseArray(capacity) {
tos = 0; // ๋ค์ ํธ์ฌํ ์์น, ํฌ๊ธฐ๋ก ๋ค๋ค๋ ๋จ
}
void MyStack::push(int num) {
if (tos == capacity()) {
return;
}
put(tos, num);
tos++;
}
int MyStack::capacity() {
return getCapacity();
}
int MyStack::length() {
return tos;
}
int MyStack::pop() {
if (tos == 0) {
return -1;
}
tos--;
return get(tos);
}
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() << endl;
cout << "์คํ์ ๋ชจ๋ ์์๋ฅผ ํํ์ฌ ์ถ๋ ฅํ๋ค>> ";
while (mStack.length() != 0) {
cout << mStack.pop() << " ";
}
cout << endl << "์คํ์ ํ์ฌ ํฌ๊ธฐ: " << mStack.length() << endl;
}
|
cs |
728x90
๋ฐ์ํ
'C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ช ํ C++] 9์ฅ ์ฐ์ต๋ฌธ์ (0) | 2022.12.05 |
---|---|
[C++] ๋ช ํ C++ 7์ฅ (0) | 2022.12.04 |
[C++] ๋ช ํ c++ 6์ฅ (0) | 2022.12.04 |
[C++] ์์๊ฐ์ํจ์์ ์ถ์ํด๋์ค (0) | 2022.12.04 |
[C++] ์์๊ณผ ์ ๊ทผ์ง์ ์ ์ ๋ฆฌ (0) | 2022.12.03 |
๋๊ธ