0% found this document useful (0 votes)
107 views22 pages

Doubly Linked List - Attempt Review

The get method returns the data at a given index by traversing the list and returning the data of the node at that index. The set method updates the data at a given index by traversing to that node and setting its data. The empty method checks if the head and tail are null. The indexOf method traverses to find the index of a given item, and contains checks if the item exists in the list. Exceptions are thrown for invalid indexes.

Uploaded by

Jo Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views22 pages

Doubly Linked List - Attempt Review

The get method returns the data at a given index by traversing the list and returning the data of the node at that index. The set method updates the data at a given index by traversing to that node and setting its data. The empty method checks if the head and tail are null. The indexOf method traverses to find the index of a given item, and contains checks if the item exists in the list. Exceptions are thrown for invalid indexes.

Uploaded by

Jo Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

22:39 22/10/2023 Doubly Linked List: Attempt review

Đã bắt đầu vào Thứ hai, 9 Tháng mười 2023, 8:09 AM


lúc
Tình trạng Đã hoàn thành
Hoàn thành vào Chủ nhật, 22 Tháng mười 2023, 10:39 PM
lúc
Thời gian thực 13 ngày 14 giờ
hiện
Điểm 6,00/6,00
Điểm 10,00 của 10,00 (100%)

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

Điểm 1,00 của 1,00

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.

template <class T>


class DLinkedList {
public:
class Node; // Forward declaration
protected:
Node* head;
Node* tail;
int count;
public:
DLinkedList();
~DLinkedList();
void add(const T &e);
void add(int index, const T &e);
int size();
public:
class Node
{
private:
T data;
Node *next;
Node *previous;
friend class DLinkedList<T>;

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);
}
cout << list.toString();

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

DLinkedList<int> list; [9,8,7,6,5,4,3,2,1,0]


int size = 10;
for(int idx=0; idx < size; idx++){
list.add(0, idx);
}
cout << list.toString();

Answer: (penalty regime: 0, 0, 0, 5, 10 %)

Reset answer

1 template <class T>


2 ▼ void DLinkedList<T>::add(const T& e) {
3 /* Insert an element into the end of the list. */
4 Node* newNode = new Node(e);
5
6 ▼ if (head == nullptr) {
7 // List is empty, make newNode both head and tail
8 head = newNode;
9 tail = newNode;
10 ▼ } else {
11 // Append newNode to the end of the list
12 tail->next = newNode;
13 newNode->previous = tail;
14 tail = newNode;
15 }
16
17 count++;
18 }
19
20 template<class T>
21
22 ▼ void DLinkedList<T>::add(int index, const T& e) {
23 ▼ if (index < 0 || index > count) {
24 // Handle invalid index (out of bounds)
25 return;
26 }
27
28 Node* newNode = new Node(e);
29 ▼ if (index == 0) {
30 // Inserting at the beginning
31 newNode->next = head;
32 ▼ if (head != nullptr) {
33 head->previous = newNode;
34 }
35 head = newNode;
36 ▼ if (count == 0) {
37 // If the list was empty, update the tail
38 tail = newNode;
39 }
40 ▼ } else if (index == count) {
41 // Inserting at the end
42 tail->next = newNode;
43 newNode->previous = tail;
44 tail = newNode;
45 ▼ } else {
46 // Inserting at some intermediate position
47 Node* current = head;
48 ▼ for (int i = 0; i < index; i++) {
49 current = current->next;
50 }

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

Test Expected Got

 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);
}
cout << list.toString();

 DLinkedList<int> list; [9,8,7,6,5,4,3,2,1,0] [9,8,7,6,5,4,3,2,1,0] 


int size = 10;
for(int idx=0; idx < size; idx++){
list.add(0, idx);
}
cout << list.toString();

Passed all tests! 

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

Điểm 1,00 của 1,00

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.

template <class T>


class DLinkedList {
public:
class Node; // Forward declaration
protected:
Node* head;
Node* tail;
int count;
public:
DLinkedList();
~DLinkedList();
void add(const T &e);
void add(int index, const T &e);
int size();
bool empty();
T get(int index);
void set(int index, const T &e);
int indexOf(const T &item);
bool contains(const T &item);
public:
class Node
{
private:
T data;
Node *next;
Node *previous;
friend class DLinkedList<T>;

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) << " |";
}

DLinkedList<int> list; [2,5,6,3,67,332,43,1,0,9]


int size = 10;
int value[] = {2,5,6,3,67,332,43,1,0,9};
for(int idx=0; idx < size; idx++){
list.add(idx);
}
for(int idx=0; idx < size; idx++){
list.set(idx, value[idx]);
}
cout << list.toString();

Answer: (penalty regime: 0, 0, 0, 5, 10 %)

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 }

Test Expected Got

 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) << " |";
}

 DLinkedList<int> list; [2,5,6,3,67,332,43,1,0,9] [2,5,6,3,67,332,43,1,0,9] 


int size = 10;
int value[] = {2,5,6,3,67,332,43,1,0,9};
for(int idx=0; idx < size; idx++){
list.add(idx);
}
for(int idx=0; idx < size; idx++){
list.set(idx, value[idx]);
}
cout << list.toString();

Passed all tests! 

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

Điểm 1,00 của 1,00

Implement Iterator class in class DLinkedList.

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

template <class T>


class DLinkedList
{
public:
class Iterator; //forward declaration
class Node; //forward declaration
protected:
Node *head;
Node *tail;
int count;
public:
DLinkedList() : head(NULL), tail(NULL), count(0){};
~DLinkedList();
void add(const T &e);
void add(int index, const T &e);
T removeAt(int index);
bool removeItem(const T &item);
bool empty();
int size();
void clear();
T get(int index);
void set(int index, const T &e);
int indexOf(const T &item);
bool contains(const T &item);
string toString();
Iterator begin()
{
return Iterator(this, true);
}
Iterator end()
{
return Iterator(this, false);
}
public:
class Node
{
private:
T data;
Node *next;
Node *previous;
friend class DLinkedList<T>;

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);
};
};

