-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpa_lab_5_3_13_(7)-B.cpp
180 lines (160 loc) · 2.88 KB
/
cpa_lab_5_3_13_(7)-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
#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();
List(List *other);
void push_front(int value);
bool pop_front(int value);
void push_back(int value);
bool pop_back(int value);
int at(int index);
void insert_at(int index, int value);
void remove_at(int index);
int size();
Node* get(int index);
private:
Node* head;
Node* tail;
};
int List::size()
{
Node* temp = head;
int result = 0;
while (temp != nullptr)
{
result++;
temp = temp->next;
}
return result;
}
List::List()
{
head = nullptr;
tail = nullptr;
}
Node* List::get(int index)
{
Node* result = this->head;
while (index > 0)
{
index--;
result = result->next;
}
return result;
}
List::List(List *other)
{
if (other->head != nullptr)
{
head = new Node(other->head->value);
Node *temp = other->head;
Node *prev = head;
while (temp->next != nullptr)
{
temp = temp->next;
tail = new Node(temp->value);
prev->next = tail;
prev = tail;
}
}
else
{
List();
}
}
void List::push_front(int value)
{
Node* newhead = new Node(value);
newhead->next = this->head;
this->head = newhead;
if (size() == 1) this->tail = this->head;
}
void List::push_back(int value)
{
Node* newtail = new Node(value);
this->tail->next = newtail;
this->tail = newtail;
if (this->size() == 0) this->head = this->tail;
}
bool List::pop_front(int value)
{
if (this->head == nullptr) return false;
Node* temp = this->head->next;
delete this->head;
this->head = temp;
return true;
}
bool List::pop_back(int value)
{
if (this->tail == nullptr) return false;
delete this->tail;
this->tail = get(this->size() - 1);
this->tail->next = nullptr;
return true;
}
int List::at(int index)
{
return this->get(index)->value;
}
void List::insert_at(int index, int value)
{
Node* temp = this->get(index - 1);
Node* newnode = new Node(value);
newnode->next = temp->next;
temp->next = newnode;
}
void List::remove_at(int index)
{
if (index == 0) this->pop_front(0);
else
{
Node* temp = this->get(index - 1);
Node* toremove = this->get(index);
temp->next = toremove->next;
delete toremove;
}
}
void printList(List list)
{
for (int i = 0; i < list.size(); i++)
{
cout << "list[" << i << "] == " << list.at(i) << endl;
}
}
int main()
{
List list1;
list1.push_front(1);
list1.push_front(2);
list1.push_front(3);
list1.push_front(4);
cout << "list1" << endl;
printList(list1);
cout << endl;
List list2 = List(&list1);
cout << "list2" << endl;
printList(list2);
cout << endl;
list1.insert_at(1, 6);
list2.remove_at(2);
cout << "list1" << endl;
printList(list1);
cout << endl << "list2" << endl;
printList(list2);
cout << endl;
return 0;
}