Lab 2 - at Home
Lab 2 - at Home
class Node
private:
T data;
Node *next;
Node *previous;
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
template<class T>
void DLinkedList<T>::add(int index, const T& e) {
/* Insert an element into the list at given index. */
if (index < 0 || index > count)
throw std::out_of_range("Out of range");
Node* newNode = new Node(e);
if (head == NULL){
head = newNode;
tail = newNode;
count++;
return;
}
Node* cur = head;
Node* pre = NULL;
for (int i = 0; i < index; i++){
pre = cur;
cur = cur->next;
}
if (pre == NULL){
newNode->next = cur;
cur->previous = newNode;
head = newNode;
}
else{
newNode->next = cur;
pre->next = newNode;
newNode->previous = pre;
if (cur == NULL)
tail = newNode;
else
cur->previous = newNode;
}
count++;
}
template<class T>
int DLinkedList<T>::size() {
/* Return the length (size) of list */
return this->count;
}
Test Expected Got
T get(int index);
public:
class Node
private:
T data;
Node *next;
Node *previous;
public:
Node()
this->previous = NULL;
this->next = NULL;
}
Node(const T &data)
this->data = data;
this->previous = NULL;
this->next = NULL;
};
};
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);
}
for(int idx=0; idx < size; idx++){
cout << list.get(idx) << " |";
}
template<class T>
T DLinkedList<T>::get(int index) {
/* Give the data of the element at given index in the list. */
Node* travel = this->head;
if(travel==NULL) return travel->data;
if(index==0) return travel->data;
int place=0;
while(travel->next!=NULL && place!=index)
{
place++;
travel=travel->next;
}
return travel->data;
}
template<class T>
bool DLinkedList<T>::empty() {
/* Check if the list is empty or not. */
Node* travel = this->head;
if(travel==NULL) return true;
return false;
template<class T>
int DLinkedList<T>::indexOf(const T& item) {
/* Return the first index wheter item appears in list, otherwise return -1 */
Node* travel = this->head;
if(travel==NULL) return -1;
if(travel->data==item) return 0;
int place=0;
while(travel!=NULL && travel->data!=item)
{
place++;
travel=travel->next;
}
if(travel==NULL) return -1;
return place;
}
template<class T>
bool DLinkedList<T>::contains(const T& item) {
/* Check if item appears in the list */
Node* travel = this->head;
if(travel==NULL) return false;
if(travel->data==item) return true;
while(travel!=NULL && travel->data!=item)
{
travel=travel->next;
}
if(travel!=NULL) return true;
return false;
}
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) << " |";
}
T removeAt(int index);
void clear();
public:
class Node
private:
T data;
Node *next;
Node *previous;
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
template<class T>
void DLinkedList<T>::clear(){
/* Remove all elements in list */
while (head != NULL) {
Node* temp = head;
head = head->next;
delete temp;
count--;
}
tail = NULL;
}
#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.
Test Result
Stack<int> stack; 1 0
cout << stack.empty() << " " << stack.size();
Stack<int> stack; 8
assert(stack.top() == 12);
stack.pop();
stack.pop();
return this->list.removeAt(size);
}
T top() {
// TODO: Get value of the element on top of the stack
int size = this->list.size()-1;
T solution = this->list.get(size);
return solution;
}
bool empty() {
// TODO: Determine if the stack is empty
return this->list.empty();
}
int size() {
// TODO: Get the size of the stack
int size = this->list.size();
return size;
}
void clear() {
// TODO: Clear all elements of the stack
this->list.clear();
}
Go
Expected
Test t
Stack<int> stack;
1 0 1 0
cout << stack.empty() << " " << stack.size();
Stack<int> stack;
stack.pop();
stack.pop();
#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
Queue<int> queue;
assert(queue.empty());
assert(queue.size() ==
0);
T pop() {
// TODO: Remove an element in the head of the queue
return this->list.removeAt(0);
}
T top() {
// TODO: Get value of the element in the head of the queue
return this->list.get(0);
}
bool empty() {
// TODO: Determine if the queue is empty
return this->list.empty();
}
int size() {
// TODO: Get the size of the queue
return this->list.size();
}
void clear() {
// TODO: Clear all elements of the queue
this->list.clear();
}
Test
#include <iostream>
#include <sstream>
class SLinkedList {
public:
protected:
Node* head;
Node* tail;
int count;
public:
SLinkedList()
this->head = nullptr;
this->tail = nullptr;
this->count = 0;
~SLinkedList(){};
void add(T e)
if (this->count == 0)
else
this->tail->next = pNew;
this->tail = pNew;
this->count++;
int size()
return this->count;
void printList()
stringstream ss;
ss << "[";
ptr = ptr->next;
if (count > 0)
else
ss << "]";
public:
class Node {
private:
T data;
Node* next;
public:
Node() {
next = 0;
Node(T data) {
this->data = data;
this->next = nullptr;
};
void bubbleSort();
};
For example:
Test Result
#include <iostream>
class Sorting
public:
}
static void selectionSort(T *start, T *end);
};
For example:
Test Result
Expected Got
Test
-2, 2, 8, 1, 0,
9
-2, 0, 8, 1, 2,
-2, 2, 8, 1, 0, 9
9
int arr[] = {9, 2, 8, 1, 0, -2}; -2, 0, 8, 1, 2, 9
-2, 0, 1, 8, 2,
Sorting<int>::selectionSort(&arr[0], -2, 0, 1, 8, 2, 9
9
&arr[6]); -2, 0, 1, 2, 8, 9
-2, 0, 1, 2, 8,
-2, 0, 1, 2, 8, 9
9
-2, 0, 1, 2, 8,
9
Passed all tests!