element delq(){ if(front==rear){ queueempty(); exit(EXIT_FAILURE); } return que[++front]; }
void construct_failure(struct gto *g) { int i; struct queue *q; g->failure = malloc(g->ary_len * sizeof(int)); for (i = 0; i < g->ary_len; ++i) g->failure[i] = 0; q = queueinit(); for (i = 0; i < 128; ++i) { int s = g->ary[0][i]; if (0 != s) { enqueue(q, s); g->failure[s] = 0; } } while (!queueempty(q)) { int a; int r = dequeue(q); for (a = 0; a < 128; ++a) { int s = g->ary[r][a]; if (FAIL != s) { int state; struct output_extent *p; enqueue(q, s); state = g->failure[r]; while (FAIL == g->ary[state][a]) state = g->failure[state]; g->failure[s] = g->ary[state][a]; /* output(s) <- output(s) U output(f(s)) */ p = &g->output[g->failure[s]]; for (i = 0; i < p->len; ++i) set_output_length(g, s, p->out[i]); } } } queuedestroy(q); }
void floortraverse(TREE T){ TREE p; LinkQueue Q; initqueue(&Q);p=T; inqueue(&Q,p); while(queueempty(Q)){ outqueue(&Q,&p); print(p->data); if(p->left) inqueue(&Q,p->left); if(p->right) inqueue(&Q,p->right); } }
void * getqueue(MessageQue * pMsgQue) { void *pTemp = NULL; int apiretval = 0; apiretval = pthread_mutex_lock(&pMsgQue->queLock); if (0 != apiretval) { return pTemp; } while(queueempty(pMsgQue->pQueue)) { pthread_cond_wait(&(pMsgQue->queCond),&(pMsgQue->queLock)); } pTemp = dequeue(pMsgQue->pQueue); pthread_mutex_unlock(&pMsgQue->queLock); return (pTemp); }
void construct_delta(struct gto *g) { struct queue *q; int i, a; g->delta = malloc(sizeof(int *)*g->ary_len); g->delta[0] = malloc(sizeof(int)*g->ary_len*128); memset(g->delta[0], 0, sizeof(int)*g->ary_len*128); for (i = 0; i < g->ary_len; ++i) g->delta[i] = g->delta[0] + i*128; q = queueinit(); for (a = 0; a < 128; ++a) { g->delta[0][a] = g->ary[0][a]; if (0 != g->ary[0][a]) enqueue(q, g->ary[0][a]); } while (!queueempty(q)) { int r = dequeue(q); for (a = 0; a < 128; ++a) { int s = g->ary[r][a]; if (FAIL != s) { enqueue(q, s); g->delta[r][a] = s; } else g->delta[r][a] = g->delta[g->failure[r]][a]; } } queuedestroy(q); }
void outdegree2(TREE T){ TREE p; LinkQueue Q; int flag1=0,flag2=0,n0=0,n1=0,n2=0; initqueue(&Q);p=T; inqueue(&Q,p); while(queueempty(Q)){ outqueue(&Q,&p); if(p->left) {inqueue(&Q,p->left);flag1=1;} if(p->right){inqueue(&Q,p->right);flag2=1;} if(flag1&&flag2) n2++; else if(!flag1&&!flag2) n0++; else n1++; flag1=flag2=0; } printf("出度为0的节点数目:%d\n",n0); printf("出度为1的节点数目:%d\n",n1); printf("出度为2的节点数目:%d\n",n2); }
int main(void){ #define BUFSIZE (N*2) char buf[BUFSIZE]; char maze[N][N]; int i, j, sx, sy, gx, gy; struct cost pos; struct queue hoge; struct cost tsugi; for (i=0; i<N; i++){ fgets(buf, BUFSIZE, stdin); for (j=0; j<N && (buf[j] != '\n' && buf[j] != '\0'); j++){ if(buf[j] == 'S'){ sx = j; sy = i; buf[j] = ' '; }else if(buf[j] == 'G'){ gx = j; gy = i; buf[j] = ' '; } maze[i][j] = buf[j]; } while(j<N) maze[i][j++] = ' '; } initqueue(&hoge); pos.x = sx; pos.y = sy; pos.c = 0; putq(&hoge, pos); while(!queueempty(&hoge)){ pos = getq(&hoge); if (pos.c == N * N){ pos.c = -1; break; } if (pos.x == gx && pos.y == gy){ break; } maze[pos.y][pos.x] = '*'; if (maze[pos.y][pos.x - 1] == ' '){ tsugi.x = pos.x - 1; tsugi.y = pos.y; tsugi.c = pos.c + 1; putq(&hoge, tsugi); } if (maze[pos.y][pos.x + 1] == ' '){ tsugi.x = pos.x + 1; tsugi.y = pos.y; tsugi.c = pos.c + 1; putq(&hoge, tsugi); } if (maze[pos.y + 1][pos.x] == ' '){ tsugi.x = pos.x; tsugi.y = pos.y + 1; tsugi.c = pos.c + 1; putq(&hoge, tsugi); } if(maze[pos.y - 1][pos.x] == ' '){ tsugi.x = pos.x; tsugi.y = pos.y - 1; tsugi.c = pos.c + 1; putq(&hoge, tsugi); } } if (pos.x != gx || pos.y != gy){ pos.c = -1; } printf("%d\n", pos.c); return 0; }
int main(void) { char buf[BUFSIZE]; char canvas[N][N]; int i,j; elemtype point,pos; struct queue que; initqueue(&que); for (i = 0; i < N; i++) { fgets(buf, BUFSIZE, stdin); for (j = 0; j < N && (buf[j] != '\n' && buf[j] != '\0'); j++) canvas[i][j] = buf[j]; while (j < N) canvas[i][j++] = ' '; } point.x=point.y=N/2; canvas[point.y][point.x]=C; enqueue(&que,point); while(!queueempty(&que)){ point=dequeue(&que); if(point.y!=0){ if(canvas[point.y-1][point.x]==' '){ pos.x=point.x; pos.y=point.y-1; enqueue(&que,pos); canvas[pos.y][pos.x]=C; } } if(point.x!=0){ if(canvas[point.y][point.x-1]==' '){ pos.x=point.x-1; pos.y=point.y; enqueue(&que,pos); canvas[pos.y][pos.x]=C; } } if(point.y!=N-1){ if(canvas[point.y+1][point.x]==' '){ pos.x=point.x; pos.y=point.y+1; enqueue(&que,pos); canvas[pos.y][pos.x]=C; } } if(point.x!=N-1){ if(canvas[point.y][point.x+1]==' '){ pos.x=point.x+1; pos.y=point.y; enqueue(&que,pos); canvas[pos.y][pos.x]=C; } } } for(i=0;i<N;i++){ for(j=0;j<N;j++)printf("%c",canvas[i][j]); printf("\n"); } return 0; }
BOOL Regen(const char *Device, int IsCacheOrJukeBox, char *SubDir) { int itemp, i, j, CDNumber, JUKEBOXNumber; Database DB; DIR *dird; dirent *diren; char PathString[1024], TempPath[1024], FileString[1024]; char ActualDevice[256]; char *p; if (!DB.Open ( DataSource, UserName, Password, DataHost ) ) { fprintf(stderr, "Error Connecting to SQL\n"); return ( FALSE ); } // convert device specification to a path GetPhysicalDevice(Device, PathString); itemp = strlen(PathString); if(!itemp) return FALSE; // strip the last subdirectory (e.g., CD0_%04d) for regen of complete CACHE or JUKEBOX devices if (IsCacheOrJukeBox) { PathString[itemp-1]=0; p = strrchr(PathString, PATHSEPCHAR); if (p) p[0]=0; OperatorConsole.printf("Regen directory = %s\n", PathString); itemp = strlen(PathString); if(!itemp) return (FALSE); } // add a '/' to end of directory name if not yet present if(PathString[itemp-1] != PATHSEPCHAR) strcat(PathString, PATHSEPSTR); // PathString now contains Image file storage root directory path // Regen all files and subdirectories below that or only the selected subdirectory if (!IsCacheOrJukeBox) { if (SubDir) { strcat(PathString, SubDir); strcat(PathString, PATHSEPSTR); } return RegenDir(&DB, PathString, Device); } // remaining code is for cache or jukebox devices strcpy(TempPath, PathString); TempPath[strlen(TempPath)-1]=0; dird = opendir(TempPath); if(dird == NULL) return ( FALSE ); while ( TRUE ) { diren = readdir(dird); if (!diren) break; // traverse one level for CD or cache directories (CDs to be burned) if (strcmp(diren->d_name, ".") != 0 && strcmp(diren->d_name, "..") != 0 ) { strcpy(FileString, diren->d_name); // Decompose jukebox directory name to find CD number // Directory name must be like aaaCCCC where C=CD number a=non-number if(strnicmp(Device, "JUKEBOX", 7)==0) { CDNumber = 0; for (i=strlen(FileString)-1; i>=0; i--) { if (FileString[i] < '0' || FileString[i] > '9') { CDNumber = atoi(FileString + i + 1); break; } } sprintf(ActualDevice, "%s.%d", Device, CDNumber); } // Decompose cache directory name to find CD number and jukebox number // Directory name must be like aaaJJaCCCC where C=CD# J=jukebox# a=non-number if(strnicmp(Device, "CACHE", 5)==0) { CDNumber = 0; JUKEBOXNumber = 0; for (i=strlen(FileString)-1; i>=0; i--) { if (FileString[i] < '0' || FileString[i] > '9') { CDNumber = atoi(FileString + i + 1); for (j=i-1; j>=0; j--) { if (FileString[j] < '0' || FileString[j] > '9') { JUKEBOXNumber = atoi(FileString + j + 1); break; } } break; } } sprintf(ActualDevice, "%s.%d.%d", Device, JUKEBOXNumber, CDNumber); } strcpy(TempPath, PathString); strcat(TempPath, FileString); strcat(TempPath, PATHSEPSTR); RegenDir(&DB, TempPath, ActualDevice); } } closedir(dird); #ifdef MULTITHREAD while(!queueempty()) sleep(1); #endif return ( TRUE ); }