Doubly Linked List - Attempt Review
Doubly Linked List - Attempt Review
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 1/22
22:39 22/10/2023 Doubly Linked List: Attempt review
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
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 2/22
22:39 22/10/2023 Doubly Linked List: Attempt review
Test Result
Reset answer
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 3/22
22:39 22/10/2023 Doubly Linked List: Attempt review
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=1435990&cmid=188423 4/22
22:39 22/10/2023 Doubly Linked List: Attempt review
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:
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 5/22
22:39 22/10/2023 Doubly Linked List: Attempt review
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 /* Give the data of the element at given index in the list. */
5 ▼ if (index < 0 || index >= count) {
6 // Invalid index, return a default value or throw an exception as appr
7 // For simplicity, we'll return a default-constructed T (e.g., 0 for i
8 return T();
9 }
10
11 Node* current = head;
12 int currentIndex = 0;
13
14 // Traverse the list to find the node at the specified index.
15 ▼ while (current != nullptr && currentIndex < index) {
16 current = current->next;
17 currentIndex++;
18 }
19
20 // Return the data of the found node.
21 ▼ if (current != nullptr) {
22 return current->data;
23 }
24
25 // Default return value (e.g., 0 for integers) if the node is not found.
26 return T();
27 }
28
29 template <class T>
30 ▼ void DLinkedList<T>::set(int index, const T& e) {
31 /* Assign new value for element at given index in the list */
32
33 ▼ if (index < 0 || index >= count) {
34 // Invalid index, do nothing.
35 return;
36 }
37
38 Node* current = head;
39 int currentIndex = 0;
40
41
42 ▼ while (current != nullptr && currentIndex < index) {
43 current = current->next;
44 currentIndex++;
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 6/22
22:39 22/10/2023 Doubly Linked List: Attempt review
45 }
46
47
48 ▼ if (current != nullptr) {
49 current->data = e;
50 }
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=1435990&cmid=188423 7/22
22:39 22/10/2023 Doubly Linked List: Attempt review
Câu hỏi 3
Chính xác
Note: Iterator is a concept of repetitive elements on sequence structures. Iterator is implemented in class vector, list in STL container in C++
(https://www.geeksforgeeks.org/iterators-c-stl/). Your task is to implement the simple same class with iterator in C++ STL container.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 8/22
22:39 22/10/2023 Doubly Linked List: Attempt review
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;
}
};
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 9/22
22:39 22/10/2023 Doubly Linked List: Attempt review
class Iterator
{
private:
DLinkedList<T> *pList;
Node *current;
int index; // is the index of current in pList
public:
Iterator(DLinkedList<T> *pList, bool begin);
Iterator &operator=(const Iterator &iterator);
void set(const T &e);
T &operator*();
bool operator!=(const Iterator &iterator);
void remove();
// 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();
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 10/22
22:39 22/10/2023 Doubly Linked List: Attempt review
Answer: (penalty regime: 0, 0, 0, 5, 10 %)
Reset answer
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=1435990&cmid=188423 12/22
22:39 22/10/2023 Doubly Linked List: Attempt review
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:
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 13/22
22:39 22/10/2023 Doubly Linked List: Attempt review
Test Result
Reset answer
1
2 template <class T>
3 T DLinkedList<T>::removeAt(int index)
4 ▼ {
5 /* Remove element at index and return removed value */
6 ▼ if (index < 0 || index >= count) {
7 throw std::out_of_range("Index out of range");
8 }
9
10 Node* p = head;
11 T value = p->data;
12 ▼ if (index == 0) {
13 head = p->next;
14 if (head == nullptr) tail = nullptr;
15 delete p;
16 }
17 ▼ else {
18 for (int i = 0; i < index - 1; i++) p = p->next;
19 Node* q = p->next;
20 value = q->data;
21 p->next = q->next;
22 if (q == tail) tail = p;
23 delete q;
24 }
25
26 count--;
27 return value;
28 }
29
30 template <class T>
31 bool DLinkedList<T>::removeItem(const T& item)
32 ▼ {
33 /* Remove the first apperance of item in list and return true, otherwise r
34 Node* p = head;
35 Node* q = nullptr;
36
37 ▼ while (p != nullptr) {
38 ▼ if (p->data == item) {
39 if (q == nullptr) head = p->next;
40 else q->next = p->next;
41
42 if (p == tail) tail = q;
43
44 delete p;
45 count--;
46 return true;
47 }
48 q = p;
49 p = p->next;
50 }
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 14/22
22:39 22/10/2023 Doubly Linked List: Attempt review
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=1435990&cmid=188423 15/22
22:39 22/10/2023 Doubly Linked List: Attempt review
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=1435990&cmid=188423 16/22
22:39 22/10/2023 Doubly Linked List: Attempt review
For example:
Test Result
Reset answer
1 DataLog::DataLog()
2 ▼ {
3 logList.push_back(0); // Add the first state with 0
4 currentState = logList.begin();
5 }
6
7 DataLog::DataLog(const int &data)
8 ▼ {
9 logList.push_back(data); // Add the first state with data
10 currentState = logList.begin();
11 }
12
13 void DataLog::addCurrentState(int number)
14 ▼ {
15 *currentState += number; // Increase the value of current state by number
16 }
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 17/22
22:39 22/10/2023 Doubly Linked List: Attempt review
17
18 void DataLog::subtractCurrentState(int number)
19 ▼ {
20 *currentState -= number; // Decrease the value of current state by number
21 }
22
23 void DataLog::save()
24 ▼ {
25 auto temp = currentState;
26 temp++;
27 while (temp != logList.end())
28 ▼ {
29 temp = logList.erase(temp); // Delete all states after the current sta
30 }
31 int currentData = *currentState;
32 logList.push_back(currentData); // Create a new state with the same data
33 currentState = --logList.end(); // Move the currentState Iterator to this
34 }
35
36 void DataLog::undo()
37 ▼ {
38 if (currentState != logList.begin())
39 ▼ {
40 currentState--; // Switch to the previous state of the data
41 }
42 }
43
44 void DataLog::redo()
45 ▼ {
46 auto last = logList.end();
47 last--;
48 if (currentState != last)
49 ▼ {
50 currentState++; // Switch to the latter state of the data
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=1435990&cmid=188423 18/22
22:39 22/10/2023 Doubly Linked List: Attempt review
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=1435990&cmid=188423 19/22
22:39 22/10/2023 Doubly Linked List: Attempt review
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
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 20/22
22:39 22/10/2023 Doubly Linked List: Attempt review
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
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 21/22
22:39 22/10/2023 Doubly Linked List: Attempt review
LIÊN HỆ
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 22/22