diff options
Diffstat (limited to 'nodelink.c')
-rw-r--r-- | nodelink.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/nodelink.c b/nodelink.c new file mode 100644 index 0000000..ad6beb3 --- /dev/null +++ b/nodelink.c @@ -0,0 +1,44 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "nodelink.h" +#include "strbst.h" + +node* newnode(void) { + node* new = malloc(sizeof(node)); + new->links = newbst(); + return new; +} + +link* newlink(node* to) { + link* new = malloc(sizeof(link)); + new->desc = ""; // preferred to NULL because it can be printed + new->to = to; + return new; +} + +static void printlink(char* name, link* conn) { + if (conn->desc[0]) + printf("Link to %s: %s", name, conn->desc); + else + printf("Link to %s", name); +} + +static void printeach(strbstnode* loc, char reprint) { + if (loc == NULL) return; + // loc->ind is the name of the connection and loc->data the link + if (!reprint) printf(" "); // indent on subnode + printlink(loc->ind, loc->data); // prints it + if (reprint) { // and their subnodes if iterating over root tree + printf("subnodes:\n"); + // prints the link's target's link tree + printeach( ( (link*)loc->data)->to->links->head, 0); + } + printeach(loc->left, reprint); + printeach(loc->right, reprint); // recursion +} + +void printnode(node* root) { + printf("Root node\n"); + printeach(root->links->head, 1); +} |