/* --- 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 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); }
int CHTBLinsert(CHtbl htbl, const void *data) { int bucket, retval; void *tmp; tmp = (void *)data; if (CHTBLlookup(htbl, &tmp) == 0) return 1; /* Hash the key */ bucket = htbl->h(data) % htbl->buckets; if ((retval = SLISTinsnext(htbl->table[bucket], NULL, data)) == 0) htbl->size++; return retval; }