diff options
Diffstat (limited to '12.c')
-rw-r--r-- | 12.c | 84 |
1 files changed, 84 insertions, 0 deletions
@@ -0,0 +1,84 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#define MAX_CONNECTIONS 50 +#define MAX_NODES 50 + +typedef struct node +{ + char name[32]; + int links; + struct node *link[MAX_CONNECTIONS]; +} Node; + +static Node node[MAX_NODES] = {0}; +static int nodes = 0; +static Node *start = NULL; +static Node *end = NULL; + +static char *ReadLine(char *p, size_t size, FILE *fp) +{ + if ((p=fgets(p, size, fp))) + { + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } + } + + return p; +} + +static Node *GetOrAddNode(const char *name) +{ + int f; + + for(f = 0; f < nodes; f++) + { + if (strcmp(name, node[f].name) == 0) + { + return node + f; + } + } + + if (f == MAX_NODES) + { + fprintf(stderr, "Too many nodes\n"); + exit(1); + } + + strcpy(node[f].name, name); + + return node + f; +} + +static void Link(Node *node1, Node *node2) +{ + node1->link[node1->links++] = node2; + node2->link[node2->links++] = node1; +} + +int main(void) +{ + char buff[4096]; + + start = GetOrAddNode("start"); + end = GetOrAddNode("end"); + + while(ReadLine(buff, sizeof buff, stdin)) + { + Node *node1, *node2; + char *p; + + p = strtok(buff, "-"); + node1 = GetOrAddNode(p); + p = strtok(NULL, "-"); + node2 = GetOrAddNode(p); + Link(node1, node2); + } + + return 0; +} |