mirror of
https://github.com/tiennm99/HCMUT.git
synced 2026-10-14 06:17:57 +00:00
Update Assignment
This commit is contained in:
1 parent
edd9b9a1f6
commit
a4393f4d3b
9 files changed
+983
No files matched your search
@@ -0,0 +1,99 @@
|
||||
/* Copyright (C) 2018
|
||||
* Course: CO2003
|
||||
* Author: Rang Nguyen
|
||||
* Ho Chi Minh City University of Technology
|
||||
*/
|
||||
|
||||
#include "Node.h"
|
||||
|
||||
Node::Node(int capacity)
|
||||
{
|
||||
maxElements = capacity;
|
||||
elements = new int[maxElements];
|
||||
numElements = 0;
|
||||
prev = next = nullptr;
|
||||
}
|
||||
|
||||
Node::~Node()
|
||||
{
|
||||
if (elements != nullptr)
|
||||
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("size: %d ", numElements);
|
||||
printf("| prev(%d) |", prev);
|
||||
for (int i = 0; i < numElements; i++)
|
||||
printf(" %d |", elements[i]);
|
||||
for (int i = numElements; i < maxElements; i++)
|
||||
printf(" X |");
|
||||
printf(" next(%d) |\n", next);
|
||||
}
|
||||
Reference in new issue
Block a user