Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Võ Tiến

Cấu trúc dữ liệu và giải thuật


Thí Nghiệm (Lab)
nội dung

nhóm thảo luận CSE


https://www.facebook.com/groups/211867931379013

Tp. Hồ Chí Minh, Tháng 2/2024

https://www.facebook.com/groups/211867931379013 Trang 1/35


Nhóm thảo luận CSE
Võ Tiến

Mục lục
1 DoubleLinklist 3
1.0.1 Câu 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.0.2 Câu 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.0.3 Câu 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.0.4 Câu 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.0.5 Câu 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.0.6 Câu 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

2 Các Khóa Học HK232 35

https://www.facebook.com/groups/211867931379013 Trang 2/35


Nhóm thảo luận CSE
Võ Tiến

1 DoubleLinklist
1.0.1 Câu 1

Hình 1: ảnh câu hỏi 1.1-1.2

https://www.facebook.com/groups/211867931379013 Trang 3/35


Nhóm thảo luận CSE
Võ Tiến

Hình 2: ảnh câu hỏi 1.3

1 #include<iostream>
2

3 using namespace std;


4

5 template <class T>


6 class DLinkedList {
7 public:
8 class Node; // Forward declaration
9 protected:
10 Node* head;
11 Node* tail;
12 int count;
13 public:
14 DLinkedList():head(nullptr),tail(nullptr),count(0){};
15 ~DLinkedList(){};
16 void add(const T &e);
17 void add(int index, const T &e);
18 int size();
19 string toString();
20 public:
21 class Node
22 {
23 private:
24 T data;
25 Node *next;
26 Node *previous;
27 friend class DLinkedList<T>;

https://www.facebook.com/groups/211867931379013 Trang 4/35


Nhóm thảo luận CSE
Võ Tiến

28

29 public:
30 Node()
31 {
32 this->previous = NULL;
33 this->next = NULL;
34 }
35

36 Node(const T &data)
37 {
38 this->data = data;
39 this->previous = NULL;
40 this->next = NULL;
41 }
42 };
43

44 };
45

46 template<class T>
47 string DLinkedList<T>::toString() {
48 string s = "[";
49 Node* current = head;
50

51 while (current) {
52 s+= to_string(current->data);
53

54 if (current->next) {
55 s+= ",";
56 }
57 current = current->next;
58 }
59 s+= "]";
60 return s;
61 }
62

63

64 template <class T>


65 void DLinkedList<T>::add(const T& e) {
66 /* //*Insert an element into the end of the list. */
67 //! nếu chưa có phần tử nào thì thêm vào đầu danh sách
68 if(count == 0){
69 tail = head = new Node(e);
70 }
71 else{
72 Node* tmp = new Node(e);
73 //! xử lý phần con trỏ để kết nói 2 chiều
74 tail->next = tmp;
75 tmp->previous = tail;
76 tail = tmp;
77 }
78 //! cộng số lượng danh sách
79 count ++;
80

81 }
82

83 template<class T>

https://www.facebook.com/groups/211867931379013 Trang 5/35


Nhóm thảo luận CSE
Võ Tiến

84 void DLinkedList<T>::add(int index, const T& e) {


85 //! kiểm tra đầu vào có hợp lệ hay không nếu không hợp lệ thì dừng chương trình
86 if(index < 0 && index > count) throw out_of_range("");
87 //! nếu chưa có phần tử nào và thêm vào đầu danh sách
88 else if(count == 0 || index == count ) add(e);
89 else if(index == 0){
90 //! xử lý con trỏ
91 Node* tmp = new Node(e);
92 tmp->next = head;
93 head->previous = tmp;
94 head = tmp;
95 count ++;
96 }
97 else{
98 //! tạo 2 node curr và prev lưu node ở vị trí chèn và node trước vị trí chèn
99 Node* curr = head->next,* prev = head;
100 index --;
101 while(index){
102 index--;
103 prev = curr;
104 curr= curr->next;
105 }
106 //! thực hiện chèn node
107

108 Node* tmp = new Node(e);


109 prev->next = tmp;
110 tmp->previous = prev;
111 tmp->next = curr;
112 curr->previous = tmp;
113 count ++;
114 }
115

116 }
117

118 template<class T>


119 int DLinkedList<T>::size() {
120 return count;
121 }
122

123 int main(){


124 DLinkedList<int> list;
125 int size = 10;
126 for(int idx=0; idx < size; idx++){
127 list.add(idx);
128 }
129 cout << list.toString()<<endl;;
130 //! [0,1,2,3,4,5,6,7,8,9]
131

132 DLinkedList<int> list1;


133 int size1 = 10;
134 for(int idx=0; idx < size; idx++){
135 list1.add(0, idx);
136 }
137 cout << list1.toString();
138 //! [9,8,7,6,5,4,3,2,1,0]

https://www.facebook.com/groups/211867931379013 Trang 6/35


Nhóm thảo luận CSE
Võ Tiến

139 }

1.0.2 Câu 2

Hình 3: ảnh câu hỏi 2

3 #include<iostream>
4

5 using namespace std;


6

7 template <class T>


