More node management
Signed-off-by: Amrit Panesar <apanesar@4195tech.com>
This commit is contained in:
1 parent
6e34d2bf12
commit
4c9d56f150
5 files changed
+102
-52
No files matched your search
@@ -1,5 +1,5 @@
|
||||
#include "Lab3.h"
|
||||
|
||||
int main(int argc, char[]* argv) {
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
NodeManagerP m = NewNodeManager();
|
||||
}
|
||||
+1
-10
@@ -1,16 +1,7 @@
|
||||
#include "Node.h"
|
||||
|
||||
void Node::Init(int aIn, float bIn, char *nameIn) {
|
||||
void Node::New(int aIn, float bIn, char *nameIn) {
|
||||
a = aIn;
|
||||
b = bIn;
|
||||
name = nameIn;
|
||||
}
|
||||
|
||||
NodeP NewNode(int aIn, float bIn, char *nameIn) {
|
||||
NodeP out;
|
||||
|
||||
out = (NodeP)malloc(sizeof(Node));
|
||||
out->Init(aIn, bIn, nameIn);
|
||||
|
||||
return out;
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
#ifndef H_NODE
|
||||
#define H_NODE
|
||||
|
||||
|
||||
typedef struct {
|
||||
int a;
|
||||
float b;
|
||||
@@ -13,7 +12,7 @@ typedef struct {
|
||||
NodeP right;
|
||||
NodeP self;
|
||||
|
||||
void Init(int aIn, float bIn, char *nameIn);
|
||||
void New(int aIn, float bIn, char *nameIn);
|
||||
|
||||
}Node, *NodeP;
|
||||
|
||||
|
||||
@@ -4,78 +4,131 @@ void NodeManager::Init(int initialSize) {
|
||||
Nodes = (NodeP*)malloc(sizeof(NodeP) * initialSize);
|
||||
}
|
||||
|
||||
int NodeManager::Add(NodeP nodeIn) {
|
||||
NodeP arrnode;
|
||||
int NodeManager::Length() {
|
||||
return nodeCount;
|
||||
}
|
||||
|
||||
if (nodeCount + 1 > allocatedCount) {
|
||||
Nodes = (NodeP*)realloc(Nodes, sizeof(Node) * nodeCount);
|
||||
}
|
||||
|
||||
// inserts at end
|
||||
Nodes[nodeCount] = nodeIn;
|
||||
Nodes[nodeCount]->self = nodeIn;
|
||||
Nodes[nodeCount]->left = Nodes[nodeCount-1];
|
||||
|
||||
Nodes[nodeCount-1]->right = nodeIn;
|
||||
|
||||
nodeCount++;
|
||||
Sort();
|
||||
int NodeManager::Size() {
|
||||
return allocatedCount;
|
||||
}
|
||||
|
||||
void NodeManager::Sort() {
|
||||
// A-Za-z0-9 sort
|
||||
int i = 0;
|
||||
|
||||
if (nodeCount < 2) {
|
||||
if (Size() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < nodeCount-1; i++) {
|
||||
for (i = 0; i < Size()-1; i++) {
|
||||
if (strcmp(Nodes[i]->name, Nodes[i+1]->name) > 0) {
|
||||
Swap(i, i+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NodeManager::Swap(int id1, int id2) {
|
||||
NodeP one, two;
|
||||
void NodeManager::ReorderRefrence() {
|
||||
int i;
|
||||
NodeP curNode = startNode;
|
||||
for (i = 0; i < nodeCount; i++) {
|
||||
Nodes[i] = curNode;
|
||||
curNode = curNode->right;
|
||||
}
|
||||
}
|
||||
|
||||
boolean NodeManager::Swap(int id1, int id2) {
|
||||
NodeP one, oneL, oneR;
|
||||
NodeP two, twoL, twoR;
|
||||
|
||||
one = GetAt(id1);
|
||||
two = GetAt(id2);
|
||||
if (one == NULL || two == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//left node
|
||||
one = Nodes[id1]->left;
|
||||
two = Nodes[id2]->left;
|
||||
Nodes[id1]->left = two;
|
||||
Nodes[id2]->left = one;
|
||||
oneL = one->left;
|
||||
oneR = one->right;
|
||||
|
||||
//right node
|
||||
one = Nodes[id1]->right;
|
||||
two = Nodes[id2]->right;
|
||||
Nodes[id1]->right = two;
|
||||
Nodes[id2]->right = one;
|
||||
twoL = two->left;
|
||||
twoR = two->right;
|
||||
|
||||
//node entry
|
||||
one = Nodes[id1];
|
||||
two = Nodes[id2];
|
||||
Nodes[id1] = two;
|
||||
Nodes[id2] = one;
|
||||
//swap
|
||||
one->left = twoL;
|
||||
one->right = twoR;
|
||||
two->left = oneL;
|
||||
two->right = twoR;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
NodeP NodeManager::GetAt(int index) {
|
||||
|
||||
int i;
|
||||
NodeP curNode;
|
||||
if (index > nodeCount) {
|
||||
return NULL;
|
||||
}
|
||||
for (i = 0; i < nodeCount; i++) {
|
||||
curNode = Nodes[0];
|
||||
}
|
||||
return curNode;
|
||||
}
|
||||
|
||||
boolean NodeManager::InsertAt(int index, NodeP nodeIn) {
|
||||
int i;
|
||||
NodeP leftNode;
|
||||
NodeP curNode;
|
||||
NodeP rightNode;
|
||||
|
||||
// make more room if we don't have any more
|
||||
if (nodeCount + 1 > allocatedCount) {
|
||||
Nodes = (NodeP*)realloc(Nodes, sizeof(Node) * (nodeCount + (nodeCount /2)));
|
||||
}
|
||||
|
||||
curNode = GetAt(index);
|
||||
leftNode = curNode->left;
|
||||
rightNode = curNode->right;
|
||||
|
||||
nodeCount++;
|
||||
}
|
||||
|
||||
boolean NodeManager::DeleteAt(int index) {
|
||||
NodeP node = GetAt(index);
|
||||
if (node == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
NodeP left = node->left;
|
||||
NodeP right = node->right;
|
||||
|
||||
left->right = right;
|
||||
right->left = left;
|
||||
|
||||
free(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
void NodeManager::Destroy() {
|
||||
int i;
|
||||
for (i = 0; i < Length() - 1; i++) {
|
||||
free(Nodes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void NodeManager::Add(int aIn, float bIn, char *nameIn) {
|
||||
NodeP out;
|
||||
|
||||
out = (NodeP)malloc(sizeof(Node));
|
||||
out->New(aIn, bIn, nameIn);
|
||||
InsertAt(nodeCount - 1, out);
|
||||
}
|
||||
|
||||
NodeManagerP NewNodeManager(int size) {
|
||||
NodeManagerP nodem;
|
||||
|
||||
nodem = (NodeManagerP)malloc(sizeof(NodeManager));
|
||||
nodem->Init(5);
|
||||
nodem->Init(size);
|
||||
|
||||
return nodem;
|
||||
}
|
||||
@@ -11,19 +11,26 @@ private:
|
||||
|
||||
public:
|
||||
NodeP *Nodes;
|
||||
NodeP startNode;
|
||||
|
||||
void Init(int initialSize);
|
||||
int Size();
|
||||
int Length();
|
||||
|
||||
int Add(NodeP nodeIn);
|
||||
NodeP GetAt(int index);
|
||||
void Add(int aIn, float bIn, char *nameIn);
|
||||
NodeP GetAt(int index);
|
||||
boolean InsertAt(int index, NodeP nodeIn);
|
||||
boolean DeleteAt(int index);
|
||||
|
||||
void Sort();
|
||||
void Swap(int id1, int id2);
|
||||
boolean Swap(int id1, int id2);
|
||||
|
||||
void Destroy();
|
||||
void ReorderRefrence();
|
||||
|
||||
} NodeManager, *NodeManagerP;
|
||||
|
||||
NodeManagerP NewNodeManager(int size);
|
||||
#define N_INITIALSIZE 10
|
||||
NodeManagerP NewNodeManager(int size = N_INITIALSIZE);
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user