/* --- 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 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"); }
/* --- 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 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); }
/* --- 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"); }
void SLISTdestroy(Slist list) { void *data; while (SLISTsize(list) > 0) { if (SLISTremnext(list, NULL, (void **)&data) == 0 && list->destroy != NULL) list->destroy(data); } free(list); }
/* --- Function: void rem_nodes(Slist list) --- */ void rem_nodes(Slist list) { int tmp, *pi, retval; char mess[BUFSIZ]; do { my_clearscrn(); printf("--- REMOVE NODE FROM LIST ---\n"); printf("\nCurrent list status(%d nodes): ", SLISTsize(list)); SLISTtraverse(list, print, SLIST_FWD); 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 = SLISTfind_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!"); SLISTdestroy(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(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); }