8 class DLinkedList {
9 public:
10 class Node; // Forward declaration
11 protected:
12 Node* head;
13 Node* tail;
14 int count;
15 public:
16 DLinkedList():head(nullptr),tail(nullptr),count(0){};
17 ~DLinkedList(){};
18 void add(const T &e);
19 void add(int index, const T &e);
20 int size();
21 string toString();
22 bool empty();
23 T get(int index);

https://www.facebook.com/groups/211867931379013 Trang 7/35


Nhóm thảo luận CSE
Võ Tiến

24 void set(int index, const T &e);


25 int indexOf(const T &item);
26 bool contains(const T &item);
27 public:
28 class Node
29 {
30 private:
31 T data;
32 Node *next;
33 Node *previous;
34 friend class DLinkedList<T>;
35

36 public:
37 Node()
38 {
39 this->previous = NULL;
40 this->next = NULL;
41 }
42

43 Node(const T &data)
44 {
45 this->data = data;
46 this->previous = NULL;
47 this->next = NULL;
48 }
49 };
50

51 };
52

53 template<class T>
54 string DLinkedList<T>::toString() {
55 string s = "[";
56 Node* current = head;
57

58 while (current) {
59 s+= to_string(current->data);
60

61 if (current->next) {
62 s+= ",";
63 }
64 current = current->next;
65 }
66 s+= "]";
67 return s;
68 }
69

70

71 template <class T>


72 void DLinkedList<T>::add(const T& e) {
73 /* //*Insert an element into the end of the list. */
74 //! nếu chưa có phần tử nào thì thêm vào đầu danh sách
75 if(count == 0){
76 tail = head = new Node(e);
77 }
78 else{
79 Node* tmp = new Node(e);

https://www.facebook.com/groups/211867931379013 Trang 8/35


Nhóm thảo luận CSE
Võ Tiến

80 //! xử lý phần con trỏ để kết nói 2 chiều


81 tail->next = tmp;
82 tmp->previous = tail;
83 tail = tmp;
84 }
85 //! cộng số lượng danh sách
86 count ++;
87

88 }
89

90 template<class T>
91 void DLinkedList<T>::add(int index, const T& e) {
92 //! kiểm tra đầu vào có hợp lệ hay không nếu không hợp lệ thì dừng chương trình
93 if(index < 0 && index > count) throw out_of_range("");
94 //! nếu chưa có phần tử nào và thêm vào đầu danh sách
95 else if(count == 0 || index == count ) add(e);
96 else if(index == 0){
97 //! xử lý con trỏ
98 Node* tmp = new Node(e);
99 tmp->next = head;
100 head->previous = tmp;
101 head = tmp;
102 count ++;
103 }
104 else{
105 //! tạo 2 node curr và prev lưu node ở vị trí chèn và node trước vị trí chèn
106 Node* curr = head->next,* prev = head;
107 index --;
108 while(index){
109 index--;
110 prev = curr;
111 curr= curr->next;
112 }
113 //! thực hiện chèn node
114

115 Node* tmp = new Node(e);


116 prev->next = tmp;
117 tmp->previous = prev;
118 tmp->next = curr;
119 curr->previous = tmp;
120 count ++;
121 }
122

123 }
124

125 template<class T>


126 int DLinkedList<T>::size() {
127 return count;
128 }
129

130

131 template<class T>


132 T DLinkedList<T>::get(int index) {
133 //! kiểm tra đầu vào có hợp lệ hay không
134 if(index < 0 && index >= count) throw out_of_range("");
135 Node* tmp = head;

https://www.facebook.com/groups/211867931379013 Trang 9/35


Nhóm thảo luận CSE
Võ Tiến

136 while(index){
137 tmp = tmp->next;
138 index --;
139 }
140 return tmp->data;
141

142 }
143

144 template <class T>


145 void DLinkedList<T>::set(int index, const T& e) {
146 //! kiểm tra đầu vào có hợp lệ hay không
147 if(index < 0 && index >= count) throw out_of_range("");
148 Node* tmp = head;
149 while(index){
150 tmp = tmp->next;
151 index --;
152 }
153 tmp->data = e;
154 }
155

156 template<class T>


157 bool DLinkedList<T>::empty() {
158 return count == 0;
159

160 }
161

162 template<class T>


163 int DLinkedList<T>::indexOf(const T& item) {
164 /* //*Return the first index wheter item appears in list, otherwise return -1 */
165 Node* tmp = head;
166 int index = 0;
167 while(tmp){
168 if(tmp->data == item) return index;
169 tmp = tmp->next;
170 index ++;
171 }
172 return -1;
173

174 }
175

176 template<class T>


177 bool DLinkedList<T>::contains(const T& item) {
178 /* //*Check if item appears in the list */
179 Node* tmp = head;
180 while(tmp){
181 if(tmp->data == item) return true;
182 tmp = tmp->next;
183 }
184 return false;
185

186 }
187

