/* 用两个队列模拟出栈操作 */ bool pop(PQUEUE pS1,PQUEUE pS2,int *pData) { if(is_empty(pS1) && is_empty(pS2)) return false; int DelData; if(!is_empty(pS2)) { int len = length(pS2); while(len-- > 1) { de_queue(pS2,&DelData); en_queue(pS1,DelData); } //将队列的最后一个元素出队,作为出栈元素 de_queue(pS2,pData); return true; } if(!is_empty(pS1)) { int len = length(pS1); while(len-- > 1) { de_queue(pS1,&DelData); en_queue(pS2,DelData); } //将队列的最后一个元素出队,作为出栈元素 de_queue(pS1,pData); return true; } }
void merge(int *p, int low, int middle, int high) { int i; queue_t *q1 = &queue1; queue_t *q2 = &queue2; queue_init(q1); queue_init(q2); for (i = low; i <= middle; i++) en_queue(q1, p[i]); for (i = middle + 1; i <= high; i++) en_queue(q2, p[i]); i = low; while (!(empty_queue(q1) || empty_queue(q2))) { if (head_queue(q1) > head_queue(q2)) p[low++] = de_queue(q1); else p[low++] = de_queue(q2); } while (!(empty_queue(q1))) p[low++] = de_queue(q1); while (!(empty_queue(q2))) p[low++] = de_queue(q2); }
int main() { int data_de = 0; //用来保存出队的元素值 //创建队列并进行入队测试 PQUEUE pS = create_queue(); en_queue(pS,2); en_queue(pS,4); en_queue(pS,6); traverse_queue(pS); //出队测试 if(de_queue(pS,&data_de)) printf("delete succeed,the deleted data is: %d\n",data_de); else printf("queue is empty! delete falied!\n"); traverse_queue(pS); //销毁队列测试 destroy_queue(pS); printf("queue destroyed!\n"); traverse_queue(pS); return 0; }
static int deq_buffer(queue* q) { int* ip = (int*) de_queue(q); if (ip != NULL) { int i = *ip; free(ip); return i; } else return INT_MIN; }
void keyboard_read(u32* key) { u8 scan_code; u32 col; BOOL b_shift; if (!is_queue_empty(&kb_queue)) { io_cli(); de_queue(&kb_queue, &scan_code); io_sti(); /* 首先处理E0 开头的情况,将b_leading_e0置位,然后直接返回 */ if (scan_code == 0xe0) { b_leading_e0 = TRUE; return; } /* 是键被抬起,暂时将键值设为0 */ if (scan_code & 0x80) { if (*key == K_SHIFT_L) b_shift_l = 0; if (*key == K_SHIFT_R) b_shift_r = 0; *key = 0; return; } b_shift = b_shift_l || b_shift_r; col = b_shift ? 1 : 0; if (b_leading_e0) { col = 2; b_leading_e0 = FALSE; } *key = keymap[scan_code & 0x7f][col]; if (*key == K_SHIFT_L) b_shift_l = 1; if (*key == K_SHIFT_R) b_shift_r = 1; b_shift = b_shift_l || b_shift_r; } }
void INTERRUPT FAR serial( void) { /* interrupt handler */ int temp; disable(); while (1) { comm_status.intrupt = inp(ComBase + INT_ID); comm_status.intrupt &= 0x0f; switch (comm_status.intrupt) { case 0x00: /* modem interrupt */ comm_status.modem = inp(ComBase + MODEM_STATUS); break; case 0x02: /* xmit interrupt */ if (queue_empty(Serial_Out_Queue)) outp(ComBase + INT_EN, RX_INT | ERR_INT | RS_INT); else { temp = de_queue(Serial_Out_Queue); if (-1 != temp) outp(ComBase + XMIT, temp); } break; case 0x04: /* receive interrupt */ en_queue(Serial_In_Queue, (char) inp(ComBase + REC)); break; case 0x06: /* line interrupt */ comm_status.line = inp(ComBase + LINE_STATUS); (void) inp(ComBase + REC); en_queue(Serial_In_Queue, '!'); break; default: /* No Mo` Left */ comm_status.modem = inp(ComBase + MODEM_STATUS); outp(0x20, 0x20); enable(); return; } /* switch */ } /* while */ }
void *task_test( void *pParam ) { CQueue *pTaskQ = (CQueue *)pParam; CQueueNode *pTaskQNode = NULL; CMessage *pMesg = NULL; if ( !pTaskQ ) return NULL; while ( 1 ) { pTaskQNode = de_queue( pTaskQ ); if ( pTaskQNode ) { pMesg = CONTAINER_OF_QUEUE( pMesg, CMessage ); log_print( "task message received..............." ); } } return NULL; }
int ComRecChar( void) { return de_queue(Serial_In_Queue); }
int main(int argc, char * argv[]) { int opt; while((opt = getopt(argc,argv,"c:d:f:w:i:o:")) != EOF) { switch(opt) { case 'c': CoverThreshold = atof(optarg); break; case 'd': DiscardThreshold = atof(optarg); break; case 'f': FThreshold = atof(optarg); break; case 'w': WThreshold = atof(optarg); break; case 'i': memcpy(ReadFileName,optarg,strlen(optarg)); ReadFileName[strlen(optarg)] = '\0'; break; case 'o': memcpy(WriteFileName,optarg,strlen(optarg)); WriteFileName[strlen(optarg)] = '\0'; break; } } /*if(argc == 2) WThreshold = atof(argv[1]); if(argc == 3) { WThreshold = atof(argv[1]); FThreshold = atof(argv[2]); } if(argc == 4) { WThreshold = atof(argv[1]); FThreshold = atof(argv[2]); DiscardThreshold = atof(argv[3]); }*/ //initialize the queue MQueue = (Queue *)malloc(sizeof(Queue)); MQueue->QnodeNum = 0; MQueue->StartQnode = (Qnode *)malloc(sizeof(Qnode)); MQueue->EndQnode = (Qnode *)malloc(sizeof(Qnode)); // MQueue->StartQnode->NextQnode = MQueue->EndQnode;//recording the relationship of the virsital start node and the virtual end node initially MQueue->EndQnode->PreviousQnode = MQueue->StartQnode; // MQueue->StartQnode->PreviousQnode = NULL;//no former node to the virtual start node MQueue->EndQnode->NextQnode = NULL;//node latter node to the virtual end node MQueue->StartQnode->QTnode = NULL;//NULL in the virtual start node MQueue->EndQnode->QTnode = NULL;//NULL in the virtual end node //judge m and n char str[10000]; get_size(m); UsedTransactionNum = m; TransactionArray = (Transaction *)malloc(m*sizeof(Transaction)); FILE * fr = fopen(ReadFileName,"r"); for( int i = 0; i < m; i ++) {//transfer each line of the data into a Transaction, we regard that there are no more than 20000 letters each line fgets(str,10000,fr); make_transaction(TransactionArray,i,str); } fclose(fr); //till now, all the data in the file has been read into the memory //initial the structure of the Tree Tree * MTree; MTree = (Tree *)malloc(sizeof(MTree)); MTree->TnodeNum = 0; MTree->Root = (Tnode *)malloc(sizeof(Tnode)); MTree->Root->TTransactionNum = m; MTree->Root->TTransaction = (Transaction *)malloc(MTree->Root->TTransactionNum * sizeof(Transaction));//allocate memory for the Transaction in the root for( int i = 0; i < MTree->Root->TTransactionNum; i ++) { MTree->Root->TTransaction[i].ItemArray = TransactionArray[i].ItemArray;//copy all the elements in the TransactionArray into the MTree MTree->Root->TTransaction[i].PacketNum = TransactionArray[i].PacketNum; } MTree->Root->ChildTnodeArray = NULL; MTree->Root->ChildTnodeNum = 0; MTree->Root->FatherTnode = NULL; MTree->Root->NextTnode = NULL; MTree->Root->PreviousTnode = NULL; MTree->Root->TItem = 0; MTree->Root->TLevel = 0; MTree->Root->TSerialNum = 0; en_queue(MTree->Root); //while(MQueue->QnodeNum) //{ //make_split(MTree,de_queue()); //} int ThisLevelNum = 0; int LastTnodeLevel = 0; // the level of last Tnode, it is used for judging whether the level has changed int TillLastLevelFinishedNum = 0; int TillThisLevelFinishedNum = 0; //the below code is to input all the data into a file FILE * fw = fopen(WriteFileName,"w"); Tnode * TmpTnode; //en_queue(MTree->Root); if( WThreshold < ((float)2)/MTree->Root->TTransactionNum) WThreshold = ((float)2)/MTree->Root->TTransactionNum - 0.01; while(MQueue->QnodeNum) { TmpTnode = de_queue(); //fprintf(fw,"TSerialNum:%d UsedTransactionNum: %d ", TmpTnode->TSerialNum, UsedTransactionNum); //if(TmpTnode->TSerialNum > 30) // fprintf(fw,"Father %d ", TmpTnode->FatherTnode->TSerialNum); // fprintf(stderr,"A\n"); make_split(MTree,TmpTnode);// all the push are done inside make_split //fprintf(fw,"AUsedTNum: %d children;: %d\n",UsedTransactionNum,TmpTnode->ChildTnodeNum); // fprintf(stderr,"%d\n",UsedTransactionNum); // fprintf(stderr,"%d\n",MTree->Root->TTransactionNum); // fprintf(stderr,"%f\n",CoverThreshold); // if(MQueue->QnodeNum == 0) // fprintf(fw,"reach end\n"); if((float)UsedTransactionNum / MTree->Root->TTransactionNum < CoverThreshold) { // fprintf(stderr,"B\n"); while(MQueue->QnodeNum) { TmpTnode = de_queue(); fprintf(fw," Numof-Tran:%5d \n",TmpTnode->TTransactionNum); for( int i = 0; i < TmpTnode->TLevel; i ++) fprintf(fw,"(@%d %d)%s %c ",TmpTnode->TItem[i].PacketSeqNum, TmpTnode->TItem[i].ByteSeqNum,TmpTnode->TItem[i].Pload,hex_asc(TmpTnode->TItem[i].Pload)==10?160:hex_asc(TmpTnode->TItem[i].Pload)); fprintf(fw,"\n"); } break; } if(TmpTnode->ChildTnodeNum) continue; fprintf(fw,"Numof-Tran:%5d \n",TmpTnode->TTransactionNum); for( int i = 0; i < TmpTnode->TLevel; i ++) fprintf(fw,"(@%d %d)%s %c ",TmpTnode->TItem[i].PacketSeqNum, TmpTnode->TItem[i].ByteSeqNum,TmpTnode->TItem[i].Pload, hex_asc(TmpTnode->TItem[i].Pload)==10?160:hex_asc(TmpTnode->TItem[i].Pload)); fprintf(fw,"\n"); } //fprintf(fw,"TnodeNum: %d\n",MTree->TnodeNum); fprintf(fw,"\n\n%.4f%% of all the Transactions are covered by this feature\n", 100 * (float)UsedTransactionNum / MTree->Root->TTransactionNum); fprintf(fw,"Parameter: \n FThreshold: %.2f;\n WThreshold: %.2f;\n DiscardThreshold: %.2f;\n CoverThreshold: %.2f;\n",FThreshold, WThreshold, DiscardThreshold, CoverThreshold); fclose(fw); fprintf(stdout,"success!\n"); return 0; }
void dancepartners(cir_queue femaledancer, cir_queue maledancer, int lun)//舞伴配对函数 { int m = length_queue(&femaledancer); //计算女队人数 int n = length_queue(&maledancer); //计算男队人数 for (int j = 1; j <= lun; ++j) //for循环表示舞会进行的轮数情况 {//通过对男女队人数的比较,进行一下操作 if (m>n)// 女队人数多于男队,女队有剩余队员 { person_type p; person_type q; person_type s; printf("------------------------"); printf("第%d轮配对情况:\n", j); for (int i = 1; i <= n; ++i) //for循环实现配对操作 { p = de_queue(&maledancer); q = de_queue(&femaledancer); //出队的男女舞伴 printf("男:%s", p.name); printf("和女:%s配对成功\n", q.name);//输出配对情况 en_queue(&maledancer, p); en_queue(&femaledancer, q); //将出队的男女舞伴重新进队 } //输出女队剩余队员情况,并输出在下一轮首先出场的队员姓名 printf("女队中还有%d个人在等待!\n", m - n); s = queue_front(&femaledancer); printf("女队中第一个等待的是:%s\n", s.name); } else if (m<n) //男队人数多于女队,男队有剩余队员 { person_type p; person_type q; person_type s; printf("------------------------"); printf("第%d轮配对情况:\n", j); for (int i = 1; i <= m; ++i) { p = de_queue(&maledancer); q = de_queue(&femaledancer);//出队的男女舞伴 printf("男:%s", p.name); printf("和女:%s配对成功\n", q.name);//输出配对情况 en_queue(&maledancer, p); en_queue(&femaledancer, q); //将出队的男女舞伴重新进队 } //输出男队剩余队员情况,并输出在下一轮首先出场的队员姓名 printf("男队中还有%d个人在等待!\n", n - m); s = queue_front(&maledancer); printf("男队中第一个等待的是:%s\n", s.name); } else //男女队人数相等,没有剩余队员 { person_type p; person_type q; // person_type s; printf("------------------------"); printf("第%d轮配对情况:\n", j); for (int i = 1; i <= m; ++i) { p = de_queue(&maledancer); q = de_queue(&femaledancer); //出队的男女舞伴 printf("男:%s", p.name); printf("和女:%s配对成功\n", q.name);//输出配对情况 en_queue(&maledancer, p); en_queue(&femaledancer, q);//将出队的男女舞伴重新进队 } printf("没有人剩余!\n"); } } }
void *workload_replayer(void *arg) { unsigned int tid = 0; readLine req; long long trace_sTime = -1; long long trace_wTime = 0; long long gio_sTime = 0; long long gio_wTime = 0; struct timeval now; int fd; char *buf; size_t ret; unsigned long mSize; unsigned long mAddr; OPERATION_TYPE op; desc = (wg_env *)arg; if( (fd = open(desc->file_path, O_CREAT|O_RDWR|O_DIRECT, 0666)) == -1 ){ //if( (fd = open(desc->file_path, O_CREAT|O_RDWR, 0666)) == -1){ PRINT("Error on opening the init_file of workload generator, file:%s, line:%d, fd=%d\n", __func__, __LINE__, fd); exit(1); } #if !defined(BLOCKING_IO) aio_initialize(desc->max_queue_depth); #endif mem_allocation(desc, &buf, REPLAYER_MAX_FILE_SIZE); if (NULL == buf) { PRINT("Error on memory allocation, file:%s, line:%d\n", __func__, __LINE__); exit(1); } while(1){ //Dequeueing req = de_queue(); //If a request is successfully dequeued. if(strcmp(req.rwbs, "EMPTY") != 0){ mSize = select_size(req.size); mAddr = select_start_addr(req.sSector); fill_data(desc, buf, mSize); //Calculate wait time based on trace log if(trace_sTime == -1){ get_start_time(&trace_sTime, &gio_sTime); } trace_wTime = MICRO_SECOND(req.sTime) - trace_sTime; if(trace_wTime < 0){ PRINT("trace_wTime : %lli\n", trace_wTime); PRINT("Issue time must be ordered. Check the issue time in trace file"); exit(1); } //Caculate how much time should wait get_current_time(&now); gio_wTime = TIME_VALUE(&now) - gio_sTime; PRINT("TIMEDEBUG trace_wTime:%10lli \tgio_wTime:%10lli\n", trace_wTime, gio_wTime); //If we need to wait if(trace_wTime > gio_wTime){ PRINT("WAITING ....%lli us\n", trace_wTime - gio_wTime); usec_sleep(trace_wTime - gio_wTime); } op = strstr(req.rwbs,"R")!=NULL? WG_READ : WG_WRITE; #if defined(BLOCKING_IO) if(op == WG_READ){ //PRINT("R\n"); ret = pread(fd, buf , (size_t)mSize, mAddr); }else{ //PRINT("W\n"); ret = pwrite(fd, buf , (size_t)mSize, mAddr); fsync(fd); } if (ret != mSize) { PRINT("Error on file I/O (error# : %zu), file:%s, line:%d\n", ret, __func__, __LINE__); break; } #else ret = aio_enqueue(fd, buf, mSize, mAddr, op); if (ret != 1) { PRINT("Error on file I/O (error# : %zu), file:%s, line:%d\n", ret, __func__, __LINE__); break; } #endif //BLOCKING_IO #if 0 PRINT("DEQUEUED REQ tid:%u sTime:%lf rwbs:%s Addr:%12li \t Size:%12lu\n\n", tid, req.sTime, req.rwbs, mAddr, mSize); #endif } if( get_queue_status() == 1 ){ PRINT("END OF REPLAYER\n"); break; } } pthread_mutex_destroy(&thr_mutex); }
int main(){ char* tmp_commend = NULL;//recieve input commend char commend[10]; DATA_TYPE v;//recieve input data node* front;//front pointer node* rear;//rear pointer front = NULL;//initialize rear = NULL; while(1){ //output hint printf("\tEnter \'en\' or \'de\' to enqueue or dequeue," "\'size\' to check queue size," "\'q\' is to leave.\n"); if(scanf("%as", &tmp_commend)){ if(strlen(tmp_commend) > 9){ printf("Commend not found!!\n"); continue; } strcpy(commend,tmp_commend); myfree(tmp_commend); //todo string copy }else{ printf("Aalloction fail!\n"); exit(1); } //to en_queu if(!strcmp(commend,"en")){ printf("\tEnter a value to push to the queue\n"); scanf("%d",&v); en_queue(&front, &rear,v); en_queue_msg(v); //to de_queue }else if(!strcmp(commend,"de")){ if(queue_empty(front)){ printf("The queue is empty!!!\n"); }else{ de_queue_msg(de_queue(&front)); } //to get size }else if(!strcmp(commend,"size")){ printf("The size of queue is %d.\n",queue_size(front)); //to quit }else if(!strcmp(commend,"q")){ printf("End process\n"); break; //enter wrong commend }else{ printf("Commend not found!!\n"); } } return 0; }
int bfs(int from, int to) { if (from == to) return 0; link_queue q; init_queue(&q); memset(visited, 0, sizeof(visited)); queue_node p, node; node.num = from; visited[from] = 1; en_queue(&q, node); while (!is_empty_queue(q)) { de_queue(&q, &p); int num = p.num; int n[4], i; for (i = 0; i < 4; i++) { n[i] = num % 10; num /= 10; } int x = p.num - n[0]; for (i = x; i < x+10; i++) { if (i == to) return visited[p.num]; if (is_prime[i] && visited[i] == 0) { visited[i] = visited[p.num] + 1; node.num = i; en_queue(&q, node); } } x = p.num - n[1]*10; for (i = x; i < x+100; i+=10) { if (i == to) return visited[p.num]; if (is_prime[i] && visited[i] == 0) { visited[i] = visited[p.num] + 1; node.num = i; en_queue(&q, node); } } x = p.num - n[2]*100; for (i = x; i < x+1000; i+=100) { if (i == to) return visited[p.num]; if (is_prime[i] && visited[i] == 0) { visited[i] = visited[p.num] + 1; node.num = i; en_queue(&q, node); } } x = p.num - n[3]*1000; for (i = x+1000; i < x+10000; i+=1000) { if (i == to) return visited[p.num]; if (is_prime[i] && visited[i] == 0) { visited[i] = visited[p.num] + 1; node.num = i; en_queue(&q, node); } } } destroy_queue(&q); return 0; }