int bfs(void) { Graph gr; Slist network; struct BfsVertexdata_ bfstmp; BfsVertexdata netdata; int tmpid, retval; SlistNode listnode; my_clearscrn(); printf("--- NETWORK HOPS/BFS DEMO ---"); gr = GRAPHinit(bfs_match, bfs_destroy); /* Read net node(=vertex) data into graph.. */ if ((read_netnodes(gr, net_nodes, NR_OF_NETNODES)) != OK) { fprintf(stderr, "Fatal error when reading netnode data - bailing out.."); GRAPHdestroy(gr); exit(-1); } /* Read net connection(=edge) data into graph.. */ if ((read_netconnections(gr, net_connections, NR_OF_NETNODES)) != OK) { fprintf(stderr, "Fatal error when reading netconnections data - bailing out.."); GRAPHdestroy(gr); exit(-1); } prompt_and_pause("\n\nGraph initialized and graph data read.."); printf("\nGRAPH:"); /* Display graph.. */ GRAPHprint(gr, prt_bfs_vtx, prt_bfs_edge); printf("\nNr of vertices/edges: %d/%d", GRAPHvcount(gr), GRAPHecount(gr)); tmpid = read_int("\nEnter startnode id ", 1, 6); bfstmp.data = &tmpid; if ((retval = ALGObfs(gr, &bfstmp, &network, bfs_match)) != OK) { fprintf(stderr, "\nFatal error when calling 'ALGObfs()'(Return value: %d) - Bailing out..", retval); GRAPHdestroy(gr); exit(-1); } printf("\nNetwork Hops(BFS Analysis)\n--------------------------"); for (listnode = SLISThead(network); listnode != NULL; listnode = SLISTnext(listnode)) { netdata = (BfsVertexdata)SLISTdata(listnode); printf("\nNode%02d, color=%d, hops=%d", *(int *)netdata->data, netdata->color, netdata->hops); } prompt_and_pause("\n\nTime to tidy up.."); SLISTdestroy(network); GRAPHdestroy(gr); return OK; }
int dfs(void) { Graph gr; Slist tasks; DfsVertexdata taskdata; int retval; SlistNode listnode; my_clearscrn(); printf("--- TOPOLOGICAL SORTING/DFS DEMO ---"); gr = GRAPHinit(dfs_match, dfs_destroy); /* Read task node(=vertex) data into graph.. */ if ((read_tasknodes(gr, task_nodes, NR_OF_TASKNODES)) != OK) { fprintf(stderr, "Fatal error when reading task node data - bailing out.."); GRAPHdestroy(gr); exit(-1); } /* Read task connection(=edge) data into graph.. */ if ((read_taskconnections(gr, task_connections, NR_OF_TASKNODES)) != OK) { fprintf(stderr, "Fatal error when reading taskconnections data - bailing out.."); GRAPHdestroy(gr); exit(-1); } prompt_and_pause("\n\nGraph created and graph data read.."); printf("\nGRAPH:"); /* Display graph.. */ GRAPHprint(gr, prt_dfs_vtx, prt_dfs_edge); printf("\nNr of vertices/edges: %d/%d", GRAPHvcount(gr), GRAPHecount(gr)); if ((retval = ALGOdfs(gr, &tasks)) != OK) { fprintf(stderr, "\nFatal error when calling 'ALGOdfs()'(Return value: %d) - Bailing out..", retval); GRAPHdestroy(gr); exit(-1); } printf("\n\nTopological Sort(DFS Analysis):\n-------------------------------"); for (listnode = SLISThead(tasks); listnode != NULL; listnode = SLISTnext(listnode)) { taskdata = (DfsVertexdata)SLISTdata(listnode); printf("\nNode %c, color=%d", *(char *)taskdata->data, taskdata->color); } prompt_and_pause("\n\nTime to tidy up.."); SLISTdestroy(tasks); GRAPHdestroy(gr); return OK; }
int CHTBLlookup(const CHtbl htbl, void **data) { int bucket; SlistNode tmpnode; /* Hash the key */ bucket = htbl->h(*data) % htbl->buckets; if ((tmpnode = SLISTfindnode(htbl->table[bucket], *data))) /* Data found */ { *data = SLISTdata(tmpnode); /* Pass data back to caller */ return 0; } return -1; }
SlistNode SLISTfindnode(Slist list, const void *data) { SlistNode member = NULL; /* Match callback not set */ if (list->match == NULL) return NULL; for (member = list->head; member != NULL; member = member->next) { if (list->match(data, SLISTdata(member))) break; } return member; }
/* --- Function: void ins_nodes(Slist list) --- */ void ins_nodes(Slist list) { int tmp, *pi; SlistNode node; char mess[BUFSIZ]; do { my_clearscrn(); printf("--- ADD NODE - WITH DATA=99 - AFTER USER-SPECIFIED NODE ---\n"); printf("\nCurrent list status(%d nodes): ", SLISTsize(list)); SLISTtraverse(list, print, SLIST_FWD); tmp = read_int("\nEnter (key)data, after which new node(key=99) will be inserted (-1=Quit): ", 0, 0); if (tmp == -1) break; if ((node = SLISTfindnode(list, &tmp)) != NULL) /* Node found */ { /* Insert node after first occurance of user-specified node */ pi = (int *)malloc(sizeof(int)); MALCHK(pi); *pi = 99; if ((SLISTinsnext(list, node, pi)) != OK) { printf("\nFatal error - exiting...!"); SLISTdestroy(list); exit(-1); } else { sprintf(mess, "Node 99 will be inserted after node %d", *(int *)SLISTdata(node)); prompt_and_pause(mess); } } else { sprintf(mess, "Error: Node %d not found...!", tmp); prompt_and_pause(mess); } } while (TRUE); }