188 int main(){


189 DLinkedList<int> list;
190 int size = 10;
191 for(int idx=0; idx < size; idx++){

https://www.facebook.com/groups/211867931379013 Trang 10/35


Nhóm thảo luận CSE
Võ Tiến

192 list.add(idx);
193 }
194 for(int idx=0; idx < size; idx++){
195 cout << list.get(idx) << " |";
196 }
197 //! 0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |
198

199

200 DLinkedList<int> list1;


201 int size1 = 10;
202 int value1[] = {2,5,6,3,67,332,43,1,0,9};
203 for(int idx=0; idx < size1; idx++){
204 list1.add(idx);
205 }
206 for(int idx=0; idx < size1; idx++){
207 list1.set(idx, value1[idx]);
208 }
209 cout << list1.toString();
210 //! [0,1,2,3,4,5,6,7,8,9]
211 }

https://www.facebook.com/groups/211867931379013 Trang 11/35


Nhóm thảo luận CSE
Võ Tiến

1.0.3 Câu 3

Hình 4: ảnh câu hỏi 3.1-3.2

https://www.facebook.com/groups/211867931379013 Trang 12/35


Nhóm thảo luận CSE
Võ Tiến

Hình 5: ảnh câu hỏi 3.3-3.4

https://www.facebook.com/groups/211867931379013 Trang 13/35


Nhóm thảo luận CSE
Võ Tiến

1 #include<iostream>
2

3 using namespace std;


4

5 template <class T>


6 class DLinkedList
7 {
8 public:
9 class Iterator; //forward declaration
10 class Node; //forward declaration
11 protected:
12 Node *head;
13 Node *tail;
14 int count;
15 public:
16 DLinkedList() : head(NULL), tail(NULL), count(0){};
17 ~DLinkedList(){};
18 void add(const T &e);
19 void add(int index, const T &e);
20 T removeAt(int index);
21 bool removeItem(const T &item);
22 bool empty();
23 int size();
24 void clear();
25 T get(int index);
26 void set(int index, const T &e);
27 int indexOf(const T &item);
28 bool contains(const T &item);
29 string toString();
30 Iterator begin()
31 {
32 return Iterator(this, true);
33 }
34 Iterator end()
35 {
36 return Iterator(this, false);
37 }
38 public:
39 class Node
40 {
41 private:
42 T data;
43 Node *next;
44 friend class DLinkedList<T>;
45 public:
46 Node()
47 {
48 next = 0;
49 }
50 Node(Node *next)
51 {
52 this->next = next;
53 }
54 Node(T data, Node *next = NULL)
55 {

https://www.facebook.com/groups/211867931379013 Trang 14/35


Nhóm thảo luận CSE
Võ Tiến

56 this->data = data;
57 this->next = next;
58 }
59 };
60 class Iterator
61 {
62 private:
63 DLinkedList<T> *pList;
64 Node *current;
65 int index; // is the index of current in pList
66 public:
67 Iterator(DLinkedList<T> *pList, bool begin);
68 Iterator &operator=(const Iterator &iterator);
69 void set(const T &e);
70 T &operator*();
71 bool operator!=(const Iterator &iterator);
72 void remove();
73

74 // Prefix ++ overload
75 Iterator &operator++();
76

77 // Postfix ++ overload
78 Iterator operator++(int);
79 };
80 };
81 template <class T>
82 void DLinkedList<T>::add(const T& e) {
83 if(count == 0){
84 head = tail = new Node(e);
85

86 }
87 else{
88 Node* tmp = new Node(e);
89 tail->next = tmp;
90 tail = tmp;
91 }
92 count++;
93

94 }
95 //! hiện thực các hàm như ở câu 1
96 template<class T>
97 void DLinkedList<T>::add(int index, const T& e) {
98 if(index < 0 || index > count) return;
99 else if(count == 0 || index == count) add(e);
100 else if(index == 0){
101 Node* tmp = new Node(e);
102 tmp->next = head;
103 head = tmp;
104 count ++;
105 }
106 else{
107 index --;
108 Node* tmp = head;
109 Node* New = new Node(e);
110 while(index){
111 tmp = tmp->next;

https://www.facebook.com/groups/211867931379013 Trang 15/35


Nhóm thảo luận CSE
Võ Tiến

112 index--;
113 }
114 New->next = tmp->next;
115 tmp->next = New;
116 count ++;
117 }
118

119 }
120

121 template<class T>


122 int DLinkedList<T>::size() {
123 return count;
124 }
125 template<class T>
126 T DLinkedList<T>::get(int index) {
127 //! ném ra ngoại lệ
128 if(index < 0 || index >= count) throw out_of_range("");
129 Node* tmp = head;
130 while(index){
131 index --;
132 tmp = tmp->next;
133 }
134 return tmp->data;
135

136 }
137

138 template <class T>


139 void DLinkedList<T>::set(int index, const T& e) {
140 //! ném ra ngoại lệ
141 if(index < 0 || index >= count) throw out_of_range("");
142 Node* tmp = head;
143 while(index){
144 index --;
145 tmp = tmp->next;
146 }
147 tmp->data = e;
148 }
149

150 template<class T>


151 bool DLinkedList<T>::empty() {
152 return count == 0;
153 }
154

155 template<class T>


