This repository has been archived by the owner on Aug 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnrolledLinkedList.h
87 lines (76 loc) · 2.04 KB
/
UnrolledLinkedList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Copyright (C) 2018
* Course: CO2003
* Author: Rang Nguyen
* Ho Chi Minh City University of Technology
*/
#pragma once
#include"Node.h"
#include <cmath> // ! Fix ceil
class UnrolledLinkedList {
private:
Node* head;
Node* tail;
int size;
int numOfNodes;
int nodeSize;
public:
// Your tasks: complete the implementation of the below methods in UnrolledLinkedList.h
void add(int val);
bool contains(int val);
int getAt(int pos);
void setAt(int pos, int val);
int firstIndexOf(int val);
int lastIndexOf(int val);
void insertAt(int pos, int val);
void deleteAt(int pos);
void reverse();
int* toArray();
UnrolledLinkedList(int capacity) {
nodeSize = capacity;
head = tail = NULL;
size = numOfNodes = 0;
}
~UnrolledLinkedList() {
Node* p;
while (p = head) {
head = head->next;
delete p;
}
size = numOfNodes = 0;
head = tail = NULL;
}
int getSize() { return size; }
int getHalfNodeSize() {return (int)ceil(nodeSize / 2.0);}
void print() {
printf("=================METADATA================\n");
printf("Size = %d, nodeSize = %d, halfNodeSize = %d\n", size, nodeSize, getHalfNodeSize());
printf("-----------------CONTENTS----------------\n");
Node* p = head;
if (p == NULL) {
printf("NULL\n");
return;
}
while (p != NULL) {
// check whether your implementation satisfied the requirements
if ((p->isUnderHalfFull() && size >= getHalfNodeSize()) ||
p->isOverflow()) {
throw "Something wrong with your implementation";
}
p->print();
p = p->next;
}
printf("\n-------------------END-------------------\n");
}
void printDetail() {
printf("=================METADATA================\n");
printf("Size = %d, numOfNodes = %d, nodeSize = %d\n", size, numOfNodes, nodeSize);
printf("-----------------CONTENTS----------------\n");
Node* p = head;
if (p == NULL) printf("NULL\n");
while (p != NULL) {
p->printDetail();
p = p->next;
}
printf("-------------------END-------------------\n");
}
};