-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_1058.cpp
171 lines (143 loc) · 2.75 KB
/
2_1058.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <cstdio>
using namespace std;
namespace LIST
{
struct NODE {
// TODO
int data;
NODE* next;
};
NODE* head = nullptr;
int len = 0;
void init()
{
// TODO
head = new NODE;
head->data = -1;
head->next = head;
}
NODE* move(int i)
{
// TODO
NODE* node = head;
for (int j = 0; j < i; j++)
{
node = node->next;
}
return node;
}
void insert(int i, int x)
{
// TODO
NODE* pre_node = move(i);
NODE* new_node = new NODE;
new_node->data = x;
new_node->next = pre_node->next;
pre_node->next = new_node;
len++;
}
void remove(int i)
{
// TODO
NODE* pre_node = move(i);
NODE* del_node;
del_node = pre_node->next;
pre_node->next = del_node->next;
delete del_node;
len--;
}
void remove_insert(int i)
{
//TODO
NODE* node = move(i + 1);
int x = node->data;
remove(i);
insert(len, x);
}
void get_length()
{
// TODO
cout << len << endl;
}
void query(int i)
{
// TODO
if (i < 0 || i > len - 1)
{
cout << -1 << endl;
}
else
{
NODE* node = move(i + 1);
cout << node->data << endl;
}
}
void get_max()
{
// TODO
NODE* node = head;
if (len > 0)
{
int maxnum = node->data;
while (node->next != head)
{
node = node->next;
maxnum = max(maxnum, node->data);
}
cout << maxnum << endl;
}
else
cout << -1 << endl;
}
void clear()
{
// TODO
NODE* q = head;
NODE* p;
while (q->next != head)
{
p = q->next;
delete q;
q = p;
}
delete q;
}
}
int n;
int main()
{
cin >> n;
int op, x, p;
LIST::init();
for (int _ = 0; _ < n; ++_)
{
cin >> op;
switch (op) {
case 0:
LIST::get_length();
break;
case 1:
cin >> p >> x;
LIST::insert(p, x);
break;
case 2:
cin >> p;
LIST::query(p);
break;
case 3:
cin >> p;
LIST::remove(p);
break;
case 4:
cin >> p;
LIST::remove_insert(p);
break;
case 5:
LIST::get_max();
break;
}
}
LIST::clear();
return 0;
}