156 int DLinkedList<T>::indexOf(const T& item) {
157 //* Return the first index wheter item appears in list, otherwise return -1 *//
158 int i = 0;
159 Node* tmp = head;
160 while(tmp){
161 if(tmp->data == item) return i;
162 i ++;
163 tmp = tmp->next;
164 }
165 return -1;
166 }
167

https://www.facebook.com/groups/211867931379013 Trang 16/35


Nhóm thảo luận CSE
Võ Tiến

168 template<class T>


169 bool DLinkedList<T>::contains(const T& item) {
170 Node* tmp = head;
171 //! duyệt vòng while
172 while(tmp){
173 if(tmp->data == item) return true;
174 tmp = tmp->next;
175 }
176 return false;
177

178 }
179 //! hiện thực hàm toString
180 template<class T>
181 string DLinkedList<T>::toString() {
182 string s = "[";
183 Node* current = head;
184

185 while (current) {


186 s+= to_string(current->data);
187

188 if (current->next) {
189 s+= ",";
190 }
191 current = current->next;
192 }
193 s+= "]";
194 return s;
195 }
196

197 template <class T>


198 T DLinkedList<T>::removeAt(int index)
199 {
200 T result;
201 //! nếu đúng thì chương trình chạy sai thì dừng chương trình
202 if(index < 0 || index > count || count == 0) throw out_of_range("");
203 //! nếu chỉ có 1 phần tử đầu tiên thì reset head = tail = nullptr
204 else if(count == 1){
205 result = head->data;
206 delete head;
207 head =tail = nullptr;
208 }
209 //! nếu xóa ở vị trí 0 thì
210 else if(index == 0){
211 //! lưu data của node đầu tiên lại
212 result = head->data;
213 Node* tmp = head;
214 head = head->next;
215 //! xóa vùng nhớ
216 delete tmp;
217 }
218 else{
219 //! giảm index xuống 1 đơn vị
220 index --;
221 Node* tmp = head;
222 //! dùng vòng lặp đến vị trí cần tìm
223 while(index){

https://www.facebook.com/groups/211867931379013 Trang 17/35


Nhóm thảo luận CSE
Võ Tiến

224 tmp = tmp->next;


225 index --;
226 }
227 if(tmp->next->next){
228 Node* d = tmp->next;
229 tmp->next = tmp->next->next;
230 result = d->data;
231 delete d;
232 }
233 else{
234 //! nếu không thỏa if thì phải lưu tail lại vì phần tử đang xóa là phần tử ở vị trí
↪ cuối cùng
235 tail = tmp;
236 Node* d = tmp->next;
237 tmp->next = nullptr;
238 result = d->data;
239 delete d;
240 }
241 }
242 count --;
243 return result;
244 }
245 //!
246 template <class T>
247 bool DLinkedList<T>::removeItem(const T& item)
248 {
249 //! xóa node có giá trị bằng phần tử ở vị trí đầu tiên của danh sách nếu có phần tử
↪ thì xóa rồi trả về true không thì ngược lại
250 int index = indexOf(item);
251 if(index == -1 ) return false;
252 removeAt(index);
253 return true;
254

255 }
256 //! giải phóng các node
257 template<class T>
258 void DLinkedList<T>::clear(){
259 Node* tmp = head;
260 while(tmp){
261 tmp = tmp->next;
262 delete head;
263 head = tmp;
264 }
265 tail = head = NULL;
266 count = 0;
267 }
268

269 template <class T>


270 DLinkedList<T>::Iterator::Iterator(DLinkedList<T>* pList , bool begin)
271 {
272 /*
273 Constructor of iterator
274 * Set pList to pList
275 * begin = true:
276 * * Set current (index = 0) to pList's head if pList is not NULL
277 * * Otherwise set to NULL (index = -1)

https://www.facebook.com/groups/211867931379013 Trang 18/35


Nhóm thảo luận CSE
Võ Tiến

278 * begin = false:


279 * * Always set current to NULL
280 * * Set index to pList's size if pList is not NULL, otherwise 0
281 */
282

283 this->pList = pList;


284 if(pList == NULL){index = -1;}
285 else if(begin){
286 if(this->pList->size() == 0){this->current = NULL;index = -1;}
287 else{ this->current = pList->head;index = 0;}
288 }
289 else{
290 this->current = NULL;
291 if(this->pList->size() == 0) index = this->pList->size();
292 else index = pList->size();
293 }
294

295

296 }
297

298 template <class T>


299 typename DLinkedList<T>::Iterator& DLinkedList<T>::Iterator::operator=(const
↪ Iterator& iterator)
300 {
301 /*
302 Assignment operator
303 * Set this current, index, pList to iterator corresponding elements.
304 */
305 // cout << this->index;
306 this->pList = iterator.pList;
307 this->current = iterator.current;
308 this->index = iterator.index;
309 return *this;
310 }
311

312 template <class T>


