-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpa_lab_5_3_13_(6)-B.cpp
196 lines (173 loc) · 3.07 KB
/
cpa_lab_5_3_13_(6)-B.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
using namespace std;
class Node
{
public:
Node(int val);
int value;
Node* next;
};
Node::Node(int val) : value(val), next(nullptr) { }
class List
{
public:
List();
void push_back(int value);
void push_front(int value);
bool pop_front(int value);
bool pop_back(int value);
int size();
int at(int index);
void insert_at(int index, int value);
void remove_at(int index);
private:
Node* head;
Node* tail;
};
int List::size() {
Node *t = head;
int i = 0;
while (t != nullptr) {
t = t->next;
++i;
}
return i;
}
List::List() : head(nullptr) { }
int List::at(int index)
{
Node *t = head;
int i = 0;
while (i < index && t != nullptr) {
t = t->next;
++i;
}
return t->value;
}
void List::insert_at(int index, int value) {
Node *t = head, *pre = nullptr;
int i = 0;
while (i < index && t != nullptr) {
pre = t;
t = t->next;
++i;
}
Node *n = new Node(value);
if (pre == nullptr) {
n->next = head;
head = n;
}
else {
n->next = pre->next;
pre->next = n;
}
}
void List::remove_at(int index) {
Node *t = head, *pre = nullptr;
int i = 0;
while (i < index && t != nullptr) {
pre = t;
t = t->next;
++i;
}
if (pre == nullptr) {
head = head->next;
}
else {
pre->next = t->next;
}
delete t;
}
void List::push_front(int value)
{
Node* new_head = new Node(value);
new_head->next = head;
if(head == nullptr)
tail = new_head;
head=new_head;
}
void List::push_back(int value)
{
if(head != nullptr)
{
Node* new_tail = new Node(value);
tail->next = new_tail;
tail = new_tail;
}
else push_front(value);
}
bool List::pop_front(int value)
{
Node *t = head, *prev = nullptr;
while (t != nullptr) {
if (t->value == value) {
if (prev == nullptr) {
prev = head;
head = head->next;
if (head->next == nullptr)
tail = head;
delete prev;
}
else
{
if (t->next == nullptr)
tail = prev;
prev->next = t->next;
delete t;
}
return true;
}
else
{
prev = t;
t = t->next;
}
}
return false;
}
bool List::pop_back(int value) {
Node *t = head, *pre = nullptr, *temp = nullptr;
while (t->next != nullptr)
{
if (t->value == value)
{
temp = pre;
}
pre = t;
t = t->next;
}
if (tail->value == value) {
temp = pre;
tail = pre;
}
if (temp != nullptr) {
t = temp->next;
temp->next = t->next;
delete t;
return true;
}
return false;
}
void printList(List list)
{
for (int i = 0; i < list.size(); i++)
{
cout << "list[" << i << "] == " << list.at(i) << endl;
}
}
int main()
{
List list;
list.push_front(1);
list.push_front(2);
list.push_front(3);
list.push_front(4);
printList(list);
cout << endl;
list.remove_at(2);
printList(list);
cout << endl;
list.insert_at(1, 6);
printList(list);
return 0;
}