Professional Documents
Culture Documents
DoubleLinklist Lab1
DoubleLinklist Lab1
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
1 DoubleLinklist
1.0.1 Câu 1
1 #include<iostream>
2
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
81 }
82
83 template<class T>
116 }
117
139 }
1.0.2 Câu 2
3 #include<iostream>
4
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
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
123 }
124
130
136 while(index){
137 tmp = tmp->next;
138 index --;
139 }
140 return tmp->data;
141
142 }
143
160 }
161
174 }
175
186 }
187
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
1.0.3 Câu 3
1 #include<iostream>
2
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;
112 index--;
113 }
114 New->next = tmp->next;
115 tmp->next = New;
116 count ++;
117 }
118
119 }
120
136 }
137
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
188 if (current->next) {
189 s+= ",";
190 }
191 current = current->next;
192 }
193 s+= "]";
194 return s;
195 }
196
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
295
296 }
297
326 this->pList->removeAt(index);
327 this->index = -1;
328 current = NULL;
329 }
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
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
1.0.4 Câu 4
1 #include<iostream>
2
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
89 }
90
91 template<class T>
92 void DLinkedList<T>::add(int index, const T& e) {
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
124 }
125
131
143 }
144
161 }
162
175 }
176
187 }
188
244 }
245
1.0.5 Câu 5
images/DoubleLinklist5.4.jpeg
Hình 9: ảnh câu hỏi 5.3-5.4
1 #include<iostream>
2 #include <list>
3
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 }
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
53 }
54
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;
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
118
119 }
120
121
1.0.6 Câu 6
1 #include<iostream>
2 #include <unordered_map>
3
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
53 nodeValue[head] = list[0];
54 ListNode* prev = head;
55
64 return head;
65 }
66
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 }