313 void DLinkedList<T>::Iterator::remove()
314 {
315 /*
316 Remove a node which is pointed by current
317 * After remove current points to the previous node of this position (or node
↪ with index - 1)
318 * If remove at front, current points to previous "node" of head (current =
↪ NULL, index = -1)
319 * Exception: throw std::out_of_range("Segmentation fault!") if remove when
↪ current is NULL
320 */
321

322 if(this->current == NULL) throw out_of_range("Segmentation fault!");


323 int index = this->pList->indexOf(this->current->data);
324 if(index == 0){
325

326 this->pList->removeAt(index);
327 this->index = -1;
328 current = NULL;
329 }

https://www.facebook.com/groups/211867931379013 Trang 19/35


Nhóm thảo luận CSE
Võ Tiến

330 else{
331 T e = this->pList->removeAt(index-1);
332 this->index = index - 1;
333 current->data = e;
334

335 }
336 }
337 template <class T>
338 void DLinkedList<T>::Iterator::set(const T& e)
339 {
340 /*
341 Set the new value for current node
342 * Exception: throw std::out_of_range("Segmentation fault!") if current is
↪ NULL
343 */
344 if(current == NULL) throw out_of_range("Segmentation fault!");
345 current->data = e;
346 }
347

348 template <class T>


349 T& DLinkedList<T>::Iterator::operator*()
350 {
351 /*
352 Get data stored in current node
353 * Exception: throw std::out_of_range("Segmentation fault!") if current is
↪ NULL
354 */
355 if(current == NULL) throw out_of_range("Segmentation fault!");
356 //cout << current->data;
357 return current->data;
358 }
359

360 template <class T>


361 bool DLinkedList<T>::Iterator::operator!=(const Iterator& iterator)
362 {
363 /*
364 Operator not equals
365 * Returns false if two iterators points the same node and index
366 */
367 return !(iterator.index == this->index || iterator.current == this->current);
368 }
369 // Prefix ++ overload
370 template <class T>
371 typename DLinkedList<T>::Iterator& DLinkedList<T>::Iterator::operator++()
372 {
373 /*
374 Prefix ++ overload
375 * Set current to the next node
376 * If iterator corresponds to the previous "node" of head, set it to head
377 * Exception: throw std::out_of_range("Segmentation fault!") if iterator
↪ corresponds to the end
378 */
379 if(current == NULL){
380 current = pList->head;
381 index = 0;
382 }

https://www.facebook.com/groups/211867931379013 Trang 20/35


Nhóm thảo luận CSE
Võ Tiến

383 else{
384 current = current->next;
385 index++;
386 }
387 return *this;
388 }
389 // Postfix ++ overload
390 template <class T>
391 typename DLinkedList<T>::Iterator DLinkedList<T>::Iterator::operator++(int)
392 {
393 /*
394 Postfix ++ overload
395 * Set current to the next node
396 * If iterator corresponds to the previous "node" of head, set it to head
397 * Exception: throw std::out_of_range("Segmentation fault!") if iterator
↪ corresponds to the end
398 */
399 DLinkedList<T>::Iterator t = *this;
400 if(current == NULL){
401 current = pList->head;
402 index = 0;
403 }
404 else{
405 current = current->next;
406 index++;
407 }
408 return t;
409 }
410

411 int main(){


412

413 DLinkedList<int> list;


414 int size = 10;
415 for(int idx=0; idx < size; idx++){
416 list.add(idx);
417 }
418 DLinkedList<int>::Iterator it = list.begin();
419 for(; it != list.end(); it++)
420 {
421 cout << *it << " |";
422 }
423 //! 0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |
424

425 DLinkedList<int> list1;


426 int size1 = 10;
427 for (int idx = 0; idx < size1; idx++)
428 {
429 list1.add(idx);
430 }
431

432 DLinkedList<int>::Iterator it1 = list1.begin();


433 for(; it1 != list1.end(); it1++)
434 {
435 it1.remove();
436 }
437 cout<<endl;

https://www.facebook.com/groups/211867931379013 Trang 21/35


Nhóm thảo luận CSE
Võ Tiến

438 cout << list1.toString();


439 //![]
440 }

1.0.4 Câu 4

Hình 6: ảnh câu hỏi 4.1-4.2

https://www.facebook.com/groups/211867931379013 Trang 22/35


Nhóm thảo luận CSE
Võ Tiến

Hình 7: ảnh câu hỏi 4.3

1 #include<iostream>
2

3 using namespace std;


4

5 template <class T>


6 class DLinkedList {
7 public:
8 class Node; // Forward declaration
9 protected:
10 Node* head;
11 Node* tail;
12 int count;
13 public:
14 DLinkedList():head(nullptr),tail(nullptr),count(0){};
15 ~DLinkedList(){};
16 void add(const T &e);
17 void add(int index, const T &e);
18 int size();
19 string toString();
20 bool empty();
21 T get(int index);
22 void set(int index, const T &e);
23 int indexOf(const T &item);
24 bool contains(const T &item);
25 T removeAt(int index);
26 bool removeItem(const T &item);
27 void clear();
28 public:
29 class Node
30 {
31 private:
32 T data;
33 Node *next;
34 Node *previous;
35 friend class DLinkedList<T>;
36

https://www.facebook.com/groups/211867931379013 Trang 23/35


Nhóm thảo luận CSE
Võ Tiến

37 public:
38 Node()
39 {
40 this->previous = NULL;
41 this->next = NULL;
42 }
43

44 Node(const T &data)
45 {
46 this->data = data;
47 this->previous = NULL;
48 this->next = NULL;
49 }
50 };
51

52 };
53

