C++
[λͺ ν c++] 8μ₯ μ°μ΅λ¬Έμ
Meaning_
2022. 12. 5. 10:06
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
λ°μν