int main(void) { int n; printf("Enter the seize of stack: "); scanf("%d", &n); STACKinit(n); STACKpush(1); STACKpush(2); printf("%d, %d\n", STACKpop(), STACKpop()); printf("Stack empty? %d\n", STACKempty()); return 0; }
void CRUELstart() { int i, j, k; list ll = LISTinit(), q; /* inicializa as 4 pilhas de naipes */ for( i = 0; i < 4; i++ ) { suitStack[i] = STACKinit( 13 ); LISTaddStart( ll, CARDcreate( i, 1 ) ); } /* aleatoriza a ordem das 4 pilhas de naipes */ for( i = 0; i < 4; i++ ) { k = rand() % (4 - i); q = LISTgetNterm( ll, k ); STACKpush( &suitStack[i], LISTgetVal(q) ); suitPosition[ CARDsuit(LISTgetVal(q)) ] = i; LISTremove(q); } /* inicializa as 12 pilhas de cartas */ for( i = 0; i < 12; i++ ) cardStack[i] = STACKinit( 26 ); /* coloca as 48 cartas na lista ligada */ for( j = 0; j < 4; j++ ) { for( i = 2; i <= 13; i++ ) { LISTaddStart( ll, CARDcreate( j, i ) ); } } /* preechemos as 12 pilhas de cartas linearmente, mas com cartas escolhidas aleatoriamente */ for( i = 0; i < 12; i++ ) { for( j = 0; j < 4; j++ ) { k = rand() % (48 - (4 * i + j) ); q = LISTgetNterm( ll, k ); STACKpush( &cardStack[i], LISTgetVal(q) ); LISTremove(q); } } LISTdestroy(ll); /* Selecionar uma carta aleatoria. Como funciona: Coloca-se todas as cartas numa LL. Sempre que precisar de uma carta aleatoria, fazer k = rand() % TOTAL_DE_CARTAS_NA_LL e pegar a k-esima carta da LL, removendo-a da lista. */ cardLeft = 48; movedSinceDist = 0; lastSugest = ACTIONcreate(-1,-1,-1); }
int main(void) { int n=4; STACKinit(n); STACKpush(0); STACKpush(1); STACKpush(2); STACKpush(2); STACKpush(2); printf("%d\n", STACKpop()); printf("%d\n", STACKpop()); printf("%d\n", STACKpop()); return 0; }
int main(int argc, char *argv[]) { STACKinit(N); STACKpush(1); STACKpush(2); STACKpush(3); STACKpush(2); STACKpush(4); printf("Popped %i\n", STACKpop()); printf("Popped %i\n", STACKpop()); printf("Popped %i\n", STACKpop()); printf("Popped %i\n", STACKpop()); return 0; }
/* --- 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 CRUELredistribute() { list ll = LISTinit(), q; int j = 0, i = 0; while( i < 12 ) { if( STACKempty( cardStack[i] ) ) i++; else LISTaddEnd( ll, STACKpop( &cardStack[i] ) ); } i = 0; while( !LISTempty( ll ) ) { if( j >= 4 ) { j = 0; i++; } q = LISTgetNterm( ll, 3 - j ); if( q ) { STACKpush( &cardStack[i], LISTgetVal( q ) ); LISTremove(q); } j++; } LISTdestroy(ll); movedSinceDist = 0; lastSugest = ACTIONcreate(-1,-1,-1); }
int main(int argc, char *argv[]) { char *e = NULL; int i, j, len, temp; STACKinit(argc-1); for (j=1; j<argc; j++) { e = argv[j]; len = strlen(e); for (i=0; i<len; i++) { if (e[i] =='+') { STACKpush(STACKpop() + STACKpop()); } if (e[i] == '-') { STACKpush((-1) * (STACKpop() - STACKpop())); } if (e[i] == '*') { STACKpush(STACKpop() * STACKpop()); } if (e[i]>='0' && e[i]<='9') { temp = 0; while (e[i]>='0' && e[i]<='9') { temp = temp*10 + (e[i]- '0'); i++; } STACKpush(temp); } } } printf("%d\n", STACKpop()); STACKfree(); free(e); e = NULL; return 0; }
void traverseTree(tnode h, void (*visit)(tnode)) { STACKinit(); STACKpush(h); while (STACKcount() > 0) { h = STACKpop(); if (h->l != NULL) STACKpush(h->l); // In-order Traversal (*visit)(h); if (h->r != NULL) STACKpush(h->r); } }
int main(void) { int i = 0, N = 0; Item tmp; char *a = (char *)malloc(Nmax * sizeof(char)); printf("Enter a postfix expression: \n"); while ((a[i++] = getchar()) != '\n'); N = i - 1; STACKinit(N); for (i = 0; i < N; i++) { if (a[i] == '+') { STACKpush(POLYadd(STACKpop(), STACKpop())); continue;// 1 } if (a[i] == '*') { STACKpush(POLYadd(STACKpop(), STACKpop())); continue;// 2 } /*if (a[i] == '-') { tmp = STACKpop(); STACKpush(STACKpop() - tmp); continue;// 1 } if (a[i] == '/') { tmp = STACKpop(); STACKpush(STACKpop() / tmp); continue;// 1 }*/ if ((a[i] >= '0') && (a[i] <= '9'))// 以下三行 把相邻的数字字符组合成十进制整数 STACKpush(POLYterm(0, 0)); while ((a[i] >= '0') && (a[i] <= '9')) STACKpush(POLYadd(POLYmult(STACKpop(), POLYterm(1, 1)), POLYterm((a[i++] - '0'), 0))); if (a[i] != ' ') i--; // 3 //1, 2, 3 使得+*和数字之间没有空格也能运行正确 } printf("%d \n", STACKpop()); return 0; }
void traverse(link h, void (*visit)(link)) { STACKinit(max); STACKpush(h); while (!STACKempty()) { (*visit)(h = STACKpop()); if(h->r != NULL) { STACKpush(h->r); } if(h->l != NULL) { STACKpush(h->l); } } }
void traverse(int k, void(*visit)(int)) { link t; STACKinit(); STACKpush(k); while (STACKcount() > 0) { k = STACKpop(); if (visited[k] == 0) { (*visit)(k); visited[k] = 1; for (t = adj[k]; t != NULL; t = t->next) { if (visited[t->v] == 0) { STACKpush(t->v); } } } } }
int main(int argc, char *argv[]){ char *a = argv[1]; int i, N = strlen(a); STACKinit(N); for (i = 0; i < N; i++){ switch( a[i] ){ case '+': STACKpush(STACKpop()+STACKpop()); break; case '*': STACKpush(STACKpop()*STACKpop()); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': STACKpush(0); while ( isdigit(a[i]) ) STACKpush(10*STACKpop() + (a[i++]-'0')); break; } } printf("%d \n", STACKpop()); return 0; }
int path(Graph g, int v) { int w; for( ; g->adj[v] != NULL; v=w){ STACKpush(v); w = g->adj[v]->v; GRAPHremoveE(g,EDGE(v,w)); } return v; }
int CRUELaction(action a) { card c; stack *from = &(cardStack[a.from]), *to = ( a.suit == 0 ) ? &(suitStack[a.to]) : &(cardStack[a.to]); c = STACKpop( from ); STACKpush(to, c); if( a.suit == 0 ) cardLeft--; movedSinceDist = 1; lastSugest = ACTIONcreate(-1,-1,-1); return 1; }
int main(int argc, char *argv[]) { char *a = argv[1]; printf("a = %s\n", a); int i, N = strlen(a); STACKinit(N); for (i = 0; i < N; ++i) { if (a[i] == ')') printf("%c ", STACKpop()); if ((a[i] == '+') || (a[i] == '*')) STACKpush(a[i]); if ((a[i] >= '0') || (a[i] <= '9')) printf("%c ", a[i]); } printf("\n"); return 0; }
void traverse(link h, void (*visit)(link)) { STACKinit(MAX); while (!STACKempty() || h != NULL) { if (h != NULL) { STACKpush(h); h = h->l; } else { (*visit) (h = STACKpop()); h = h->r; } } }
/* --- 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 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); }
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; }
void cpyR(S s, link a){ if(a==NULL) return; cpyR(s, a->next); STACKpush(s, a->item); return; }
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; }
int main (int argc, char * argv[]) { GRAPH graph; FILE * finput; symboltable table; EDGE tmpedge; ITEM * items; finput=fopen(argv[1], "r"); int V; fscanf(finput, "%d", &V); graph=GRAPHinit(V); table=HASHinit(V); items=malloc(V*sizeof(ITEM)); int i; for(i=0; i<V; i++) items[i]=NULL; i=0; char v1[10+1]; char v2[10+1]; while(fscanf(finput, "%s %s", v1, v2)==2) { if(HASHreturn(table, v1)==-1) { items[i]=ITEMinit(v1); HASHinsert(table, items[i], i); i++; } if(HASHreturn(table, v2)==-1) { items[i]=ITEMinit(v2); HASHinsert(table, items[i], i); i++; } tmpedge=EDGEadd(HASHreturn(table, v1), HASHreturn(table, v2)); GRAPHinsertE(graph, tmpedge); } int * solution; int cmd, exit=0, path, v, w, j, k; while(!exit) { i=1; printf("\n"); printf("%d Caclolare il camino semplice di lunghezza minima tra due vertici\n", i++); printf("%d Caclolare il camino semplice di lunghezza massima tra due vertici\n", i++); printf("%d Caclolare il numero di camini semplici tra due vertici\n", i++); printf("%d Calcolare le componenti fortemente connesse al grafo\n", i++); printf("%d Individuare un possibile sottoinsieme di archi per ottenere un grafo fortemente connesso\n", i++); printf("%d Esci\n--> ", i++); scanf("%d", &cmd); switch(cmd) { case 1: printf("Inserire il nome dei due nodi separati da spazio: "); scanf("%s %s", v1, v2); path=0; v=HASHreturn(table, v1); w=HASHreturn(table, v2); solution = optimalmin(graph, v, w, V); printf("Nodi attraversati:\n"); ITEMshow(items[v], stdout); while(v!=w) { printf(" -> "); v=solution[v]; ITEMshow(items[v], stdout); path++; } printf("\nLa lunghezza è %d\n", path); break; case 2: printf("Inserire il nome dei due nodi separati da spazio: "); scanf("%s %s", v1, v2); path=0; v=HASHreturn(table, v1); w=HASHreturn(table, v2); solution = optimalmax(graph, v, w, V); printf("Nodi attraversati:\n"); ITEMshow(items[v], stdout); while(v!=w) { printf(" -> "); v=solution[v]; ITEMshow(items[v], stdout); path++; } printf("\nLa lunghezza è %d\n", path); break; case 3: printf("Inserire il nome dei due nodi separati da spazio: "); scanf("%s %s", v1, v2); printf("Il numero dei cammini e' %d\n", npath(graph, HASHreturn(table, v1), HASHreturn(table, v2), V)); break; case 4: k=0; for(i=0; i<V; i++) for(j=i+1; j<V; j++) { if(GRAPHpath(graph, i, j) && GRAPHpath(graph, j, i)) { k++; ITEMshow(items[i], stdout); printf(" e' fortemente connesso con "); ITEMshow(items[j], stdout); printf("\n"); } } printf("Ci sono %d componenti fortemente connesse\n", k); break; case 5: k=0; STACK additionaledges=STACKinit(); for(i=0; i<V; i++) for(j=i+1; j<V; j++) { int ab=GRAPHpath(graph, i, j); int ba=GRAPHpath(graph, j, i); if(!(ab && ba)) { if(!ab) { tmpedge = EDGEadd(i, j); STACKpush(additionaledges, tmpedge); GRAPHinsertE(graph, tmpedge); } if(!ba) { tmpedge = EDGEadd(j, i); STACKpush(additionaledges, tmpedge); GRAPHinsertE(graph, tmpedge); } } } while(STACKcardinality(additionaledges)) { tmpedge=STACKpop(additionaledges); ITEMshow(items[tmpedge.v], stdout); printf(" -> "); ITEMshow(items[tmpedge.w], stdout); printf("\n"); } break; case 6: exit=1; } } return 0; }