54 template<class T>
55 string DLinkedList<T>::toString() {
56 string s = "[";
57 Node* current = head;
58

59 while (current) {
60 s+= to_string(current->data);
61

62 if (current->next) {
63 s+= ",";
64 }
65 current = current->next;
66 }
67 s+= "]";
68 return s;
69 }
70

71

72 template <class T>


73 void DLinkedList<T>::add(const T& e) {
74 /* //*Insert an element into the end of the list. */
75 //! nếu chưa có phần tử nào thì thêm vào đầu danh sách
76 if(count == 0){
77 tail = head = new Node(e);
78 }
79 else{
80 Node* tmp = new Node(e);
81 //! xử lý phần con trỏ để kết nói 2 chiều
82 tail->next = tmp;
83 tmp->previous = tail;
84 tail = tmp;
85 }
86 //! cộng số lượng danh sách
87 count ++;
88

89 }
90

91 template<class T>
92 void DLinkedList<T>::add(int index, const T& e) {

https://www.facebook.com/groups/211867931379013 Trang 24/35


Nhóm thảo luận CSE
Võ Tiến

93 //! kiểm tra đầu vào có hợp lệ hay không nếu không hợp lệ thì dừng chương trình
94 if(index < 0 && index > count) throw out_of_range("");
95 //! nếu chưa có phần tử nào và thêm vào đầu danh sách
96 else if(count == 0 || index == count ) add(e);
97 else if(index == 0){
98 //! xử lý con trỏ
99 Node* tmp = new Node(e);
100 tmp->next = head;
101 head->previous = tmp;
102 head = tmp;
103 count ++;
104 }
105 else{
106 //! tạo 2 node curr và prev lưu node ở vị trí chèn và node trước vị trí chèn
107 Node* curr = head->next,* prev = head;
108 index --;
109 while(index){
110 index--;
111 prev = curr;
112 curr= curr->next;
113 }
114 //! thực hiện chèn node
115

116 Node* tmp = new Node(e);


117 prev->next = tmp;
118 tmp->previous = prev;
119 tmp->next = curr;
120 curr->previous = tmp;
121 count ++;
122 }
123

124 }
125

126 template<class T>


127 int DLinkedList<T>::size() {
128 return count;
129 }
130

131

132 template<class T>


133 T DLinkedList<T>::get(int index) {
134 //! kiểm tra đầu vào có hợp lệ hay không
135 if(index < 0 && index >= count) throw out_of_range("");
136 Node* tmp = head;
137 while(index){
138 tmp = tmp->next;
139 index --;
140 }
141 return tmp->data;
142

143 }
144

145 template <class T>


146 void DLinkedList<T>::set(int index, const T& e) {
147 //! kiểm tra đầu vào có hợp lệ hay không
148 if(index < 0 && index >= count) throw out_of_range("");

https://www.facebook.com/groups/211867931379013 Trang 25/35


Nhóm thảo luận CSE
Võ Tiến

149 Node* tmp = head;


150 while(index){
151 tmp = tmp->next;
152 index --;
153 }
154 tmp->data = e;
155 }
156

157 template<class T>


158 bool DLinkedList<T>::empty() {
159 return count == 0;
160

161 }
162

163 template<class T>


164 int DLinkedList<T>::indexOf(const T& item) {
165 /* //*Return the first index wheter item appears in list, otherwise return -1 */
166 Node* tmp = head;
167 int index = 0;
168 while(tmp){
169 if(tmp->data == item) return index;
170 tmp = tmp->next;
171 index ++;
172 }
173 return -1;
174

175 }
176

177 template<class T>


178 bool DLinkedList<T>::contains(const T& item) {
179 /* //*Check if item appears in the list */
180 Node* tmp = head;
181 while(tmp){
182 if(tmp->data == item) return true;
183 tmp = tmp->next;
184 }
185 return false;
186

187 }
188

189 template <class T>


190 T DLinkedList<T>::removeAt(int index)
191 {
192 T result;
193 //! kiểm tra đầu vào
194 if(index < 0 && index >= count) throw out_of_range("");
195 //! nếu chỉ có 1 phần tử
196 if(count == 1){
197 result = head->data;
198 delete head;
199 head = tail = NULL;
200 }
201 //! nhiều hơn 1 phần tử và ở vị trí đầu tiên
202 else if(index == 0){
203 Node* tmp = head;
204 head = head->next;

https://www.facebook.com/groups/211867931379013 Trang 26/35


Nhóm thảo luận CSE
Võ Tiến

205 result = tmp->data;


206 delete tmp;
207 head->previous = NULL;
208 }
209 else{
210 Node* tmp = head;
211 //! tìm vị trí trước vị trí cần xóa
212 index --;
213 while(index){
214 tmp = tmp->next;
215 index --;
216 }
217 //! lưu vị giá trị xóa đi
218 result = tmp->next->data;
219 //! nếu nó là vị trí cuối cùng
220 if(tmp->next == tail){
221 delete tail;
222 tail = tmp;
223 tail->next = NULL;
224 }
225 else{
226 Node* h = tmp->next;
227 tmp->next = tmp->next->next;
228 tmp->next->previous = tmp;
229 delete h;
230 }
231 }
232 count --;
233 return result;
234 }
235

