Advertisement
Guest User

Untitled

a guest
Sep 29th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. typedef struct FORK *PFork;
  5. typedef struct FORK **PPFork;
  6. struct FORK {
  7. int value;
  8. PFork next;
  9. };
  10.  
  11. PFork getNode() {return (PFork) malloc(sizeof(struct FORK));}
  12.  
  13. PPFork createRoot(int nodes, int maxConnections) {
  14. int i;
  15. PPFork root = (PPFork) malloc(sizeof(struct FORK)*nodes);
  16. for ( i = 0 ; i < nodes ; i++ ) {
  17. root[i] = NULL;
  18. }
  19. return root;
  20. }
  21.  
  22. PPFork clearRoot(PPFork oldRoot, int nodes, int maxConnections) {
  23. int i;
  24. for ( i = 0 ; i < nodes ; i++) {
  25. free(oldRoot[i]);
  26. }
  27. return NULL;
  28. }
  29.  
  30. int insertInRoot(PPFork root, int aimNode, int targetNode) {
  31. PFork last = root[aimNode];
  32. if (last == NULL) return 0;
  33. while (last->next != NULL) last = last->next;
  34. printf("last->value => %i\n", last->value);
  35. printf("root[targetNode] value => %i\n", root[targetNode]->value);
  36. last->next = root[targetNode];
  37. return 1;
  38. }
  39.  
  40. PFork insertNode(int value) {
  41. PFork newNode = getNode();
  42. if (newNode == NULL) return NULL;
  43. newNode->value = value;
  44. newNode->next = NULL;
  45. return newNode;
  46. }
  47.  
  48.  
  49. int main() {
  50. PPFork root = NULL;
  51. int i;
  52. root = createRoot(5, 5);
  53. for ( i = 0 ; i < 5 ; i++ ) {
  54. root[i] = insertNode(i+1);
  55. }
  56. insertInRoot(root, 1, 4);
  57. printf("root[1]->next->value => %i\n", root[1]->next->value);
  58. root = clearRoot(root, 5, 5);
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement