aboutsummaryrefslogtreecommitdiff
path: root/nodelink.c
blob: aaf8c189c0f763540f26100489e1ea8e1a7fc799 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "nodelink.h"
#include "strbst.h"

node* newnode(void) {
    node* new = malloc(sizeof(node));
    new->links = newbst();
    return new;
}

static char* empty(void) {
    char* out = malloc(sizeof(char));
    *out = 0;
    return out;
}

link* newlink(node* to) {
    link* new = malloc(sizeof(link));
    new->desc = empty(); // 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("%s: %s", name, conn->desc);
    else
        printf("%s\n", 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
    // and subnodes if iterating over root tree (except root link)
    if (reprint && strcmp(loc->ind,"")) {
        // 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) {
    printeach(root->links->head, 1);
}