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"
|
#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"
|
#include "Node.h"
|
||||||
|
|
||||||
void Node::Init(int aIn, float bIn, char *nameIn) {
|
void Node::New(int aIn, float bIn, char *nameIn) {
|
||||||
a = aIn;
|
a = aIn;
|
||||||
b = bIn;
|
b = bIn;
|
||||||
name = nameIn;
|
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
|
#ifndef H_NODE
|
||||||
#define H_NODE
|
#define H_NODE
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int a;
|
int a;
|
||||||
float b;
|
float b;
|
||||||
@@ -13,7 +12,7 @@ typedef struct {
|
|||||||
NodeP right;
|
NodeP right;
|
||||||
NodeP self;
|
NodeP self;
|
||||||
|
|
||||||
void Init(int aIn, float bIn, char *nameIn);
|
void New(int aIn, float bIn, char *nameIn);
|
||||||
|
|
||||||
}Node, *NodeP;
|
}Node, *NodeP;
|
||||||
|
|
||||||
|
|||||||
@@ -4,78 +4,131 @@ void NodeManager::Init(int initialSize) {
|
|||||||
Nodes = (NodeP*)malloc(sizeof(NodeP) * initialSize);
|
Nodes = (NodeP*)malloc(sizeof(NodeP) * initialSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
int NodeManager::Add(NodeP nodeIn) {
|
int NodeManager::Length() {
|
||||||
NodeP arrnode;
|
return nodeCount;
|
||||||
|
}
|
||||||
|
|
||||||
if (nodeCount + 1 > allocatedCount) {
|
int NodeManager::Size() {
|
||||||
Nodes = (NodeP*)realloc(Nodes, sizeof(Node) * nodeCount);
|
return allocatedCount;
|
||||||
}
|
|
||||||
|
|
||||||
// inserts at end
|
|
||||||
Nodes[nodeCount] = nodeIn;
|
|
||||||
Nodes[nodeCount]->self = nodeIn;
|
|
||||||
Nodes[nodeCount]->left = Nodes[nodeCount-1];
|
|
||||||
|
|
||||||
Nodes[nodeCount-1]->right = nodeIn;
|
|
||||||
|
|
||||||
nodeCount++;
|
|
||||||
Sort();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeManager::Sort() {
|
void NodeManager::Sort() {
|
||||||
// A-Za-z0-9 sort
|
// A-Za-z0-9 sort
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
if (nodeCount < 2) {
|
if (Size() < 2) {
|
||||||
return;
|
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) {
|
if (strcmp(Nodes[i]->name, Nodes[i+1]->name) > 0) {
|
||||||
Swap(i, i+1);
|
Swap(i, i+1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeManager::Swap(int id1, int id2) {
|
void NodeManager::ReorderRefrence() {
|
||||||
NodeP one, two;
|
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
|
//left node
|
||||||
one = Nodes[id1]->left;
|
oneL = one->left;
|
||||||
two = Nodes[id2]->left;
|
oneR = one->right;
|
||||||
Nodes[id1]->left = two;
|
|
||||||
Nodes[id2]->left = one;
|
|
||||||
|
|
||||||
//right node
|
//right node
|
||||||
one = Nodes[id1]->right;
|
twoL = two->left;
|
||||||
two = Nodes[id2]->right;
|
twoR = two->right;
|
||||||
Nodes[id1]->right = two;
|
|
||||||
Nodes[id2]->right = one;
|
|
||||||
|
|
||||||
//node entry
|
//swap
|
||||||
one = Nodes[id1];
|
one->left = twoL;
|
||||||
two = Nodes[id2];
|
one->right = twoR;
|
||||||
Nodes[id1] = two;
|
two->left = oneL;
|
||||||
Nodes[id2] = one;
|
two->right = twoR;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeP NodeManager::GetAt(int index) {
|
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) {
|
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) {
|
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 NewNodeManager(int size) {
|
||||||
NodeManagerP nodem;
|
NodeManagerP nodem;
|
||||||
|
|
||||||
nodem = (NodeManagerP)malloc(sizeof(NodeManager));
|
nodem = (NodeManagerP)malloc(sizeof(NodeManager));
|
||||||
nodem->Init(5);
|
nodem->Init(size);
|
||||||
|
|
||||||
return nodem;
|
return nodem;
|
||||||
}
|
}
|
||||||
@@ -11,19 +11,26 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
NodeP *Nodes;
|
NodeP *Nodes;
|
||||||
|
NodeP startNode;
|
||||||
|
|
||||||
void Init(int initialSize);
|
void Init(int initialSize);
|
||||||
|
int Size();
|
||||||
|
int Length();
|
||||||
|
|
||||||
int Add(NodeP nodeIn);
|
void Add(int aIn, float bIn, char *nameIn);
|
||||||
NodeP GetAt(int index);
|
NodeP GetAt(int index);
|
||||||
boolean InsertAt(int index, NodeP nodeIn);
|
boolean InsertAt(int index, NodeP nodeIn);
|
||||||
boolean DeleteAt(int index);
|
boolean DeleteAt(int index);
|
||||||
|
|
||||||
void Sort();
|
void Sort();
|
||||||
void Swap(int id1, int id2);
|
boolean Swap(int id1, int id2);
|
||||||
|
|
||||||
|
void Destroy();
|
||||||
|
void ReorderRefrence();
|
||||||
|
|
||||||
} NodeManager, *NodeManagerP;
|
} NodeManager, *NodeManagerP;
|
||||||
|
|
||||||
NodeManagerP NewNodeManager(int size);
|
#define N_INITIALSIZE 10
|
||||||
|
NodeManagerP NewNodeManager(int size = N_INITIALSIZE);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in new issue
Block a user