Please read example carefully to see how we use the iterator.

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

1 template <class T>


2 DLinkedList<T>::Iterator::Iterator(DLinkedList<T> *pList, bool begin)
3 ▼ {
4 this->pList = pList;
5 if (begin == true)
6 ▼ {
7 if (this->pList == NULL)
8 ▼ {
9 this->current = NULL;
10 this->index = -1;
11 }
12 else
13 ▼ {
14 this->current = this->pList->head;
15 this->index = 0;
16 }
17 }
18 else
19 ▼ {
20 this->current = NULL;
21 this->index = (this->pList->count == 0) ? (this->pList->size()) : 0;
22 }
23 }
24
25 template <class T>
26 typename DLinkedList<T>::Iterator& DLinkedList<T>::Iterator::operator=(const D
27 ▼ {
28 this->pList = iterator.pList;
29 this->current = iterator.current;
30 this->index = iterator.index;
31 return *this;
32 }
33
34 template <class T>
35 void DLinkedList<T>::Iterator::set(const T &e)
36 ▼ {
37 if (this->current == NULL)
38 throw std::out_of_range("Segmentation fault!");
39 else
40 this->pList->set(this->index, e);
41 }
42
43 template<class T>
44 T& DLinkedList<T>::Iterator::operator*()
45 ▼ {
46 if (this->current == NULL)
47 throw std::out_of_range("Segmentation fault!");
48 return this->current->data;
49 }
50
51 template<class T>
52 void DLinkedList<T>::Iterator::remove()
53 ▼ {
54 ▼ /*
55 * TODO: delete Node in pList which Node* current point to.
56 * After that, Node* current point to the node before the node just d
57 * If we remove first node of pList, Node* current point to nullptr.
58 * Then we use operator ++, Node* current will point to the head of p
59 */
60 if (this->current == NULL)
61 throw std::out_of_range("Segmentation fault!");
62 else if (this->current == this->pList->head)
63 ▼ {
64 this->current = NULL;
65 this->pList->removeAt(this->index);
66 this->index = -1;
67 }
68 else
69 ▼ {
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 11/22
22:39 22/10/2023 Doubly Linked List: Attempt review
70 this->current = this->current->previous;
71 this->pList->removeAt(this->index);
72 --this->index;
73 }
74 }
75

Test Expected Got

 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();

Passed all tests! 

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

Điểm 1,00 của 1,00

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.

template <class T>


class DLinkedList {
public:
class Node; // Forward declaration
protected:
Node* head;
Node* tail;
int count;
public:
DLinkedList();
~DLinkedList();
void add(const T &e);
void add(int index, const T &e);
int size();
bool empty();
T get(int index);
void set(int index, const T &e);
int indexOf(const T &item);
bool contains(const T &item);
T removeAt(int index);
bool removeItem(const T &item);
void clear();
public:
class Node
{
private:
T data;
Node *next;
Node *previous;
friend class DLinkedList<T>;

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

DLinkedList<int> list; [5,6,3,67,332,43,1,0,9]


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

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


list.add(value[idx]);
}
list.removeAt(0);
cout << list.toString();

Answer: (penalty regime: 0 %)

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

Test Expected Got

 DLinkedList<int> list; [5,6,3,67,332,43,1,0,9] [5,6,3,67,332,43,1,0,9] 


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

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


list.add(value[idx]);
}
list.removeAt(0);
cout << list.toString();

Passed all tests! 

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

Điểm 1,00 của 1,00

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.

We have include <iostream> <list> and using namespace std;

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

DataLog log(10); [ 10 ] => Current state: [ 25 ] => [ 40 ] => END_LOG


log.save();
log.addCurrentState(15);
log.save();
log.addCurrentState(15);
log.undo();
log.printLog();

DataLog log(10); [ 10 ] => [ 25 ] => [ 40 ] => Current state: [ 35 ] => END_LOG


log.save();
log.addCurrentState(15);
log.save();
log.addCurrentState(15);
log.save();
log.subtractCurrentState(5);
log.printLog();

Answer: (penalty regime: 0, 0, 0, 5, 10 %)

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

Test Expected Got

 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();

Passed all tests! 

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

Điểm 1,00 của 1,00

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:

Test Input Result

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

Test Input Result

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;

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ ListNode* reverse(ListNode* head, int a, int b) {


2 if (a == b) return head;
3
4 ListNode* dummy = new ListNode(0);
5 dummy->right = head;
6 ListNode* pre = dummy;
7 ▼ for (int i = 0; i < a - 1; i++) {
8 pre = pre->right;
9 }
10
11 ListNode* start = pre->right;
12 ListNode* then = start->right;
13
14 ▼ for (int i = 0; i < b - a; i++) {
15 start->right = then->right;
16 if(then->right)
17 then->right->left = start;
18 then->right = pre->right;
19 pre->right->left = then;
20 pre->right = then;
21 then->left = pre;
22 if(start->right)
23 then = start->right;
24 }
25 return dummy->right;
26 }
27

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

Test Input Expected Got

 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;

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

BÁCH KHOA E-LEARNING

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Ệ

 268 Lý Thường Kiệt, P.14, Q.10, TP.HCM

 (028) 38 651 670 - (028) 38 647 256 (Ext: 5258, 5234)

[email protected]

Copyright 2007-2022 BKEL - Phát triển dựa trên Moodle

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1435990&cmid=188423 22/22

You might also like