236 template <class T>


237 bool DLinkedList<T>::removeItem(const T& item)
238 {
239 int index = indexOf(item);
240 if(index == -1) return false;
241 removeAt(index);
242 return true;
243

244 }
245

246 template<class T>


247 void DLinkedList<T>::clear(){
248 Node* tmp = head;
249 while(head){
250 head = head->next;
251 delete tmp;
252 tmp = head;
253 }
254 head = tail = NULL;
255 count = 0;
256 }
257

258 int main(){


259 DLinkedList<int> list;
260 int size = 10;

https://www.facebook.com/groups/211867931379013 Trang 27/35


Nhóm thảo luận CSE
Võ Tiến

261 int value[] = {2,5,6,3,67,332,43,1,0,9};


262

263 for(int idx=0; idx < size; idx++){


264 list.add(value[idx]);
265 }
266 list.removeAt(0);
267 cout << list.toString();
268 //! [5,6,3,67,332,43,1,0,9]
269 }

1.0.5 Câu 5

Hình 8: ảnh câu hỏi 5.1-5.2

https://www.facebook.com/groups/211867931379013 Trang 28/35


Nhóm thảo luận CSE
Võ Tiến

images/DoubleLinklist5.4.jpeg
Hình 9: ảnh câu hỏi 5.3-5.4

1 #include<iostream>
2 #include <list>
3

4 using namespace std;


5

6 class DataLog
7 {
8 private:
9 list<int> logList;
10 list<int>::iterator currentState;
11

12 public:
13 DataLog();
14 DataLog(const int &data);
15 void addCurrentState(int number);
16 void subtractCurrentState(int number);
17 void save();
18 void undo();
19 void redo();
20

21 int getCurrentStateData()
22 {
23 return *currentState;
24 }
25

26 void printLog()
27 {
28 for (auto i = logList.begin(); i != logList.end(); i++) {
29 if(i == currentState) cout << "Current state: ";
30 cout << "[ " << *i << " ] => ";
31 }

https://www.facebook.com/groups/211867931379013 Trang 29/35


Nhóm thảo luận CSE
Võ Tiến

32 cout << "END_LOG";


33 }
34 };
35

36 DataLog::DataLog()
37 {
38 /*
39 * TODO: add the first state with 0
40 */
41 logList.push_front(0);
42 currentState = logList.begin();
43 }
44

45 DataLog::DataLog(const int &data)


46 {
47 /*
48 * TODO: add the first state with data
49 */
50 logList.push_front(data);
51 currentState = logList.begin();
52

53 }
54

55 void DataLog::addCurrentState(int number)


56 {
57 *currentState += number;
58 }
59

60 void DataLog::subtractCurrentState(int number)


61 {
62 /*
63 * TODO: Decrease the value of current state by number
64 */
65 *currentState -= number;
66 }
67

68 void DataLog::save()
69 {
70 /*
71 * TODO: This function will create a new state, copy the data of the
↪ currentState
72 * and move the currentState Iterator to this new state. If there are
↪ other states behind the
73 * currentState Iterator, we delete them all before creating a new state.
74 */
75 //! Di chuyển con trỏ currentState lên một bước để trỏ tới phần tử tiếp theo trong
↪ danh sách.
76 currentState++;
77 //! Kiểm tra nếu currentState không trỏ tới cuối danh sách.
78 if(currentState != logList.end()){
79 list<int>::iterator curr = currentState;
80 list<int>::iterator prev = currentState;
81 while(curr != logList.end()){
82 curr++;
83 logList.erase(prev);
84 prev = curr;

https://www.facebook.com/groups/211867931379013 Trang 30/35


Nhóm thảo luận CSE
Võ Tiến

85 }
86 currentState = logList.end();
87

88 }
89 currentState--;
90

91 logList.push_back(*currentState);
92 currentState++;
93 for (auto i = logList.begin(); i != logList.end(); i++) {
94 cout << "[ " << *i << " ] => ";
95 }
96 cout<<endl;
97

98 }
99

100 void DataLog::undo()


101 {
102 /*
103 * TODO: Switch to the previous state of the data
104 * If this is the oldest state in the log, nothing changes
105 */
106 if(currentState != logList.begin())
107 --currentState;
108 }
109 void DataLog::redo()
110 {
111 /*
112 * TODO: Switch to the latter state of the data
113 * If this is the latest state in the log, nothing changes
114 */
115 ++currentState;
116 if(currentState == logList.end()) --currentState;
117

118

119 }
120

121

122 int main(){


123 DataLog log(10);
124 log.save();
125 log.addCurrentState(15);
126 log.save();
127 log.addCurrentState(15);
128 log.undo();
129 log.printLog();
130 //! [ 10 ] => Current state: [ 25 ] => [ 40 ] => END_LOG
131 cout<<endl;
132 DataLog log1(10);
133 log1.save();
134 log1.addCurrentState(15);
135 log1.save();
136 log1.addCurrentState(15);
137 log1.save();
138 log1.subtractCurrentState(5);
139 log1.printLog();
140

https://www.facebook.com/groups/211867931379013 Trang 31/35


Nhóm thảo luận CSE
Võ Tiến

141 // //! [ 10 ] => [ 25 ] => [ 40 ] => Current state: [ 35 ] => END_LOG


142 }
143

