Lab 2: Doubly LL + Stack + Queue + Sorting
Lab 2: Doubly LL + Stack + Queue + Sorting
EX3:
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);
};
EX5:
You are keeping score for a basketball game with some new rules. The game consists of several
rounds, where the scores of past rounds may affect future rounds' scores.
At the beginning of the game, you start with an empty record. You are given a list of strings ops,
where ops[i] is the operation you must apply to the record, with the following rules:
For example:
Test Result
class _Stack{
public:
class Node;
private:
int count = 0;
public:
int pop();
T top();
bool isEmpty();
int size();
T dataOut = ptr->data;
return dataOut;
public:
class Node{
private:
T data;
Node* next;
public:
Node() {
next = 0;
Node(Node* next) {
this->next = next;
this->data = data;
this->next = next;
};
};
if (this->count == 0)
this->tail = pNew;
else
pNew->next = this->head;
this->head = pNew;
this->count++;
if (this->count == 0) return 0;
if (this->count == 1){
this->head = nullptr;
this->tail = nullptr;
this->head = dltPtr->next;
delete dltPtr;
this->count--;
return 1;
T _Stack<T>::top(){
if (this->count == 0) return 0;
T dataOut = this->head->data;
return dataOut;
bool _Stack<T>::isEmpty(){
int _Stack<T>::size(){
return this->count;
/*TODO*/
_Stack<int> record;
for (size_t i=0; i<ops.length(); i++){
record.push(value);
if (ops[i] == 'C')
record.pop();
if (ops[i] == 'D'){
record.push(value);
if (ops[i] == '+'){
record.push(value);
int sum = 0;
sum += record.top();
record.pop();
return sum;
#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:
Test Result
Stack<int> stack; 10
cout << stack.empty() << " " << stack.size();
Stack<int> stack; 8
assert(stack.top() == 12);
stack.pop();
stack.pop();
this->list.add(0, item);
T pop() {
return this->list.removeAt(0);
T top() {
return this->list.get(0);
}
bool empty() {
return this->list.empty();
int size() {
return this->list.size();
void clear() {
this->list.clear();
}
Test Expected Got
Stack<int> stack; 10 10
cout << stack.empty() << " " << stack.size();
Stack<int> stack; 8 8
assert(stack.top() == 12);
stack.pop();
stack.pop();
EX7:
Given a string S of characters, a duplicate removal consists of choosing two adjacent and equal
letters, and removing them.
Return the final string after all such duplicate removals have been made.
For example:
Test Result
EX8:
Given a string s containing just the characters '(', ')', '[', ']', '{', and '}'. Check if the input string is
valid based on following rules:
For example:
Test Result
class _Stack{
public:
class Node;
private:
Node* head;
int count = 0;
public:
T top();
bool isEmpty();
public:
class Node{
private:
T data;
Node* next;
public:
Node() {
next = 0;
Node(Node* next) {
this->next = next;
this->data = data;
this->next = next;
};
};
pNew->next = this->head;
this->head = pNew;
this->count++;
if (this->count == 0) return 0;
this->head = dltPtr->next;
this->count--;
delete dltPtr;
return 1;
T _Stack<T>::top(){
if (this->count == 0) return 0;
T dataOut = this->head->data;
return dataOut;
bool _Stack<T>::isEmpty(){
_Stack<char> parent;
parent.push(str[i]);
if (parent.isEmpty())
return false;
if ((top == '(' && str[i] == ')') || (top == '[' && str[i] == ']') || (top == '{' && str[i] == '}')){
parent.pop();
else
return false;
return parent.isEmpty();
EX9:
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).
#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 << "[";
{
ss << ptr->data << ",";
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
#ifndef SORTINGAPPLICATION_H
#define SORTINGAPPLICATION_H
#include <iostream>
#include <string>
using namespace std;
bool isPermutation (string a, string b) {}
#endif /* SORTINGAPPLICATION_H */
For example:
Test Result
string a = "abba"; 1
string b="baba";
cout << isPermutation(a, b);
string a = "abbac"; 0
string b="baba";
cout << isPermutation(a, b);
string a = "abba"; 1 1
string b="baba";
cout << isPermutation(a, b);
string a = "abbac"; 0 0
string b="baba";
cout << isPermutation(a, b);
EX11:
QuickSort is one of the fastest sorting algorithms for sorting large data. When implemented well, it
can be about two or three times faster than its main competitors, such as MergeSort and
HeapSort. When the number of elements is below some threshold (min_size), switch to insertion sort
- non-recursive sorting algorithm that performs fewer swaps, comparisons or other operations on
such small arrays.
Implement static methods hybridQuickSort in class Sorting to sort an array in ascending order. If
you do insertion sort, please print "Insertion sort: array" (using printArray) first. If you do quick sort,
please print "Quick sort: array (using printArray)" first. Please read example carefully to know exactly
what we print.
To match the test cases, please note:
class Sorting
private:
public:
};
For example:
Test Result
int array[] = {2, 6, 4, 12, 23, 1, 0, -12}; Quick sort: 2, 6, 4, 12, 23, 1, 0, -12
int min_size = 4; Insertion sort: 1, -12, 0
Sorting<int>::hybridQuickSort(&array[0], &array[8], min_size); Quick sort: 23, 12, 4, 6
Insertion sort: 6, 12, 4
int array[] = {30, 7, 20, 0, -13, 1, 19, 72}; Quick sort: 30, 7, 20, 0, -13, 1, 19, 72
int min_size = 3; Quick sort: 19, 7, 20, 0, -13, 1
Sorting<int>::hybridQuickSort(&array[0], &array[8], min_size); Quick sort: -13, 7, 1, 0
Quick sort: 7, 1, 0
Insertion sort: 0, 1
Insertion sort: 20
Insertion sort: 72
printArray(start,end);
T pivot = start[0];
int i=0;
int j=end-start;
do{
do{
i = i + 1;
do{
j = j - 1;
swap(start[i],start[j]);
swap(start[i],start[j]);
swap(start[0], start[j]);
return &start[j];
printArray(start,end);
int curr = 1;
T temp = start[curr];
start[walker+1] = start[walker];
walker = walker - 1;
start[walker+1] = temp;
curr = curr + 1;
insertionSort(start, end);
else{
if (end > start){
hybridQuickSort(pivot+1,end,min_size);
}
}
Test Expected Got
int array[] = {2, 6, 4, 12, 23, 1, 0, -12}; Quick sort: 2, 6, 4, 12, 23, 1, Quick sort: 2, 6, 4, 12, 23, 1,
int min_size = 4; 0, -12 0, -12
Sorting<int>::hybridQuickSort(&array[0], &array[8], Insertion sort: 1, -12, 0 Insertion sort: 1, -12, 0
min_size); Quick sort: 23, 12, 4, 6 Quick sort: 23, 12, 4, 6
Insertion sort: 6, 12, 4 Insertion sort: 6, 12, 4
int array[] = {30, 7, 20, 0, -13, 1, 19, 72}; Quick sort: 30, 7, 20, 0, -13, Quick sort: 30, 7, 20, 0, -13,
int min_size = 3; 1, 19, 72 1, 19, 72
Sorting<int>::hybridQuickSort(&array[0], &array[8], Quick sort: 19, 7, 20, 0, -13, 1 Quick sort: 19, 7, 20, 0, -13,
min_size); Quick sort: -13, 7, 1, 0 1
Quick sort: 7, 1, 0 Quick sort: -13, 7, 1, 0
Insertion sort: 0, 1 Quick sort: 7, 1, 0
Insertion sort: 20 Insertion sort: 0, 1
Insertion sort: 72 Insertion sort: 20
Insertion sort: 72
Passed all tests!
EX12:
Implement static methods Merge and MergeSort in class Sorting to sort an array in ascending
order. The Merge method has already been defined a call to method printArray so you do not have
to call this method again to print your array.
#ifndef SORTING_H
#define SORTING_H
#include <iostream>
using namespace std;
template <class T>
class Sorting {
public:
/* Function to print an array */
static void printArray(T *start, T *end)
{
long size = end - start + 1;
for (int i = 0; i < size - 1; i++)
cout << start[i] << ", ";
cout << start[size - 1];
cout << endl;
}
For example:
Test Result
EX13:
Implement static method oddEvenSort in class Sorting to sort an array in ascending order. After
each selection, we will print out a list to check (using printArray method). Remember to sort even
first.
Remind about odd even sort:
• This algorithm is divided into two phases - odd and even Phase. The algorithm runs until the
array elements are sorted and in each iteration two phases mentioned above occurs.
• In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase,
we perform a bubble sort on even indexed elements.
#include <iostream>
class Sorting
public:
};
For example:
Test Result
*a = *b;
*b = *temp;
/*TODO*/
flag = true;
if (start[i]>start[i+1]){
flag = false;
if (start[i]>start[i+1]){
flag = false;
Sorting<T>::printArray(start, end);
2, 1, 3, 3, 4, 5, 6, 8 2, 1, 3, 3, 4, 5, 6, 8
1, 2, 3, 3, 4, 5, 6, 8 1, 2, 3, 3, 4, 5, 6, 8
1, 2, 3, 3, 4, 5, 6, 8 1, 2, 3, 3, 4, 5, 6, 8
int arr[] = {9, 30, 1, -7, 7, -9, 5, 6,-1,-2}; 9, -7, 30, -9, 1, 5, 7, -2, 6, -1 9, -7, 30, -9, 1, 5, 7, -2, 6, -1
Sorting<int>::oddEvenSort(&arr[0], &arr[10]); -7, -9, 9, 1, 30, -2, 5, -1, 7, 6 -7, -9, 9, 1, 30, -2, 5, -1, 7, 6
-9, -7, 1, -2, 9, -1, 30, 5, 6, 7 -9, -7, 1, -2, 9, -1, 30, 5, 6, 7
-9, -7, -2, -1, 1, 5, 9, 6, 30, 7 -9, -7, -2, -1, 1, 5, 9, 6, 30, 7
-9, -7, -2, -1, 1, 5, 6, 7, 9, 30 -9, -7, -2, -1, 1, 5, 6, 7, 9, 30
-9, -7, -2, -1, 1, 5, 6, 7, 9, 30 -9, -7, -2, -1, 1, 5, 6, 7, 9, 30
EX14:
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 */
For example:
Test Result
int array[] = { 3, 5, 7, 10 ,12, 14, 15, 13, 1, 2, 9, 6, 4, 8, 11, 16, 17, Index of pivots: 2 0 0 6 1 0 2 1 0 0 2 1 0 0 0 0 0 0 1 0
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 << " ";
static T* Partition(T* start, T* end) {
// TODO: return the pointer which points to the pivot after rearrange the array.
T pivot = start[0];
int i=0;
int j=end-start;
do{
do{
i = i + 1;
} while(start[i] < pivot);
do{
j = j - 1;
} while(start[j] > pivot);
swap(start[i],start[j]);
EX15:
Implement static methods sortSegment and ShellSort 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;
public:
static void ShellSort(T* start, T* end, int* num_segment_list, int num_phases) ;
};
#endif /* SORTING_H */
For example:
Test Result
}
static void ShellSort(T* start, T* end, int* num_segment_list, int num_phases) {
// TODO
// Note: You must print out the array after sorting segments to check whether your algorithm is true.
for (int i=num_phases-1; i>=0;i--){
for (int j=0; j<num_segment_list[i]; j++)
sortSegment(start, end, j, num_segment_list[i]);
cout<<num_segment_list[i]<< " segments: ";
printArray(start, end);
}
}
EX16:
Implement static method selectionSort in class Sorting to sort an array in ascending order. After
each selection, we will print out a list to check (using printArray).
#include <iostream>
class Sorting
public:
};
For example:
Test Result