Doubly Linked List - LAB2
Doubly Linked List - LAB2
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 1/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
Câu hỏi 1
Đúng
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
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 2/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
Answer: (penalty regime: 0, 0, 0, 5, 10 %)
Reset answer
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 3/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 4/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
Câu hỏi 2
Đúng
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:
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 5/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
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) << " |";
}
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<0 || index>count){
5 throw std::out_of_range("Index is out of range!");
6 }
7
8 Node *current = head;
9▼ for(int i=-1; i<index-1; i++){
10 current = current->next;
11 }
12
13 return current->data;
14 }
15
16 template <class T>
17 ▼ void DLinkedList<T>::set(int index, const T& e) {
18 /* Assign new value for element at given index in the list */
19 ▼ if(index<0 || index>count){
20 throw std::out_of_range("Index is out of range!");
21 }
22
23 Node *current = head;
24 ▼ for(int i=-1; i<index-1; i++){
25 current = current->next;
26 }
27
28 current->data = e;
29 }
30
31 template<class T>
32 ▼ bool DLinkedList<T>::empty() {
33 /* Check if the list is empty or not. */
34 return count==0;
35 }
36
37 template<class T>
38 ▼ int DLinkedList<T>::indexOf(const T& item) {
39 /* Return the first index wheter item appears in list, otherwise return -1 */
40 Node *current = head;
41 ▼ for(int i=0; i<count; i++){
42 ▼ if(current->data == item){
43 return i;
44 }
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 6/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
44 }
45
46 current = current->next;
47 }
48
49 return -1;
50 }
51
52 template<class T>
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) << " |";
}
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 7/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
Câu hỏi 3
Đúng
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:
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 8/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
Test Result
Reset answer
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 9/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 10/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
Câu hỏi 4
Đúng
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://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 11/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
For example:
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();
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 12/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
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 *currentState += number;
25 }
26
27 void DataLog::subtractCurrentState(int number)
28 ▼ {
29 ▼ /*
30 * TODO: Decrease the value of current state by number
31 */
32 *currentState -= number;
33 }
34
35 void DataLog::save()
36 ▼ {
37 ▼ /*
38 * TODO: This function will create a new state, copy the data of the currentState
39 * and move the currentState Iterator to this new state. If there are other states behind the
40 * currentState Iterator, we delete them all before creating a new state.
41 */
42
43 auto it = currentState;
44 ++it;
45 logList.erase(it, logList.end());
46
47 logList.push_back(*currentState);
48 currentState = --logList.end();
49 }
50
51 void DataLog::undo()
52 ▼ {
DataLog log(10); [ 10 ] => Current state: [ 25 ] => [ [ 10 ] => Current state: [ 25 ] => [
log.save(); 40 ] => END_LOG 40 ] => 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();
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 13/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
Câu hỏi 5
Đúng
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://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 14/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
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 //To Do
12 if(!head || a==b) return head;
13
14 ListNode* dummy = new ListNode(0);
15 dummy->right = head;
16 head->left = dummy;
17
18 ListNode* prev = dummy;
19 ▼ for(int i=1; i<a; ++i){
20 prev = prev->right;
21 }
22
23 ListNode* const start = prev->right;
24 ListNode* then = start->right;
25
26 ▼ for(int i=0; i<b-a; ++i){
27 start->right = then->right;
28 if(then->right) then->right->left = start;
29 then->right = prev->right;
30 prev->right->left = then;
31 prev->right = then;
32 then = start->right;
33 }
34
35 head = dummy->right;
36 head->left = nullptr;
37 delete dummy;
38 return head;
39 }
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 15/16
10/23/24, 3:01 PM Doubly Linked List: Xem lại lần làm thử | BK-LMS
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;
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4307063&cmid=442755 16/16