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 CRUELfree() { int i; for( i = 0; i < 4; i++ ) while( !STACKempty(suitStack[i]) ) CARDremove( STACKpop( &(suitStack[i]) ) ); for( i = 0; i < 12; i++ ) while( !STACKempty(cardStack[i]) ) CARDremove( STACKpop( &(cardStack[i]) ) ); /* Como funciona: Para cada pilha, de naipe e de carta, desempilha uma carta e a remove. */ }
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; }
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; }
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); }
void pathEshow(Graph g, int v, int w) { STACKinit(g->e); printf("%d",w); while((path(g,v) == v) && !STACKempty()){ v = STACKpop(); printf("-%d",v); } printf("\n"); }
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; }
/* --- 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); }
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 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); } }
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); } } }
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 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; } } }
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(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; }
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 main(int argc, char *argv[]) { int next_option; const char *const short_options = "hvad:i:mn:w:o:"; const struct option long_options[] = { { "help", 0, NULL, 'h' }, { "verbose", 0, NULL, 'v' }, { "all", 0, NULL, 'a' }, { "display", 1, NULL, 'd' }, { "id", 1, NULL, 'i' }, { "mouse", 0, NULL, 'm' }, { "name", 1, NULL, 'n' }, { "wid", 1, NULL, 'w' }, { "opacity", 1, NULL, 'o' }, { NULL, 0, NULL, 0 } }; int verbose = 0; program_name = argv[0]; double vopacity; unsigned int opacity; char *display_name = NULL; char *window_name = NULL; pid_t pid = 0; unsigned use_mouse = 1; unsigned all = 0; Window wid = 0; unsigned char *data = NULL; Atom actual; int format; unsigned long n, left; do { next_option = getopt_long(argc, argv, short_options, long_options, NULL); switch (next_option) { case 'h': print_usage(stdout, 0); case 'v': verbose = 1; break; case 'd': display_name = optarg; break; case 'i': pid = atoi(optarg); use_mouse = 0; break; case 'm': use_mouse = 1; break; case 'n': window_name = optarg; use_mouse = 0; break; case 'w': wid = atoi(optarg); use_mouse = 0; break; case 'o': vopacity = atof(optarg); opacity = 1; break; case 'a': all = 1; pid = 0; wid = 0; window_name = NULL; break; case '?': print_usage(stderr, 1); case -1: break; default: abort(); } } while (next_option != -1); if (verbose) SetDebug(1); Display *dpy = Open_Display (display_name); unsigned screen = DefaultScreen(dpy); if (opacity) { opacity = (unsigned int) (vopacity * OPAQUE); } else { XGetWindowProperty(dpy, target_win, XInternAtom(dpy, OPACITY, False), 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left, (unsigned char **) &data); if (data != NULL) { memcpy (&opacity, data, sizeof (unsigned int)); XFree(( void *) data ); if (verbose) fprintf(stdout, "Found property: %g\n", (double) opacity / OPAQUE); } else opacity = OPAQUE; if (opacity != OPAQUE) opacity = OPAQUE; else opacity = 0xc0000000; } if (verbose) fprintf(stdout, "opacity 0x%x\n", opacity); if (!all) { if (use_mouse) { if(verbose) fprintf(stdout, "Using mouse for selection.\n"); target_win = Select_Window_by_Mouse(dpy); if(verbose) fprintf(stdout, "Window id : %lu(0x%lx)\n", target_win, target_win); } else if (wid) { if(verbose) fprintf(stdout, "Using window id for selection.\n"); target_win = wid; } else if (pid) { if(verbose) fprintf(stdout, "Using process id for selection.\n"); target_win = Get_Window_by_PID(dpy, pid); if(target_win) target_win = Get_Top_Window(dpy, target_win); if(verbose) fprintf(stdout, "Window id : %lu(0x%lx)\n", target_win, target_win); } else if (window_name != NULL) { if(verbose) fprintf(stdout, "Using window name for selection.\n"); target_win = Get_Window_by_Name(dpy, DefaultRootWindow(dpy), window_name); if(target_win) target_win = Get_Top_Window(dpy, target_win); if(verbose) fprintf(stdout, "Window id : %lu(0x%lx)\n", target_win, target_win); } if ((opacity == OPAQUE)&&(target_win != 0)) XDeleteProperty (dpy, target_win, XInternAtom(dpy, OPACITY, False)); else if (target_win != 0) XChangeProperty(dpy, target_win, XInternAtom(dpy, OPACITY, False), XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &opacity, 1L); } if (all) { Get_All_Windows(dpy); while(!STACKempty()) { if (opacity == OPAQUE) XDeleteProperty (dpy, STACKpop(), XInternAtom(dpy, OPACITY, False)); else XChangeProperty(dpy, STACKpop(), XInternAtom(dpy, OPACITY, False), XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &opacity, 1L); } } XSync(dpy, False); if(verbose) fprintf(stdout, "Set Property to %g\n", (double) opacity / OPAQUE); return EXIT_SUCCESS; }
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; }
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; }