Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct FORK *PFork;
- typedef struct FORK **PPFork;
- struct FORK {
- int value;
- PFork next;
- };
- PFork getNode() {return (PFork) malloc(sizeof(struct FORK));}
- PPFork createRoot(int nodes, int maxConnections) {
- int i;
- PPFork root = (PPFork) malloc(sizeof(struct FORK)*nodes);
- for ( i = 0 ; i < nodes ; i++ ) {
- root[i] = NULL;
- }
- return root;
- }
- PPFork clearRoot(PPFork oldRoot, int nodes, int maxConnections) {
- int i;
- for ( i = 0 ; i < nodes ; i++) {
- free(oldRoot[i]);
- }
- return NULL;
- }
- int insertInRoot(PPFork root, int aimNode, int targetNode) {
- PFork last = root[aimNode];
- if (last == NULL) return 0;
- while (last->next != NULL) last = last->next;
- printf("last->value => %i\n", last->value);
- printf("root[targetNode] value => %i\n", root[targetNode]->value);
- last->next = root[targetNode];
- return 1;
- }
- PFork insertNode(int value) {
- PFork newNode = getNode();
- if (newNode == NULL) return NULL;
- newNode->value = value;
- newNode->next = NULL;
- return newNode;
- }
- int main() {
- PPFork root = NULL;
- int i;
- root = createRoot(5, 5);
- for ( i = 0 ; i < 5 ; i++ ) {
- root[i] = insertNode(i+1);
- }
- insertInRoot(root, 1, 4);
- printf("root[1]->next->value => %i\n", root[1]->next->value);
- root = clearRoot(root, 5, 5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement