/* --- 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); }
void Except( Pilha p ) { LL linkA, linkB, linkR; if ( STACKsize( p ) >= 2 ) { linkA = STACKget(p); linkB = STACKget(p); linkR = Intersection( linkA, linkB ); STACKput( p, linkR ); } }
void Plus( Pilha p ) { LL linkA, linkB, linkR; if ( STACKsize( p ) >= 2 ) { linkA = STACKget(p); linkB = STACKget(p); linkR = Junction( linkA, linkB ); STACKput( p, linkR ); } }
void Diff( Pilha p ) { LL linkA, linkB, linkR; if ( STACKsize( p ) >= 2 ) { linkA = STACKget(p); linkB = STACKget(p); linkR = Difference( linkA, linkB ); STACKput( p, linkR ); } }
/* --- 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 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"); }
void STACKcpy(S b, S a){ while(STACKsize(b)!=0) STACKpop(b); cpyR(b, a->top); }
int main(int argc, char ** argv) { item it; int i, s; short **tab; if( argc != 2 ) return 2; N = atoi(argv[1]); resp = STACKinit(N); queen = malloc( N * sizeof(*queen) ); if( !queen ) exit(1); curX = curY = 0; printf("Hello there, and welcome to another backtracking program! I hope you enjoy your stay!\n" "We'll be trying to fit %d queens in a %dx%d board.\n", N, N, N); while( STACKsize( resp ) < N ) { s = STACKsize( resp ); it = ITEMcreate(curX, curY); /*printf("Queens: %d | Pos = ", STACKsize( resp ) ); ITEMprint(it); printf("| "); STACKdump( resp );*/ for(i = 0; i < s && !QUEEN_CONFLICT( queen[i], it ); i++); if( i == s ) { /* achou mais uma rainha */ queen[i] = it; STACKpush( resp, it ); curX = curY = 0; s++; } else { /* posicao invalida */ invalid: curX++; if( curX == N ) { curX = 0; curY++; if( curY == N ) { /* F**K, BACKTRACK! */ if( STACKempty( resp ) ) { break; /* tabuleiro imposivel! */ } else if( s < N ){ it = STACKpop( resp ); s--; curX = ITEMkey(it); curY = ITEMval(it); goto invalid; } } } } } if( s < N ) { printf("We're deeply sorry, but it's impossible to fit %d queens in a %dx%d board.\n", N, N, N); } else { tab = malloc( N * sizeof( *tab ) ); if( !tab ) exit(1); for( i = 0; i < N; i++ ) { tab[i] = malloc( N * sizeof( **tab ) ); if( !tab[i] ) exit(1); for( s = 0; s < N; s++ ) tab[i][s] = 0; } while( !STACKempty( resp ) ) { it = STACKpop( resp ); tab[ITEMkey(it)][ITEMval(it)] = 1; } printf("\n"); for( s = 0; s < N; s++ ) { for( i = 0; i < N; i++ ) printf("%c ", tab[i][s] + '0'); printf("\n"); } } return 0; }
/* --- Function: void dequeue_push_node(Queue que, Stack stk) --- */ void dequeue_push_node(Queue que, Stack stk) { int tmp, *pi, *ptmp; char mess[BUFSIZ], ans; /* Initialize 'tmp'... */ tmp = 0; do { if (tmp == -1) break; my_clearscrn(); printf("--- DEQUEUE NODE FROM QUEUE AND PUSH ON STACK ---\n"); printf("\nCurrent queue/stack status: "); printf("\nQueue: "); SLISTtraverse(que, print, SLIST_FWD); printf(" (%d nodes)", QUEUEsize(que)); printf("\nStack: "); SLISTtraverse(stk, print, SLIST_FWD); printf(" (%d nodes)", STACKsize(stk)); 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 - and push it on stack.. - 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 { if (STACKpush(stk, pi) != OK) { printf("\nFatal error pushing data - exiting...!"); STACKdestroy(stk); exit(-1); } sprintf(mess, "Node %d will be dequeued - and pushed on stack..!", *pi); prompt_and_pause(mess); } } else tmp = -1; } } while (TRUE); }