Lab 2 - Preparation - Attempt Review
Lab 2 - Preparation - Attempt Review
Dashboard / Courses / Học kỳ I năm học 2021-2022 (Semester 1 - Academic year 2021-2022)
/ Khoa Khoa học và Kỹ thuật Máy tính (Faculty of Computer Science and Engineering ) / Khoa Học Máy Tính
/ Cấu trúc dữ liệu và giải thuật (thực hành) (CO2004)_Trần Khánh Tùng (DH_HK211) / Lab 2: Doubly Linked List + Stack + Queue + Sorting
/ Lab 2: Preparation
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 1/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Question 1
Correct
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
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 2/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Test Result
Reset answer
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 3/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Correct
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 4/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Question 2
Correct
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
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 5/19
2/16/22, 7:47 AM Lab 2: Preparation: 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 if (count == 0) return -1;
4 if (index == this->count - 1) return tail->data;
5 if (index == 0) return head->data;
6 int idx = 0;
7 for (Node* h = head; h != NULL; h = h->next, ++idx)
8▼ {
9 if (idx == index) return h->data;
10 }
11 return -1;
12 /* Give the data of the element at given index in the list. */
13 }
14 template <class T>
15 ▼ void DLinkedList<T>::set(int index, const T& e) {
16 if (count == 0) return;
17 if (index == 0)
18 ▼ {
19 head->data = e;
20 return;
21 }
22 if (index == this->count - 1)
23 ▼ {
24 tail->data = e;
25 return;
26 }
27 int idx = 0;
28 for (Node* h = head; h != NULL; h = h->next, ++idx)
29 ▼ {
30 if (idx == index)
31 ▼ {
32 h->data = e;
33 return;
34 }
35 }
36 /* Assign new value for element at given index in the list */
37 }
38
39 template<class T>
40 ▼ bool DLinkedList<T>::empty() {
41 /* Check if the list is empty or not. */
42 if (count ==0) return true;
43 return false;
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 6/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
43 return false;
44 }
45
46 template<class T>
47 ▼ int DLinkedList<T>::indexOf(const T& item) {
48 int idx = 0;
49 if (count == 0) return -1;
50 /* Return the first index wheter item appears in list, otherwise retu
51 for (Node* h = head; h != NULL; h = h->next, ++idx)
52 ▼ {
53 if (h->data == item) return idx;
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) << " |";
}
Correct
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 7/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Question 3
Correct
Implement all methods in class Stack with template type T. The description of each method is written as comment in frame code.
#ifndef STACK_H
#define STACK_H
#include "DLinkedList.h"
template<class T>
class Stack {
protected:
DLinkedList<T> list;
public:
Stack() {}
void push(T item) ;
T pop() ;
T top() ;
bool empty() ;
int size() ;
void clear() ;
};
#endif
You can use all methods in class DLinkedList without implementing them again. The description of class DLinkedList is written as comment
in frame code.
For example:
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 8/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Test Result
Stack<int> stack; 1 0
cout << stack.empty() << " " << stack.size();
Stack<int> stack; 8
assert(stack.top() == 12);
stack.pop();
stack.pop();
Reset answer
1
2 ▼ void push(T item) {
3 // TODO: Push new element into the top of the stack
4 list.add(0,item);
5 }
6
7 ▼ T pop() {
8 // TODO: Remove an element on top of the stack
9 T temp = list.removeAt(0);
10 return temp;
11 }
12
13 ▼ T top() {
14 // TODO: Get value of the element on top of the stack
15 return list.get(0);
16 }
17
18 ▼ bool empty() {
19 // TODO: Determine if the stack is empty
20 if (list.size() == 0) return true;
21 return false;
22 }
23
24 ▼ int size() {
25 // TODO: Get the size of the stack
26 return list.size();
27 }
28
29 ▼ void clear() {
30 // TODO: Clear all elements of the stack
31 list.clear();
32 }
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 9/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Stack<int> stack; 1 0 1 0
cout << stack.empty() << " " << stack.size();
Stack<int> stack; 8 8
assert(stack.top() == 12);
stack.pop();
stack.pop();
Correct
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 10/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Question 4
Correct
Implement all methods in class Queue with template type T. The description of each method is written as comment in frame code.
#ifndef QUEUE_H
#define QUEUE_H
#include "DLinkedList.h"
template<class T>
class Queue {
protected:
DLinkedList<T> list;
public:
Queue() {}
void push(T item) ;
T pop() ;
T top() ;
bool empty() ;
int size() ;
void clear() ;
};
#endif /* QUEUE_H */
You can use all methods in class DLinkedList without implementing them again. The description of class DLinkedList is written as comment
in frame code.
For example:
Test Result
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 11/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Test Result
Queue<int> queue;
assert(queue.empty());
assert(queue.size() == 0);
Reset answer
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 12/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Test
Correct
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 13/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Question 5
Incorrect
Implement static methods Partition and QuickSort in class Sorting to sort an array in ascending order.
#ifndef SORTING_H
#define SORTING_H
#include <sstream>
#include <iostream>
#include <type_traits>
using namespace std;
template <class T>
class Sorting {
private:
static T* Partition(T* start, T* end) ;
public:
static void QuickSort(T* start, T* end) ;
};
#endif /* SORTING_H */
You can read the pseudocode of the algorithm used to in method Partition in the below image.
For example:
Test Result
int array[] = { 3, 5, 7, 10 ,12, 14, 15, 13, 1, 2, 9, 6, 4, 8, 11, 16, Index of pivots: 2 0 0 6 1 0 2 1 0 0 2 1 0 0 0 0 0 0 1 0
17, 18, 20, 19 }; Array after sorting: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
cout << "Index of pivots: "; 16 17 18 19 20
Sorting<int>::QuickSort(&array[0], &array[20]);
cout << "\n";
cout << "Array after sorting: ";
for (int i : array) cout << i << " ";
Reset answer
1
2 ▼ static T* Partition(T* start, T* end) {
3 // TODO: return the pointer which points to the pivot after rearrange t
4 T pivot = *end; // pivot
5 //T i = (low - 1); // Index of smaller element and indicates the right
6
7 while (start ! NULL)
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 14/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
7 while (start != NULL)
8▼ {
9 // If current element is smaller than the pivot
10 if (*start < pivot)
11 ▼ {
12 start = start + 1; // increment index of smaller element
13 swap(*start, *end);
14 }
15 }
16 swap(*(start+1), *end);
17 return (start+1);
18 }
19 ▼ static void QuickSort(T* start, T* end) {
20 if (*start < *end)
21 ▼ {
22 ▼ /* pi is partitioning index, arr[p] is now
23 at right place */
24 T* pi = Partition(start, end);
25
26 // Separately sort elements before
27 // partition and after partition
28 QuickSort(start, pi - 1);
29 QuickSort(pi + 1, end);
30 }
31 // TODO
32 // In this question, you must print out the index of pivot in subarray
33
34 }
Show differences
Incorrect
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 15/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Question 6
Partially correct
Implement method bubbleSort() in class SLinkedList to sort this list in ascending order. After each bubble, we will print out a list to check
(using printList).
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 16/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
#include <iostream>
#include <sstream>
using namespace std;
if (this->count == 0)
{
this->head = this->tail = pNew;
}
else
{
this->tail->next = pNew;
this->tail = pNew;
}
this->count++;
}
int size()
{
return this->count;
}
void printList()
{
stringstream ss;
ss << "[";
Node *ptr = head;
while (ptr != tail)
{
ss << ptr->data << ",";
ptr = ptr->next;
}
if (count > 0)
ss << ptr->data << "]";
else
ss << "]";
cout << ss.str() << endl;
}
public:
class Node {
private:
T data;
Node* next;
friend class SLinkedList<T>;
public:
Node() {
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 17/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
next = 0;
}
Node(T data) {
this->data = data;
this->next = nullptr;
}
};
void bubbleSort();
};
For example:
Test Result
Reset answer
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 18/19
2/16/22, 7:47 AM Lab 2: Preparation: Attempt review
Partially correct
Marks for this submission: 0.60/1.00. Accounting for previous tries, this gives 0.70/1.00.
Jump to...
e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 19/19