/* --- Function: void push_node(Stack stk) --- */ void push_node(Stack stk) { int tmp, *pi; char mess[BUFSIZ]; do { my_clearscrn(); printf("--- PUSH NODE ON STACK ---\n"); printf("\nCurrent stack status(%d nodes): ", STACKsize(stk)); SLISTtraverse(stk, print, SLIST_FWD); tmp = read_int("\nEnter integer data of node to be pushed (-1=Quit): ", 0, 0); if (tmp == -1) break; pi = (int *)malloc(sizeof(int)); MALCHK(pi); *pi = tmp; if ((STACKpush(stk, pi)) != OK) { printf("\nFatal error pushing data - exiting...!"); STACKdestroy(stk); exit(-1); } else { sprintf(mess, "Node %d will be pushed on stack!", *pi); prompt_and_pause(mess); } } while (TRUE); }
/* --- Function: void search_node(Slist lst) --- */ void search_node(Slist lst) { int tmp; char mess[BUFSIZ]; do { my_clearscrn(); printf("--- SEARCH NODE ---\n"); printf("\nCurrent list status(%d nodes): ", SLISTsize(lst)); SLISTtraverse(lst, print, SLIST_FWD); tmp = read_int("\nEnter keydata for node to be found (-1=Quit): ", 0, 0); if (tmp == -1) break; if (SLISTfindnode(lst, &tmp) == NULL) /* Node not found.. */ { sprintf(mess, "Node %d NOT found..!", tmp); prompt_and_pause(mess); } else { /* Search succesful - notify user.. */ sprintf(mess, "Node %d FOUND!", tmp); prompt_and_pause(mess); } } while (TRUE); }
/* --- Function: void enqueue_node(Queue que) --- */ void enqueue_node(Queue que) { int tmp, *pi; char mess[BUFSIZ]; do { my_clearscrn(); printf("--- ENQUEUE NODE TO QUEUE ---\n"); printf("\nCurrent queue status(%d nodes): ", QUEUEsize(que)); SLISTtraverse(que, print, SLIST_FWD); tmp = read_int("\nEnter integer data of node to be enqueued (-1=Quit): ", 0, 0); if (tmp == -1) break; pi = (int *)malloc(sizeof(int)); MALCHK(pi); *pi = tmp; if ((QUEUEenqueue(que, pi)) != OK) { printf("\nFatal error enqueing data - exiting...!"); QUEUEdestroy(que); exit(-1); } else { sprintf(mess, "Node %d will be enqueued!", *pi); prompt_and_pause(mess); } } while (TRUE); }
/* --- Function: void create_random_nodes(Dlist list, int nr_of_nodes) --- */ void create_random_nodes(Dlist list, int nr_of_nodes) { int i=0, *pi, retval; my_clearscrn(); /* Initialize the list.. */ printf("--- CREATED A DOUBLY-LINKED LIST(%d NODES) - RANDOM INTEGER DATA ---", NR_OF_ITEMS); do { pi = (int *)malloc(sizeof(int)); MALCHK(pi); *pi = rand_int(1,50); /* Defensive programming... */ if (!DLISTsize(list)) retval=DLISTinsprev(list, NULL, pi); else retval=DLISTinsprev(list, DLISThead(list), pi); assert(retval == OK); } while (++i < nr_of_nodes); /* Display the list... */ printf("\n\nCurrent list status(%d nodes): ", DLISTsize(list)); printf("\nAscending : "); DLISTtraverse(list, print, DLIST_FWD); printf("\nDescending: "); DLISTtraverse(list, print, DLIST_BWD); prompt_and_pause("\n\n"); }
/* --- Function: void print_info(Graph gr, void (*vtxprt)(const void *data), void (*edgprt)(const void *data)) --- */ void print_info(Graph gr, void (*vtxprt)(const void *data), void (*edgprt)(const void *data)) { my_clearscrn(); printf("--- BASIC GRAPH OPERATIONS ---\n"); GRAPHprint(gr, vtxprt, edgprt); printf("\n\nNr of vertices/edges: %d/%d ", GRAPHvcount(gr), GRAPHecount(gr)); }
/* --- Function: void create_random_nodes(Slist list, int nr_of_nodes) --- */ void create_random_nodes(Slist list, int nr_of_nodes) { int i=0, *pi, retval; my_clearscrn(); /* Initialize the list.. */ printf("--- CREATED A SINGLY-LINKED LIST(%d NODES)- RANDOM INTEGER DATA ---", NR_OF_ITEMS); do { pi = (int *)malloc(sizeof(int)); MALCHK(pi); *pi = rand_int(1,50); retval=SLISTinsnext(list, NULL, pi); assert(retval == OK); } while (++i < nr_of_nodes); /* Display the list... */ printf("\n\nCurrent list status(%d nodes): ", SLISTsize(list)); SLISTtraverse(list, print, SLIST_FWD); prompt_and_pause("\n\n"); }
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; }
/* --- Function: void create_nodes(BiTree tree, int nr_of_nodes) --- */ void create_nodes(BiTree tree, int nr_of_nodes) { int i=0, *pi, retval, dupctr=0; do { pi = (int *)malloc(sizeof(int)); MALCHK(pi); *pi = rand_int(1,99); if ((retval = BITREEinsert(tree, pi)) != 0) /* Insertion failed... */ { if (retval == 1) /* Duplicate key value.. */ { dupctr++; my_destroy(pi); /* Free node - since duplicate.. */ } else { prompt_and_pause("Fatal error - bailing out..!\n"); BITREEdestroy(tree); exit(-1); } } } while (++i < nr_of_nodes); my_clearscrn(); printf("--- INITIALIZING A BINARY SEARCH TREE, %d NODES, RANDOM INTEGER DATA ---\n", NR_OF_ITEMS); print_tree(tree); printf("\n\n%d/%d successful insertions -- %d duplicate(s) rejected..", BITREEsize(tree), nr_of_nodes, dupctr); prompt_and_pause("\n\n"); }
/* --- Function: void final_status(Slist list) --- */ void final_status(Slist list) { my_clearscrn(); printf("--- FINAL STATUS ---\n"); /* Final list status... */ printf("\nFinal list contents(%d nodes): ", SLISTsize(list)); SLISTtraverse(list, print, SLIST_FWD); }
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; }
/* --- Function: void print_list(Slist lst) --- */ void print_list(Slist lst) { my_clearscrn(); printf("--- PRINT LIST ---\n"); /* List status... */ printf("\nCurrent list status(%d nodes): ", SLISTsize(lst)); SLISTtraverse(lst, print, SLIST_FWD); prompt_and_pause("\n\n"); }
/* --- Function: void final_status(Slist list) --- */ void final_status(BiTree tree) { /* Final list status... */ my_clearscrn(); printf("--- FINAL TREE STATUS ---\n"); BITREEprint(tree, print); printf("INORDER: "); BITREEinorder(tree, print); }
int main(void) { /* Declare YOUR variables here ! */ BiTree mytree; int menu_choice; srand((unsigned int)time(NULL)); if ((mytree = BITREEinit(my_destroy)) == NULL) { printf("\nFatal error - bailing out...\n!"); BITREEdestroy(mytree); exit(-1); } /* Don't forget to set the compare callback..! */ BITREEsetcompare(mytree, my_cmp); /* Initialize - and add nodes to the table... */ create_nodes(mytree, NR_OF_ITEMS); do { menu_choice = menu(MAIN_MENU_ROW, 0, 4); switch (menu_choice) { case 1: ins_node(mytree); break; case 2: rem_node(mytree); break; case 3: search_node(mytree); break; case 4: my_clearscrn(); printf("--- PRINT TREE ---\n"); print_tree(mytree); prompt_and_pause("\n\n"); break; default: final_status(mytree); break; } } while (menu_choice); prompt_and_pause("\n\nLet's tidy up and destroy the tree..- Bye!"); BITREEdestroy(mytree); return 0; }
/* --- Function: void final_status(Dlist list) --- */ void final_status(Dlist list) { my_clearscrn(); printf("--- FINAL STATUS ---\n"); /* Final list status... */ printf("\nFinal list contents(%d nodes): ", DLISTsize(list)); printf("\nAscending : "); DLISTtraverse(list, print, DLIST_FWD); printf("\nDescending: "); DLISTtraverse(list, print, DLIST_BWD); }
/* --- Function: void sort_list(Slist list) --- */ void sort_list(Slist list) { my_clearscrn(); printf("--- SORT LIST ---\n"); SLISTsort(list, my_cmp); printf("\nCurrent list status(%d nodes, ascending) : ", SLISTsize(list)); SLISTtraverse(list, print, SLIST_FWD); printf("\nCurrent list status(%d nodes, descending): ", SLISTsize(list)); SLISTtraverse(list, print, SLIST_BWD); prompt_and_pause("\n\n"); }
/* --- Function: void print_list(Dlist lst) --- */ void print_list(Dlist lst) { my_clearscrn(); printf("--- PRINT LIST ---\n"); /* Print list status... */ printf("\nCurrent list contents(%d nodes): ", DLISTsize(lst)); printf("\nAscending : "); DLISTtraverse(lst, print, DLIST_FWD); printf("\nDescending: "); DLISTtraverse(lst, print, DLIST_BWD); prompt_and_pause("\n\n"); }
/* --- Function: void final_status(Queue que, Stack stk) --- */ void final_status(Queue que, Stack stk) { /* Final list status... */ my_clearscrn(); printf("--- FINAL STATUS ---\n"); printf("\nFinal list contents: "); printf("\nQueue: "); SLISTtraverse(que, print, SLIST_FWD); printf(" (%d nodes)", QUEUEsize(que)); printf("\nStack: "); SLISTtraverse(stk, print, SLIST_FWD); printf(" (%d nodes)", STACKsize(stk)); }
/* --- Function: void print_queue_stack(Queue que, Stack stk) --- */ void print_queue_stack(Queue que, Stack stk) { my_clearscrn(); printf("--- PRINT QUEUE AND STACK ---\n"); printf("\nCurrent contents: "); printf("\nQueue: "); SLISTtraverse(que, print, SLIST_FWD); printf(" (%d nodes)", QUEUEsize(que)); printf("\nStack: "); SLISTtraverse(stk, print, SLIST_FWD); printf(" (%d nodes)", STACKsize(stk)); prompt_and_pause("\n\n"); }
/* --- Function: void pop_node(Stack stk) --- */ void pop_node(Stack stk) { int tmp, *pi, *ptmp; char mess[BUFSIZ], ans; /* Initialize 'tmp'... */ tmp = 0; do { if (tmp == -1) break; my_clearscrn(); printf("--- POP NODE FROM STACK ---\n"); printf("\nCurrent stack status(%d nodes): ", STACKsize(stk)); SLISTtraverse(stk, print, SLIST_FWD); ptmp = (int *)STACKpeek(stk); if (ptmp == NULL) { prompt_and_pause("\n\nStack is EMPTY - nothing to do..!"); tmp = -1; } else { sprintf(mess, "\nAbout to pop node %d.. - Continue? (y/n): ", *ptmp); ans = read_char(mess, 0, 0, my_chkch); if (ans == 'y' || ans == 'Y') { if ((STACKpop(stk, (void **)&pi)) != OK) { printf("\nFatal error popping data - exiting...!"); STACKdestroy(stk); exit(-1); } else { sprintf(mess, "Node %d will be popped!", *pi); prompt_and_pause(mess); my_destroy(pi); } } else tmp = -1; } } while (TRUE); }
/* --- Function: void dequeue_node(Queue que) --- */ void dequeue_node(Queue que) { int tmp, *pi, *ptmp; char mess[BUFSIZ], ans; /* Initialize 'tmp'... */ tmp = 0; do { if (tmp == -1) break; my_clearscrn(); printf("--- DEQUEUE NODE FROM QUEUE ---\n"); printf("\nCurrent queue status(%d nodes): ", QUEUEsize(que)); SLISTtraverse(que, print, SLIST_FWD); ptmp = (int *)QUEUEpeek(que); if (ptmp == NULL) { prompt_and_pause("\n\nQueue is EMPTY - nothing to do..!"); tmp = -1; } else { sprintf(mess, "\nAbout to dequeue node %d.. - Continue? (y/n): ", *ptmp); ans = read_char(mess, 0, 0, my_chkch); if (ans == 'y' || ans == 'Y') { if ((QUEUEdequeue(que, (void **)&pi)) != OK) { printf("\nFatal error dequeing data - exiting...!"); QUEUEdestroy(que); exit(-1); } else { sprintf(mess, "Node %d will be dequeued!", *pi); prompt_and_pause(mess); my_destroy(pi); } } else tmp = -1; } } while (TRUE); }
/* --- Function: void rem_nodes(Dlist list) --- */ void rem_nodes(Dlist list) { int tmp, *pi, retval; char mess[BUFSIZ]; do { my_clearscrn(); printf("--- REMOVE NODE FROM LIST ---\n"); printf("\nCurrent list status(%d nodes): ", DLISTsize(list)); printf("\nAscending : "); DLISTtraverse(list, print, DLIST_FWD); printf("\nDescending: "); DLISTtraverse(list, print, DLIST_BWD); tmp = read_int("\nEnter keydata for node to be removed (-1=Quit): ", 0, 0); if (tmp == -1) break; /* Remove node - and free memory */ pi = &tmp; if ((retval = DLISTfind_remove(list, (void **)&pi)) != OK) { if (retval == 1) { sprintf(mess, "Error: Node %d not found..!", tmp); prompt_and_pause(mess); } else { if (retval == -2) printf("\nError: Match-callback is missing... - bailing out!"); else printf("\nFatal error... - bailing out!"); DLISTdestroy(list); exit(retval); } } else { /* Removal succesful - notify user.. */ sprintf(mess, "Node %d will be removed..!", *(int *)pi); prompt_and_pause(mess); /* Free node - after being removed from list.. */ my_destroy(pi); } } while (TRUE); }
/* --- Function: void ins_nodes(Dlist list) --- */ void ins_nodes(Dlist list) { int tmp, *pi; DlistNode node; char mess[BUFSIZ]; do { my_clearscrn(); printf("--- ADD NODE WITH DATA=99 - AFTER USER-SPECIFIED NODE ---\n"); printf("\nCurrent list status(%d nodes): ", DLISTsize(list)); printf("\nAscending : "); DLISTtraverse(list, print, DLIST_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 = DLISTfindnode(list, &tmp)) != NULL) /* Node found */ { /* Insert node after first occurance of user-specified node */ pi = (int *)malloc(sizeof(int)); MALCHK(pi); *pi = 99; if ((DLISTinsnext(list, node, pi)) != OK) { printf("\nFatal error - exiting..."); DLISTdestroy(list); exit(-1); } else { sprintf(mess, "Node 99 will be inserted after node %d", *(int *)DLISTdata(node)); prompt_and_pause(mess); } } else { sprintf(mess, "Error: Node %d not found...!", tmp); prompt_and_pause(mess); } } while (TRUE); }
/* --- Function: void insert_nodes(BiTree tree, int nr_of_insertions) --- */ void ins_node(BiTree tree) { int tmp, *pi, retval; char mess[BUFSIZ]; do { my_clearscrn(); printf("--- INSERT NODE ---\n"); print_tree(tree); tmp = read_int("\nEnter integer data for node to be inserted (-1=Quit): ", 0, 0); if (tmp == -1) break; pi = (int *)malloc(sizeof(int)); MALCHK(pi); *pi = tmp; if ((retval = BITREEinsert(tree, pi)) != 0) /* Insertion failed... */ { if (retval == 1) /* Duplicate key value.. */ { sprintf(mess, "Error: Duplicate - node %d already present..!", *pi); prompt_and_pause(mess); my_destroy(pi); /* Free node - since being duplicate.. */ } else { prompt_and_pause("\nFatal error - bailing out..:!\n"); BITREEdestroy(tree); exit(-1); } } else { sprintf(mess, "Node %d will be inserted..", *(int *)pi); prompt_and_pause(mess); } } while (TRUE); }
int main(void) { /* Declare YOUR variables here ! */ int menu_choice, retval; my_clearscrn(); prompt_and_pause(INITIAL_INFO); /* Enter menu loop.. */ do { menu_choice = menu(MAIN_MENU_ROW, 0, 3); switch (menu_choice) { case 1: if ((retval = basics()) != OK) { exit(EXIT_FAILURE); } break; case 2: if ((retval = bfs()) != OK) { exit(EXIT_FAILURE); } break; case 3: if ((retval = dfs()) != OK) { exit(EXIT_FAILURE); } break; default: prompt_and_pause("\nThat's all folks - Bye..!"); break; } } while (menu_choice); return 0; }
/* --- Function: void remove_nodes(BiTree tree, int nr_of_removes) --- */ void rem_node(BiTree tree) { int tmp, *pi, retval; char mess[BUFSIZ]; do { my_clearscrn(); printf("--- REMOVE NODE ---\n"); print_tree(tree); tmp = read_int("\nEnter data for node to be removed (-1=Quit): ", 0, 0); if (tmp == -1) break; pi = &tmp; if ((retval = BITREEremove(tree, (void **)&pi)) != 0) /* Node removal failed.. */ { /* Removal didn't work - node NOT found... */ if (retval == -1) { sprintf(mess, "Error: Node %d not found..!", *(int *)pi); prompt_and_pause(mess); } else /* Serious failure..(-1 or -2) */ { printf("\nFatal failure - bailing out..."); BITREEdestroy(tree); exit(retval); } } else { /* Removal succesful - notify user.. */ sprintf(mess, "Node %d will be removed..!", *(int *)pi); prompt_and_pause(mess); /* Free node - after being removed from tree.. */ my_destroy(pi); } } while (TRUE); }
/* --- Function: void search_node(BiTree tree) --- */ void search_node(BiTree tree) { int tmp, *pi, retval; char mess[BUFSIZ]; do { my_clearscrn(); printf("--- SEARCH NODE ---\n"); print_tree(tree); tmp = read_int("\nEnter data for node to be found (-1=Quit): ", 0, 0); if (tmp == -1) break; pi = &tmp; if ((retval = BITREElookup(tree, (void **)&pi)) != 0) /* Node searching failed.. */ { /* The search didn't work - node NOT found... */ if (retval == -1) { sprintf(mess, "Node %d NOT found..!", *(int *)pi); prompt_and_pause(mess); } else /* Compare-callback not set - or serious failure..(-2) */ { printf("\nCompare-callback not set - or other fatal failure - bailing out..."); BITREEdestroy(tree); exit(retval); } } else { /* Searching succesful - notify user.. */ sprintf(mess, "Node %d FOUND..!", *(int *)pi); prompt_and_pause(mess); } } while (TRUE); }
/* --- Function: void queue_nodess(Queue que, Stack stk, int nr_of_ele) --- */ void enqueue_push_nodes(Queue que, Stack stk, int nr_of_ele) { int i=0, *pi, retval; my_clearscrn(); printf("--- CREATING QUEUE AND STACK (%d nodes each), RANDOM INTEGER DATA ---\n", NR_OF_ITEMS); do { /* Create dyn. memory, store random nr - and enqueue... */ pi = (int *)malloc(sizeof(int)); MALCHK(pi); *pi = rand_int(1,50); retval = QUEUEenqueue(que, pi); assert(retval == OK); /* Create dyn. memory, store random nr - and push... */ pi = (int *)malloc(sizeof(int)); MALCHK(pi); *pi = rand_int(1,50); retval = STACKpush(stk, pi); assert(retval == OK); } while (++i < nr_of_ele); printf("\nCurrent queue and stack status: "); printf("\nQueue(%d nodes): ", QUEUEsize(que)); SLISTtraverse(que, print, SLIST_FWD); printf("\nStack(%d nodes): ", STACKsize(stk)); SLISTtraverse(stk, print, SLIST_FWD); prompt_and_pause("\n\n"); }
/* --- Function: void basics(void) --- */ int basics(void) { Graph gr; VertexNode vnode; EdgeNode enode; char data[STRSIZ], *pdata1, *pdata2; int retval; char mess[MESSIZ]; /* Initialize the graph. */ gr = GRAPHinit(bas_match, free); my_clearscrn(); printf("--- BASIC GRAPH USAGE ---\n\nGraph created.. - let's do some basic graph editing.."); /* --- Perform some graph operations --- */ /* --- Start with inserting some vertices.. --- */ /* Insert first vertex.. */ if ((pdata1 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata1, "a"); sprintf(mess, "\n\nInsert first vertex: %s", pdata1); prompt_and_pause(mess); if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next vertex.. */ if ((pdata1 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata1, "b"); sprintf(mess, "\n\nInsert vertex: %s", pdata1); prompt_and_pause(mess); if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next vertex.. */ if ((pdata1 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata1, "c"); sprintf(mess, "\n\nInsert vertex: %s", pdata1); prompt_and_pause(mess); if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next vertex.. */ if ((pdata1 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata1, "d"); sprintf(mess, "\n\nInsert vertex: %s", pdata1); prompt_and_pause(mess); if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next vertex.. */ if ((pdata1 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata1, "e"); sprintf(mess, "\n\nInsert vertex: %s", pdata1); prompt_and_pause(mess); if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); prompt_and_pause("\n\nCheck out all vertex insertions above.."); /* --- Now, let's insert some edges.. --- */ /* Insert first edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "a"); strcpy(pdata2, "b"); sprintf(mess, "\nNow - let's insert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "a"); strcpy(pdata2, "c"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "b"); strcpy(pdata2, "c"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "b"); strcpy(pdata2, "d"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "c"); strcpy(pdata2, "b"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "c"); strcpy(pdata2, "c"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0)/* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "c"); strcpy(pdata2, "d"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "d"); strcpy(pdata2, "a"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "e"); strcpy(pdata2, "c"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "e"); strcpy(pdata2, "d"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Now, let's remove some edges.. */ /* Remove first edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "a"); strcpy(pdata2, "c"); pdata1 = pdata2; sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHremedge(gr, data, (void **)&pdata1) != 0) /* Do the removal.. */ { free(pdata2); return 1; } free(pdata1); /* Deallocate mem. held by removed edge.. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Remove next edge.. */ strcpy(data, "c"); strcpy(pdata2, "c"); pdata1 = pdata2; sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHremedge(gr, data, (void **)&pdata1) != 0) /* Do the removal.. */ { free(pdata2); return 1; } free(pdata1); /* Deallocate mem. held by removed edge.. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Remove next edge.. */ strcpy(data, "e"); strcpy(pdata2, "c"); pdata1 = pdata2; sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHremedge(gr, data, (void **)&pdata1) != 0) /* Do the removal.. */ { free(pdata2); return 1; } free(pdata1); /* Deallocate mem. held by removed edge.. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Remove next edge.. */ strcpy(data, "a"); strcpy(pdata2, "b"); pdata1 = pdata2; sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHremedge(gr, data, (void **)&pdata1) != 0) /* Do the removal.. */ { free(pdata2); return 1; } free(pdata1); /* Deallocate mem. held by removed edge.. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Remove next edge.. */ strcpy(data, "d"); strcpy(pdata2, "a"); pdata1 = pdata2; sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHremedge(gr, data, (void **)&pdata1) != 0) /* Do the removal.. */ { free(pdata2); return 1; } free(pdata1); /* Deallocate mem. held by removed edge.. */ free(pdata2); /* Deallocate mem. held by search key value(=a).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Now, lets remove a vertex.. */ strcpy(data, "a"); pdata1 = data; sprintf(mess, "\n\nRemove vertex %s", data); prompt_and_pause(mess); if (GRAPHremvertex(gr, (void **)&pdata1) != 0) /* Do the removal.. */ return 1; free(pdata1); /* Deallocate mem. held by removed vertex.. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* --- Do some invalid insertions/removals --- */ /* First invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "f"); strcpy(pdata2, "a"); sprintf(mess,"\n\nNow - try to insert an invalid edge from %s --> %s", data, pdata2); prompt_and_pause(mess); retval = GRAPHinsedge(gr, data, pdata2); /* Do the insertion.. */ if (retval != 0) free(pdata2); /* Deallocte mem. held by search key value(=a).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "c"); strcpy(pdata2, "b"); sprintf(mess,"\nInsert an existing edge from %s --> %s", data, pdata2); prompt_and_pause(mess); retval = GRAPHinsedge(gr, data, pdata2); /* Do the insertion.. */ if (retval != 0) free(pdata2); /* Deallocte mem. held by search key value(=b).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "f"); strcpy(pdata2, "a"); pdata1 = pdata2; retval = GRAPHremedge(gr, data, (void **)&pdata2); /* Do the removal.. */ sprintf(mess, "\nRemove an invalid edge from %s --> %s", data, pdata2); prompt_and_pause(mess); if (retval == 0) { free(pdata2); /* Deallocte mem. held by removed edge.. */ free(pdata1); /* Deallocte mem. held by search key value(=b).. */ return 1; } free(pdata2); /* Deallocte mem. held by search key value(=b).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "c"); strcpy(pdata2, "e"); pdata1 = pdata2; retval = GRAPHremedge(gr, data, (void **)&pdata2); /* Do the removal.. */ sprintf(mess, "\nRemoving an invalid edge from %s --> %s", data, pdata2); prompt_and_pause(mess); if (retval == 0) { free(pdata2); /* Deallocte mem. held by removed edge.. */ free(pdata1); /* Deallocte mem. held by search key value(=e).. */ return 1; } free(pdata2); /* Deallocte mem. held by search key value(=e).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata2, "c"); retval = GRAPHinsvertex(gr, pdata2); /* Do the insertion.. */ sprintf(mess, "\nInsert an existing vertex %s", data); prompt_and_pause(mess); if (retval != 0) /* Insertion failed - which is the case here.. */ free(pdata2); /* Deallocte mem. held by search key value(=c).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata2, "c"); pdata1 = pdata2; retval = GRAPHremvertex(gr, (void **)&pdata2); /* Do the insertion.. */ sprintf(mess, "\nRemove an existing vertex %s", pdata1); prompt_and_pause(mess); if (retval == 0) { free(pdata2); /* Deallocte mem. held by removed vertex.. */ free(pdata1); /* Deallocte mem. held by search key value(=c).. */ return 1; } free(pdata2); /* Deallocte mem. held by search key value(=c).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata2, "d"); pdata1 = pdata2; retval = GRAPHremvertex(gr, (void **)&pdata2); /* Do the insertion.. */ sprintf(mess, "\nRemove an existing vertex %s", pdata1); prompt_and_pause(mess); if (retval == 0) { free(pdata2); /* Deallocte mem. held by removed vertex.. */ free(pdata1); /* Deallocte mem. held by search key value(=d).. */ return 1; } free(pdata2); /* Deallocte mem. held by search key value(=d).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* --- Testing GRAPHis_adjacent() --- */ /* First test of GRAPHis_adjacent().. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "b"); strcpy(pdata2, "d"); retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */ sprintf(mess, "\nTest if (%s, %s) are adjacent...", data, pdata2); prompt_and_pause(mess); /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_testmess(retval); /* Next test of GRAPHis_adjacent().. */ strcpy(data, "a"); strcpy(pdata2, "e"); retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */ sprintf(mess, "\n\nTest if (%s, %s) are adjacent...", data, pdata2); prompt_and_pause(mess); /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_testmess(retval); /* Next test of GRAPHis_adjacent().. */ strcpy(data, "e"); strcpy(pdata2, "d"); retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */ sprintf(mess, "\n\nTest if (%s, %s) are adjacent...", data, pdata2); prompt_and_pause(mess); /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_testmess(retval); /* Next test of GRAPHis_adjacent().. */ strcpy(data, "c"); strcpy(pdata2, "a"); retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */ sprintf(mess, "\n\nTest if (%s, %s) are adjacent...", data, pdata2); prompt_and_pause(mess); /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_testmess(retval); /* Free temporary dyn. memory - no longer used.. */ free(pdata2); /* Print all adjacent vertices - to a certain vertex.. */ strcpy(data, "c"); sprintf(mess, "\n\nFinally - print vertices adjacent to %s: ", data); prompt_and_pause(mess); vnode = GRAPHfindvertex(gr, data); printf("\nVertices adjacent(=incident) from %s (%d pcs): ", data, GRAPHgetedgecount(vnode)); for (enode = GRAPHgetedgehead(vnode); enode != NULL; enode = GRAPHgetedgenext(enode)) { printf("%s ", (char *)GRAPHgetedgedata(enode)); } /* Destroy the graph. */ sprintf(mess, "\n\nTime to tidy up.."); prompt_and_pause(mess); GRAPHdestroy(gr); return OK; }
int main(void) { /* Declare YOUR variables here ! */ Stack mystk; Queue myqueue; char mess[BUFSIZ]; int i, nr; srand((unsigned int)time(NULL)); my_clearscrn(); printf("--- INITIALIZING A QUEUE, %d ELEMENTS, RANDOM INTEGER DATA ---", NR_OF_ITEMS); if ((myqueue = QUEUEinit(my_destroy)) == NULL) /* Initialize the queue... */ { printf("\nFatal error - bailing out...!"); exit(-1); } queue_elements(myqueue, NR_OF_ITEMS); /* Populate the queue... */ nr = QUEUEsize(myqueue)/2; /* Save half the size of the queue... */ sprintf(mess, "\nNext - let's DEQUEUE %d elements from our queue...", nr); prompt_and_pause(mess); prompt_and_pause("...and now PUSH them - on a brand, new STACK...!!"); if ((mystk = STACKinit(my_destroy)) == NULL) /* Set up a new stack... */ { printf("\nFatal error - bailing out...!"); exit(-1); } for (i = 0; i < nr; ++i) { void *piq, *pis; int retval; retval = QUEUEdequeue(myqueue, &piq); assert(retval == OK); sprintf(mess, "QUEUE: Dequeued: %02d (new frontvalue: %02d)", *(int *)piq, *(int *)QUEUEpeek(myqueue)); prompt_and_pause(mess); /* Check current stack top... */ pis = STACKpeek(mystk); /* Push the value just dequeued - from our queue... */ retval = STACKpush(mystk, piq); assert(retval == OK); if (pis == NULL) /* If this is the FIRST stack push... */ sprintf(mess, "STACK: Pushed : %02d (old stacktop : none)", *(int *)STACKpeek(mystk)); else sprintf(mess, "STACK: Pushed : %02d (old stacktop : %02d)", *(int *)STACKpeek(mystk), *(int *)pis); /* Print the message assembled above... */ prompt_and_pause(mess); } printf("\n--- CURRENT STATUS OF STACK AND QUEUE ---"); printf("\nStack: "); SLISTtraverse(mystk, print, SLIST_FWD); printf("\nQueue: "); SLISTtraverse(myqueue, print, SLIST_FWD); prompt_and_pause("\n\nLet's tidy up (destroy queue/stack) - Bye!"); SLISTdestroy(mystk); SLISTdestroy(myqueue); return 0; }