1.0.6 Câu 6

Hình 10: ảnh câu hỏi 6

https://www.facebook.com/groups/211867931379013 Trang 32/35


Nhóm thảo luận CSE
Võ Tiến

1 #include<iostream>
2 #include <unordered_map>
3

4 using namespace std;


5

7 struct ListNode {
8 int val;
9 ListNode *left;
10 ListNode *right;
11 ListNode(int x = 0, ListNode *l = nullptr, ListNode* r = nullptr) : val(x),
↪ left(l), right(r) {}
12 };
13

14 ListNode* reverse(ListNode* head, int a, int b) {


15 //! Khai báo một bảng băm (unordered_map) để lưu trữ con trỏ tới các ListNode với
↪ khóa là chỉ số.
16 unordered_map<int ,ListNode* > v;
17 //! Khởi tạo một con trỏ tạm thời để duyệt qua danh sách.
18 ListNode* tmp = head;
19 //! Khởi tạo biến index để theo dõi chỉ số của các nút trong danh sách.
20 int index = 1;
21 //! Khởi tạo biến i với giá trị b, sẽ được sử dụng để đảo ngược thứ tự của các nút
↪ từ b đến a.
22 int i = b;
23 while(tmp){
24 if(index >= a && index <= b){
25 //! Lưu trữ con trỏ tới các nút trong khoảng a đến b vào bảng băm v.
26 v[i] = tmp;
27 i--;
28 }
29 //! Lưu trữ con trỏ tới các nút nằm ngoài khoảng a đến b.
30 else v[index] = tmp;
31 tmp = tmp->right;
32 index ++;
33 }
34 //! Gán con trỏ head bằng con trỏ tới nút đầu tiên sau khi đảo ngược.
35 head = v[1];
36 head->left = NULL;
37 tmp = head;
38 for(int i = 2; i < index; i++){
39 //! thực hiện các thao tác liên kết lại
40 tmp->right = v[i];
41 v[i]->left = tmp;
42 tmp = v[i];
43 }
44 tmp->right = NULL;
45 return head;
46 }
47 ListNode* init(int* list, int size, unordered_map<ListNode*, int>& nodeValue) {
48 if (size == 0) {
49 return nullptr;
50 }
51

52 ListNode* head = new ListNode(list[0]);

https://www.facebook.com/groups/211867931379013 Trang 33/35


Nhóm thảo luận CSE
Võ Tiến

53 nodeValue[head] = list[0];
54 ListNode* prev = head;
55

56 for (int i = 1; i < size; i++) {


57 ListNode* newNode = new ListNode(list[i]);
58 nodeValue[newNode] = list[i];
59 prev->right = newNode;
60 newNode->left = prev;
61 prev = newNode;
62 }
63

64 return head;
65 }
66

67 void printList(ListNode* head, unordered_map<ListNode*, int>& nodeValue) {


68 ListNode* tmp = head;
69 while (tmp) {
70 cout << tmp->val << " ";
71 tmp = tmp->right;
72 }
73 cout << endl;
74 }
75

76 void freeMem(ListNode* head) {


77 while (head) {
78 ListNode* tmp = head;
79 head = head->right;
80 delete tmp;
81 }
82 }
83

84

85 int main(){
86 int size;
87 cin >> size;
88 int* list = new int[size];
89 for(int i = 0; i < size; i++) {
90 cin >> list[i];
91 }
92 int a, b;
93 cin >> a >> b;
94 unordered_map<ListNode*, int> nodeValue;
95 ListNode* head = init(list, size, nodeValue);
96 ListNode* reversed = reverse(head, a, b);
97 try {
98 printList(reversed, nodeValue);
99 }
100 catch(char const* err) {
101 cout << err << '\n';
102 }
103 freeMem(head);
104 delete[] list;
105 }

https://www.facebook.com/groups/211867931379013 Trang 34/35


Nhóm thảo luận CSE
Võ Tiến

2 Các Khóa Học HK232


Võ Tiến
https://www.facebook.com/profile.php?id=100056605580171
Các lớp HK232 sẽ mở
• Lớp BTL1 + GK (giữa kì) + LAB + Lý thuyết + Harmony của môn
DSA HK232
• Lớp BTL2 + CK (cuối kì) + LAB + Lý thuyết + Harmony của môn
DSA HK232
• Lớp BTL1 + LAB + Lý thuyết + Harmony của môn KTLT HK232
• Lớp BTL2 + LAB + Lý thuyết + Harmony + CK của môn KTLT
HK232
• Lớp BTL1 + BTL2 + GK + Harmony của môn PPL HK232
• Lớp BTL3 + BTL4 + CK + Harmony của môn PPL HK232
CHÚC CÁC EM HỌC TỐT

https://www.facebook.com/groups/211867931379013 Trang 35/35


Nhóm thảo luận CSE

You might also like