//检查点B是否应加入队列(点B是由pqp决定的点A的dir方向) static int check_exit_node(pLinkList pList, McMap *mcMap, int i, int j, int dir) { unsigned short *p1, *p2; QueuePoint *tmp; int exits[4][4] = { {NODE_LEFT, NODE_RIGHT, 0, -1}, {NODE_RIGHT, NODE_LEFT, 0, +1}, {NODE_UP, NODE_DOWN, -1, 0}, {NODE_DOWN, NODE_UP, +1, 0} }; p1 = &(mcMap->map[i][j].exit); if (*p1 & exits[dir][0]) { //如果有A出口 p2 = &((mcMap->map[i + exits[dir][2]][j + exits[dir][3]]).exit); *p2 |= exits[dir][1]; //A出口点必定有_A出口点 if (InQueue(*p2)) //如果A出口点已经在队列中 return 0; tmp = calloc(1, sizeof (QueuePoint)); if (tmp == NULL) return -1; tmp->i = i + exits[dir][2]; tmp->j = j + exits[dir][3]; insert_node(pList, tmp); //将出口点加入队列 *p2 |= NODE_QUEUED; //标记处理 } return 0; }
int main() { queue q; initqueue(&q); showqueue(&q); printf("对尾插入元素...\n"); InQueue(&q,'a'); InQueue(&q,'b'); InQueue(&q,'c'); InQueue(&q,'d'); showqueue(&q); printf("队列中的元素个数为%d\n",Length(&q)); printf("队首删除元素..\n"); OutQueue(&q); showqueue(&q); return 0; }
/*server engine start function */ int main(int argc,char *argv[]) { char * Ip_Addr = NULL; char * sPort; char * tmpptr; char * ServerId = NULL; int Port; ServerId = argv[1]; int new_fd,i,j,thread_no; debug_print(("Server Engine Open!\n")); /*初始化任务队列*/ p = (tQueue *)malloc(sizeof(struct Queue)); InitQueue(p); /*创建MAX_THREADNUM个线程,并初始化该线程对应的信号量*/ for(i = 0;i < MAX_THREADNUM ; i++) { thread_no = i; sem_init(&pSems[i],0,0); //初始化线程信号量 if(pthread_create(&thread_id[i] ,NULL,(void*)AllocateTask,(void*)thread_no) != 0) { fprintf(stderr,"pthread_create Error,%s:%d\n",__FILE__,__LINE__); exit(-1); } } ServerPrepare(ServerId); printf("server engine open!\n"); pthread_mutex_init(&mutex, NULL); while(1) { new_fd = ServerOpen(); ptask = (tQueueNode *)malloc(sizeof(struct QueueNode)); ptask->sockfd = new_fd; tmpptr = RecvData(new_fd); memcpy(ptask->pBuf,tmpptr,strlen(tmpptr)+1); ptask->bufsize = strlen(ptask->pBuf); debug_print(("ptask you want insert:sockfd is %d,pBuf is %s,bufsize is %d\n",ptask->sockfd,\ ptask->pBuf,\ ptask->bufsize)); InQueue(p,ptask); //如果收到数据则入队等候处理 j = (rand()%3); //随机唤醒其中的一个线程将资源分配给该线程 debug_print(("V op +%d thread\n",j)); sem_post(&pSems[j]); //用信号量v操作将资源数目+1 } ServerCloseinfo(ServerId); CloseServer(); for(i = 0;i < MAX_THREADNUM;i++) { sem_destroy(&pSems[i]); DeleteQueue(p); } return 0; }
int main(){ QueueInt *cola; int elemento; bool res; cola = CrearQueue(2); res = InQueue(cola,3); ///if(res) printf("Se pudo\n"); res = InQueue(cola,5); ///if(res) printf("Se pudo\n"); res = InQueue(cola,4); ///if(res) printf("Se pudo\n"); res = dQueue(cola,&elemento); if(res) printf("Se pudo\n"); printf("%d\n",elemento); res = dQueue(cola,&elemento); if(res) printf("Se pudo\n"); printf("%d\n",elemento); res = dQueue(cola,&elemento); if(res) printf("Se pudo\n"); printf("%d\n",elemento); return 0; }
void SetKnight(Solution* Sol, int lin, int col){ Sol->nKnights++; InQueue(Sol->knights[lin], col); }
void SetQueen(Solution* Sol, int lin, int col){ Sol->nQueens++; InQueue(Sol->queens[lin], col); }