0% found this document useful (0 votes)
18 views19 pages

Lab 2 - Preparation - Attempt Review

The document details the preparation and review of Lab 2 for a computer science course, focusing on implementing data structures such as doubly linked lists and stacks using template classes in C++. It includes specific coding tasks, tests, and results for methods like add, size, get, set, and others with example outputs. The student completed the lab with a score of 4.70 out of 6.00, achieving a grade of 7.83 out of 10.00.

Uploaded by

nguyenduydang225
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)
18 views19 pages

Lab 2 - Preparation - Attempt Review

The document details the preparation and review of Lab 2 for a computer science course, focusing on implementing data structures such as doubly linked lists and stacks using template classes in C++. It includes specific coding tasks, tests, and results for methods like add, size, get, set, and others with example outputs. The student completed the lab with a score of 4.70 out of 6.00, achieving a grade of 7.83 out of 10.00.

Uploaded by

nguyenduydang225
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/ 19

2/16/22, 7:47 AM Lab 2: Preparation: Attempt review

Dashboard / Courses / Học kỳ I năm học 2021-2022 (Semester 1 - Academic year 2021-2022)

/ Đại Học Chính Qui (Bacherlor program (Full-time study))

/ 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

Started on Saturday, 25 September 2021, 4:10 PM


State Finished
Completed on Sunday, 3 October 2021, 11:28 AM
Time taken 7 days 19 hours
Marks 4.70/6.00
Grade 7.83 out of 10.00 (78%)

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

Mark 1.00 out of 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();

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

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 if (count == 0)
5 ▼ {
6 Node* newNode = new Node(e);
7 head = newNode;
8 tail = newNode;
9 tail->next = NULL;
10 ++(this->count);
11 return;
12 }
13 Node* newNode = new Node(e);
14 tail->next = newNode;
15 newNode->previous = tail;
16 newNode->next = NULL;
17 tail = newNode;
18 ++(this->count);
19 return;
20 }
21
22 template<class T>
23 ▼ void DLinkedList<T>::add(int index, const T& e) {
24 /* Insert an element into the list at given index. */
25 if (count == 0) {add(e);return;}
26 if (index == 0)
27 ▼ {
28 Node* newNode = new Node(e);
29 newNode->next = head;
30 head->previous = newNode;
31 head = newNode;
32 ++(this->count);
33 return;
34 }
35 if (index == this->count) {add(e); return;}
36 int idx = 0;
37 Node* front = head;
38 Node* back = NULL;
39 for (;front != NULL; back = front, front = front->next, ++idx)
40 ▼ {
41 if (idx == index)
42 ▼ {
43 Node* newNode = new Node (e);
44 ++(this->count);
45 back->next = newNode;
46 newNode->next = front;
47 front->previous = newNode;
48 return;
49 }
50 }
51
52 }
53 template<class T>

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

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

Correct

Marks for this submission: 1.00/1.00.

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

Mark 1.00 out of 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:

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

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

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

Correct

Marks for this submission: 1.00/1.00.

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

Mark 1.00 out of 1.00

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.

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);
T removeAt(int index);
bool removeItem(const T& removeItem);
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);
};

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

int item[] = { 3, 1, 4, 5, 2, 8, 10, 12 };


for (int idx = 0; idx < 8; idx++) stack.push(item[idx]);

assert(stack.top() == 12);

stack.pop();
stack.pop();

cout << stack.top();

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

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

Test Expected Got

 Stack<int> stack; 1 0 1 0 
cout << stack.empty() << " " << stack.size();

 Stack<int> stack; 8 8 

int item[] = { 3, 1, 4, 5, 2, 8, 10, 12 };


for (int idx = 0; idx < 8; idx++) stack.push(item[idx]);

assert(stack.top() == 12);

stack.pop();
stack.pop();

cout << stack.top();

Passed all tests! 

Correct

Marks for this submission: 1.00/1.00.

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

Mark 1.00 out of 1.00

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.

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);
T removeAt(int index);
bool removeItem(const T& removeItem);
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);
};

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

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

Reset answer

1 ▼ void push(T item) {


2 // TODO: Push new element into the end of the queue
3 list.add(item);
4 }
5
6 ▼ T pop() {
7 // TODO: Remove an element in the head of the queue
8 T temp = list.removeAt(0);
9 return temp;
10 }
11
12 ▼ T top() {
13 // TODO: Get value of the element in the head of the queue
14 return list.get(0);
15 }
16
17 ▼ bool empty() {
18 // TODO: Determine if the queue is empty
19 if (list.size() == 0 ) return true;
20 return false;
21
22 }
23
24 ▼ int size() {
25 // TODO: Get the size of the queue
26 return list.size();
27 }
28
29 ▼ void clear() {
30 // TODO: Clear all elements of the queue
31 list.clear();
32 }

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

Passed all tests! 

Correct

Marks for this submission: 1.00/1.00.

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

Mark 0.00 out of 1.00

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

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

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 }

Test Expected Got

Testing was aborted due to error.

Show differences

Incorrect

Marks for this submission: 0.00/1.00.

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

Mark 0.70 out of 1.00

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;

template <class T>


class SLinkedList {
public:
class Node; // Forward declaration
protected:
Node* head;
Node* tail;
int count;
public:
SLinkedList()
{
this->head = nullptr;
this->tail = nullptr;
this->count = 0;
}
~SLinkedList(){};
void add(T e)
{
Node *pNew = new Node(e);

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

int arr[] = {9, 2, 8, 4, 1}; [2,8,4,1,9]


SLinkedList<int> list; [2,4,1,8,9]
for(int i = 0; i <int(sizeof(arr))/4;i++) [2,1,4,8,9]
list.add(arr[i]); [1,2,4,8,9]
list.bubbleSort();

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

Reset answer

1 template <class T>


2 void SLinkedList<T>::bubbleSort()
3 ▼ {
4 if (count <= 1) {printList(); return;}
5 bool cmp = true;
6 for (int i = 0; i < count; ++i)
7 ▼ {
8 cmp = false;
9 Node* curr = head->next;
10 Node* prev = head;
11 for (; curr!= NULL; prev = curr, curr = curr->next)
12 ▼ {
13 if (prev->data > curr->data)
14 ▼ {
15 T temp = prev->data;
16 prev->data = curr->data;
17 curr->data = temp;
18 cmp = true;
19 }
20 }
21 if (cmp == false && i != 0) break;
22 else printList();
23 }
24 }

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

Test Expected Got

 int arr[] = {9, 2, 8, 4, 1}; [2,8,4,1,9] [2,8,4,1,9] 


SLinkedList<int> list; [2,4,1,8,9] [2,4,1,8,9]
for(int i Copyright 2007-2021 Ho Chi Minh
= 0; i <int(sizeof(arr))/4;i++) City University
[2,1,4,8,9] of
[2,1,4,8,9] Technology. All Rights Reserved.
Address: Hochiminh City University Of Technology
list.add(arr[i]); [1,2,4,8,9] - 268 Ly Thuong Kiet Street, District 10, Ho Chi Minh City.
[1,2,4,8,9]
list.bubbleSort(); Email: [email protected]
Powered by Moodle
Your code failed one or more hidden tests.

Partially correct

Marks for this submission: 0.60/1.00. Accounting for previous tries, this gives 0.70/1.00.

◄ Lab 1: Singly Linked List

Jump to...

Lab 2: Doubly LL, Stack and Queue ►

e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=3813403&cmid=664816 19/19

You might also like