Double Linked List Review & Implementation
Double Linked List Review & Implementation
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 1/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Câu hỏi 1
Chính xác
Implement methods add, size in template class DLinkedList (which implements List ADT) representing the doubly linked list with
type T with the initialized frame. The description of each method is given in the code.
public:
Node()
{
this->previous = NULL;
this->next = NULL;
}
Node(const T &data)
{
this->data = data;
this->previous = NULL;
this->next = NULL;
}
};
};
In this exercise, we have include <iostream>, <string>, <sstream> and using namespace std.
For example:
Test Result
Reset answer
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 3/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 4/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Câu hỏi 2
Chính xác
Implement methods get, set, empty, indexOf, contains in template class DLinkedList (which implements List ADT) representing
the singly linked list with type T with the initialized frame. The description of each method is given in the code.
public:
Node()
{
this->previous = NULL;
this->next = NULL;
}
Node(const T &data)
{
this->data = data;
this->previous = NULL;
this->next = NULL;
}
};
};
In this exercise, we have include <iostream>, <string>, <sstream> and using namespace std.
For example:
Test Result
DLinkedList<int> list; 0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |
int size = 10;
for(int idx=0; idx < size; idx++){
list.add(idx);
}
for(int idx=0; idx < size; idx++){
cout << list.get(idx) << " |";
}
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 5/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Test Result
Reset answer
1 template<class T>
2 ▼ T DLinkedList<T>::get(int index) {
3 /* Give the data of the element at given index in the list. */
4 if(index>= this->count||index<0) throw std::out_of_range("-1");
5 Node *current = head;
6 ▼ for(int i =0;i<index;i++){
7 current = current->next;
8 }
9 return current->data;
10 }
11
12 template <class T>
13 ▼ void DLinkedList<T>::set(int index, const T& e) {
14 /* Assign new value for element at given index in the list */
15 if(index>= this->count||index<0) throw std::out_of_range("-1");
16 Node *current = head;
17 ▼ for(int i =0;i<index;i++){
18 current = current->next;
19 }
20 current->data = e;
21 }
22
23 template<class T>
24 ▼ bool DLinkedList<T>::empty() {
25 /* Check if the list is empty or not. */
26 if(this->count ==0) return 1;
27 return 0;
28
29 }
30
31 template<class T>
32 ▼ int DLinkedList<T>::indexOf(const T& item) {
33 /* Return the first index wheter item appears in list, otherwise return -1 */
34 Node *current = head;
35 int n=0;
36 ▼ while(current !=nullptr){
37 if (current->data == item ) return n;
38 n++;
39 current = current->next;
40 }
41 return -1;
42 }
43
44 template<class T>
45 ▼ bool DLinkedList<T>::contains(const T& item) {
46 /* Check if item appears in the list */
47 Node *current = head;
48 int n=0;
49 ▼ while(current !=nullptr){
50 if (current->data == item ) return 1;
51 n++;
52 current = current->next;
53 }
54 return 0;
55 }
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 6/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
DLinkedList<int> list; 0 |1 |2 |3 |4 |5 |6 |7 |8 |9 | 0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |
int size = 10;
for(int idx=0; idx < size; idx++){
list.add(idx);
}
for(int idx=0; idx < size; idx++){
cout << list.get(idx) << " |";
}
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 7/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Câu hỏi 3
Chính xác
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 8/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Iterator begin()
{
return Iterator(this, true);
}
Iterator end()
{
return Iterator(this, false);
}
public:
Node()
{
this->previous = NULL;
this->next = NULL;
}
Node(const T &data)
{
this->data = data;
this->previous = NULL;
this->next = NULL;
}
};
class Iterator
{
private:
DLinkedList<T> *pList;
Node *current;
int index; // is the index of current in pList
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 9/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
// Prefix ++ overload
Iterator &operator++();
// Postfix ++ overload
Iterator operator++(int);
};
};
For example:
Test Result
DLinkedList<int> list; 0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |
int size = 10;
for(int idx=0; idx < size; idx++){
list.add(idx);
}
DLinkedList<int>::Iterator it = list.begin();
for(; it != list.end(); it++)
{
cout << *it << " |";
}
DLinkedList<int> list; []
int size = 10;
for (int idx = 0; idx < size; idx++)
{
list.add(idx);
}
DLinkedList<int>::Iterator it = list.begin();
while (it != list.end())
{
it.remove();
it++;
}
cout << list.toString();
DLinkedList<int> list; []
int size = 10;
for (int idx = 0; idx < size; idx++)
{
list.add(idx);
}
DLinkedList<int>::Iterator it = list.begin();
for(; it != list.end(); it++)
{
it.remove();
}
cout << list.toString();
Reset answer
1 ▼ /*
2 * TODO: Implement class Iterator's method
3 * Note: method remove is different from SLinkedList, which is the advantage of DLinkedList
4 */
5 template <class T>
6 DLinkedList<T>::Iterator::Iterator(DLinkedList<T> *pList, bool begin)
7▼ {
8 this->pList = pList;
9▼ if(begin == 1) {
10 if(thi Li t ! ll t ){
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 10/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
DLinkedList<int> list; 0 |1 |2 |3 |4 |5 |6 |7 |8 |9 | 0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |
int size = 10;
for(int idx=0; idx < size; idx++){
list.add(idx);
}
DLinkedList<int>::Iterator it = list.begin();
for(; it != list.end(); it++)
{
cout << *it << " |";
}
DLinkedList<int> list; [] []
int size = 10;
for (int idx = 0; idx < size; idx++)
{
list.add(idx);
}
DLinkedList<int>::Iterator it = list.begin();
while (it != list.end())
{
it.remove();
it++;
}
cout << list.toString();
DLinkedList<int> list; [] []
int size = 10;
for (int idx = 0; idx < size; idx++)
{
list.add(idx);
}
DLinkedList<int>::Iterator it = list.begin();
for(; it != list.end(); it++)
{
it.remove();
}
cout << list.toString();
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 12/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Câu hỏi 4
Chính xác
Implement methods removeAt, removeItem, clear in template class SLinkedList (which implements List ADT) representing the
singly linked list with type T with the initialized frame. The description of each method is given in the code.
public:
Node()
{
this->previous = NULL;
this->next = NULL;
}
Node(const T &data)
{
this->data = data;
this->previous = NULL;
this->next = NULL;
}
};
};
In this exercise, we have include <iostream>, <string>, <sstream> and using namespace std.
For example:
Test Result
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 13/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Reset answer
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 14/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 15/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Câu hỏi 5
Chính xác
In this exercise, we will use Standard Template Library List (click open in other tab to show more) to implement a Data Log.
This is a simple implementation in applications using undo and redo. For example in Microsoft Word, you must have nodes to store states when
Ctrl Z or Ctrl Shift Z to go back or forward.
DataLog has a doubly linked list to store the states of data (an integer) and iterator to mark the current state. Each state is stored in a node, the
transition of states is depicted in the figure below.
Your task in this exercise is implement functions marked with /* * TODO */.
class DataLog
{
private:
list<int> logList;
list<int>::iterator currentState;
public:
DataLog();
DataLog(const int &data);
void addCurrentState(int number);
void subtractCurrentState(int number);
void save();
void undo();
void redo();
int getCurrentStateData()
{
return *currentState;
}
void printLog()
{
for (auto i = logList.begin(); i != logList.end(); i++) {
if(i == currentState) cout << "Current state: ";
cout << "[ " << *i << " ] => ";
}
cout << "END_LOG";
}
};
Note: Normally, when we say a List, we talk about doubly linked list. For implementing a singly linked list, we use forward list.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 16/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Test Result
Reset answer
1 DataLog::DataLog()
2▼ {
3▼ /*
4 * TODO: add the first state with 0
5 */
6 logList.push_back(0);
7 currentState = logList.begin();
8 }
9
10 DataLog::DataLog(const int &data)
11 ▼ {
12 ▼ /*
13 * TODO: add the first state with data
14 */
15 logList.push_back(data);
16 currentState = logList.begin();
17 }
18
19 void DataLog::addCurrentState(int number)
20 ▼ {
21 ▼ /*
22 * TODO: Increase the value of current state by number
23 */
24 ▼ if (currentState != logList.end()) {
25 *currentState += number;}
26 }
27
28 void DataLog::subtractCurrentState(int number)
29 ▼ {
30 ▼ /*
31 * TODO: Decrease the value of current state by number
32 */
33 ▼ if (currentState != logList.end()) {
34 *currentState -= number;}
35 }
36
37 void DataLog::save()
38 ▼ {
39 ▼ /*
40 * TODO: This function will create a new state, copy the data of the currentState
41 * and move the currentState Iterator to this new state. If there are other states behind the
42 * currentState Iterator, we delete them all before creating a new state.
43 */
44 list<int>::iterator after = currentState;
45 after++;
46 if(after != logList.end())logList.erase(after,logList.end());
47 logList.push_back(*currentState);
48 currentState = prev(logList.end());
49
50 }
51
52 void DataLog::undo()
53 ▼ {
54 /*
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 17/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
DataLog log(10); [ 10 ] => Current state: [ 25 ] => [ 40 ] [ 10 ] => Current state: [ 25 ] => [ 40 ]
log.save(); => END_LOG => END_LOG
log.addCurrentState(15);
log.save();
log.addCurrentState(15);
log.undo();
log.printLog();
DataLog log(10); [ 10 ] => [ 25 ] => [ 40 ] => Current [ 10 ] => [ 25 ] => [ 40 ] => Current
log.save(); state: [ 35 ] => END_LOG state: [ 35 ] => END_LOG
log.addCurrentState(15);
log.save();
log.addCurrentState(15);
log.save();
log.subtractCurrentState(5);
log.printLog();
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 18/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
Câu hỏi 6
Chính xác
Given the head of a doubly linked list, two positive integer a and b where a <= b. Reverse the nodes of the list from position a to position b and
return the reversed list
Note: the position of the first node is 1. It is guaranteed that a and b are valid positions. You MUST NOT change the val attribute in each node.
struct ListNode {
int val;
ListNode *left;
ListNode *right;
ListNode(int x = 0, ListNode *l = nullptr, ListNode* r = nullptr) : val(x), left(l), right(r) {}
};
Constraint:
1 <= list.length <= 10^5
0 <= node.val <= 5000
1 <= left <= right <= list.length
Example 1:
Input: list = {3, 4, 5, 6, 7} , a = 2, b = 4
Output: 3 6 5 4 7
Example 2:
Input: list = {8, 9, 10}, a = 1, b = 3
Output: 10 9 8
For example:
int size; 5 3 6 5 4 7
cin >> size; 3 4 5 6 7
int* list = new int[size]; 2 4
for(int i = 0; i < size; i++) {
cin >> list[i];
}
int a, b;
cin >> a >> b;
unordered_map<ListNode*, int> nodeValue;
ListNode* head = init(list, size, nodeValue);
ListNode* reversed = reverse(head, a, b);
try {
printList(reversed, nodeValue);
}
catch(char const* err) {
cout << err << '\n';
}
freeMem(head);
delete[] list;
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 19/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
int size; 3 10 9 8
cin >> size; 8 9 10
int* list = new int[size]; 1 3
for(int i = 0; i < size; i++) {
cin >> list[i];
}
int a, b;
cin >> a >> b;
unordered_map<ListNode*, int> nodeValue;
ListNode* head = init(list, size, nodeValue);
ListNode* reversed = reverse(head, a, b);
try {
printList(reversed, nodeValue);
}
catch(char const* err) {
cout << err << '\n';
}
freeMem(head);
delete[] list;
Reset answer
1 ▼ /*
2 ▼ struct ListNode {
3 int val;
4 ListNode *left;
5 ListNode *right;
6 ListNode(int x = 0, ListNode *l = nullptr, ListNode* r = nullptr) : val(x), left(l), right(r) {}
7 };
8 */
9
10 ▼ ListNode* reverse(ListNode* head, int a, int b) {
11 ListNode* current = head;
12 ListNode* tail = head;
13 for(int i=1;i<a;i++) current = current->right;
14 for(int i=1;i<b;i++) tail = tail->right;
15 ▼ for(int i =0;i<b-a;i+=2){
16 ListNode *lcurrent = current->left;
17 ListNode *rcurrent = current->right;
18 ListNode *ltail = tail->left;
19 ListNode *rtail = tail->right;
20 ▼ if(current->right == tail){
21 tail->right = current;
22 current->left = tail;
23 ▼ } else{
24 tail->right = rcurrent;
25 rcurrent->left = tail;
26 ltail->right = current;
27 current->left=ltail;
28 }
29 ▼ if(lcurrent != nullptr){
30 lcurrent->right = tail;
31 tail->left = lcurrent;
32 } else head =tail, tail->left=nullptr;
33 ▼ if(rtail !=nullptr){
34 current->right = rtail;
35 rtail->left=current;
36 } else current->right = nullptr;
37 ListNode *a = current;
38 current = tail;
39 tail =a;
40 current = current->right;
41 tail = tail->left;
42 }
43 return head;
44 }
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 20/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
int size; 5 3 6 5 4 7 3 6 5 4 7
cin >> size; 3 4 5 6 7
int* list = new int[size]; 2 4
for(int i = 0; i < size; i++) {
cin >> list[i];
}
int a, b;
cin >> a >> b;
unordered_map<ListNode*, int> nodeValue;
ListNode* head = init(list, size, nodeValue);
ListNode* reversed = reverse(head, a, b);
try {
printList(reversed, nodeValue);
}
catch(char const* err) {
cout << err << '\n';
}
freeMem(head);
delete[] list;
int size; 3 10 9 8 10 9 8
cin >> size; 8 9 10
int* list = new int[size]; 1 3
for(int i = 0; i < size; i++) {
cin >> list[i];
}
int a, b;
cin >> a >> b;
unordered_map<ListNode*, int> nodeValue;
ListNode* head = init(list, size, nodeValue);
ListNode* reversed = reverse(head, a, b);
try {
printList(reversed, nodeValue);
}
catch(char const* err) {
cout << err << '\n';
}
freeMem(head);
delete[] list;
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
WEBSITE
HCMUT
MyBK
BKSI
LIÊN HỆ
268 Lý Thường Kiệt, P.14, Q.10, TP.HCM
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 21/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])
lOMoARcPSD|19774343
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1431309&cmid=186808 22/22
Downloaded by TH?NH LÊ L?C QU?C ([email protected])