백준 18258번 : 큐 2 / C++
2023. 2. 28. 16:35ㆍ개인공부/코딩테스트
https://www.acmicpc.net/problem/18258
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
|
#include<iostream>
using namespace std;
template <typename T>
struct Node
{
Node* NextNode;
T Number;
public:
Node()
: NextNode(nullptr)
, Number(0)
{}
public:
~Node()
{}
};
template <typename T>
class Queue
{
private:
Node<T>* m_FirstNode;
Node<T>* m_EndNode;
int m_Size;
public:
void push(T number)
{
Node<T>* list = new Node<T>;
list->Number = number;
if (m_Size == 0)
{
m_FirstNode = list;
m_EndNode = list;
}
else
{
m_EndNode->NextNode = list;
m_EndNode = list;
}
++m_Size;
}
T pop()
{
if (m_Size == 0)
return -1;
T number = m_FirstNode->Number;
Node<T>* next = m_FirstNode->NextNode;
delete m_FirstNode;
m_FirstNode = next;
--m_Size;
return number;
}
int size()
{
return m_Size;
}
bool empty()
{
if (m_Size == 0)
return 1;
return 0;
}
T front()
{
if (m_Size == 0)
return -1;
return m_FirstNode->Number;
}
T back()
{
if (m_Size == 0)
return -1;
return m_EndNode->Number;
}
Queue()
:m_EndNode(nullptr)
, m_FirstNode(nullptr)
, m_Size(0)
{}
~Queue()
{
while (m_FirstNode != nullptr)
{
Node<T>* Next = m_FirstNode->NextNode;
delete m_FirstNode;
m_FirstNode = Next;
}
}
};
int main()
{
cin.tie(NULL);
cout.tie(NULL);
ios::sync_with_stdio(false);
int TestCase;
cin >> TestCase;
Queue<int> q;
for (int i = 0; i < TestCase; ++i)
{
string str;
cin >> str;
if (str == "push")
{
int number;
cin >> number;
q.push(number);
}
else if (str == "pop")
cout << q.pop() << '\n';
else if (str == "size")
cout << q.size() << '\n';
else if (str == "empty")
cout << q.empty() << '\n';
else if (str == "front")
cout << q.front() << '\n';
else if (str == "back")
cout << q.back() << '\n';
}
return 0;
}
|
cs |
'개인공부 > 코딩테스트' 카테고리의 다른 글
백준 1021번: 회전하는 큐 (0) | 2023.03.01 |
---|---|
백준 10866번 : 덱 / C++ (0) | 2023.02.28 |
백준 11866번 : 요세푸스 문제 0 / C++ (0) | 2023.02.28 |
백준 2164번: 카드2 / C++ (0) | 2023.02.27 |
백준 4949번 : 균형잡힌 세상 / C++ (0) | 2023.02.26 |