This repository was 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
Expand file tree
/
Copy pathNode.cpp
More file actions
89 lines (75 loc) · 1.9 KB
/
Node.cpp
File metadata and controls
89 lines (75 loc) · 1.9 KB
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
88
89
/*
* Copyright (C) 2018
* Course: CO2003
* Author: Rang Nguyen
* Ho Chi Minh City University of Technology
*/
#include"Node.h"
#include <cstring> // ! Fix memmove()
#include <cmath> // ! Fix ceil()
Node::Node(int capacity) {
maxElements = capacity;
elements = new int[capacity]; // ! Allocated dynamic
numElements = 0;
prev = next = NULL;
}
Node::~Node() {
if (elements != NULL)
delete[] elements;
}
int Node::getHalfNodeSize() {
return (int)ceil(maxElements/2.0);
}
bool Node::isUnderHalfFull() {
return numElements < getHalfNodeSize();
}
bool Node::isFull() {
return numElements >= maxElements;
}
bool Node::isOverflow() {
return numElements > maxElements;
}
bool Node::isEmpty() {
return numElements == 0;
}
void Node::add(int val) {
if (isFull())
throw "NodeOverflowExeception";
else
{
elements[numElements] = val;
numElements++;
}
}
void Node::insertAt(int pos, int val) {
if (isFull()) throw "NodeOverflowExeception";
else if (pos < 0 || pos > numElements) throw "IndexOutOfBoundsException";
else {
memmove(elements + pos + 1, elements + pos, (numElements - pos)*sizeof(int));
elements[pos] = val;
numElements++;
}
}
void Node::removeAt(int pos) {
if (pos < 0 || pos >= numElements) throw "IndexOutOfBoundsException";
else {
memmove(elements + pos, elements + pos + 1, (numElements - pos - 1)*sizeof(int));
numElements--;
}
}
void Node::reverse() {
for(int i = 0; i<numElements/2;i++)
std::swap(elements[i], elements[numElements - 1 - i]);
}
void Node::print() {
for (int i = 0; i < numElements; i++)
printf("%d ", elements[i]);
}
void Node::printDetail() {
printf("| prev(%p) |", prev); // ! %d -> %p
for (int i = 0; i < numElements; i++)
printf(" %d |", elements[i]);
for (int i = numElements; i < maxElements; i++)
printf(" X |");
printf(" next(%p) |\n", next);// ! $d -> %p
}