//This is a callback function for SLiRP that sends a packet //to the calling library. In this case I stuff //it in q queue void slirp_output (const unsigned char *pkt, int pkt_len) { struct queuepacket *p; p=(struct queuepacket *)malloc(sizeof(struct queuepacket)); SDL_LockMutex(slirp_mutex); p->len=pkt_len; memcpy(p->data,pkt,pkt_len); QueueEnter(slirpq,p); SDL_UnlockMutex(slirp_mutex); Log_Printf(LOG_WARN, "[SLIRP] Output packet with %i bytes to queue",pkt_len); }
int main() { Queue q; QueueNew(&q, sizeof(int)); int a = 2; int b = 3; int c = 4; QueueEnter(&q, &a); QueueEnter(&q, &b); QueueEnter(&q, &c); int d, e, f; QueueDelete(&q, &d); printf("%d\n", d); QueueDelete(&q, &e); printf("%d\n", e); QueueDelete(&q, &f); printf("%d\n", f); QueueFree(&q); }
//求所有路径之间的距离 int GrapgAllWay(AdjList *graph, int f, int t, void (*weizhi)(int)) { int done[MAX_VERTEX_NUM] = {0}; Queue *que; QueueDataElem *p, *q; ArcNode *x; List * head; int i; int leap = 0; que = (Queue *)malloc(sizeof(Queue)); p = (QueueDataElem *)malloc(sizeof(QueueDataElem)); x = (ArcNode *)malloc(sizeof(ArcNode)); QueueInit(que); p->l = 0; p->vn = f; p->datanext = NULL; done[p->vn] = 1; QueueEnter(que, *p); do{ QueueDelete(que, p); q = p; while(p != NULL){ done[p->vn] = 1; p = p->datanext; continue; } p = q; if(p->vn == t){ leap++; head = (List *)malloc(sizeof(List)); ListInit(head); while(p != NULL){ // printf("%d\t", p->vn + 1); // p = p->datanext; ListInsert(head, *p); p = p->datanext; } ListDxu(head); head = head->next; while(head != NULL){ // printf("%d\t\t", head->data.vn); weizhi(head->data.vn); head = head->next; } printf("\n"); printf("%d", leap); printf("\n"); } p = q; x = graph->vertex[p->vn].fristarc; while(x != NULL){ if(done[x->adjvex] == 1){ x = x->nextarc; continue; } p = (QueueDataElem *)malloc(sizeof(QueueDataElem)); p->vn = x->adjvex; p->datanext = q; QueueEnter(que, *p); x = x->nextarc; } for(i = 0 ; i < graph->vexnum ; i++){ done[i] = 0; } }while(QueueEmpty(que)); return 0; }
//迪杰斯特拉求最短路径 int GraphListPath_DJS(AdjList *graph, int f, int t) { Queue *path; //申请队列 QueueDataElem *node, *p; //申请队列元素节点 MidNode *head, *mid; //申请中转节点 int min = 0, num = 0; //记录节点的数值 int i = 0; int weigth[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //存放临接矩阵 int donevex[MAX_VERTEX_NUM] = {0}; //存放已经走过的节点 ListNode LiNo[MAX_VERTEX_NUM]; //存放数字信息 ArcNode *GrNo; //申请末尾节点 List *h; GraphRead(graph, weigth); //读取临接矩阵 path = (Queue *) malloc (sizeof(Queue)); p = (QueueDataElem *) malloc (sizeof(QueueDataElem)); GrNo = (ArcNode *) malloc (sizeof(ArcNode)); node = (QueueDataElem *) malloc (sizeof(QueueDataElem)); //初始化 donevex[f] = 1; //置初始访问路径 QueueInit(path); //初始化队列 node->l = 0; node->vn = f; node->datanext = NULL; //置初始节点的next为NULL QueueEnter(path, *node); //初始节点入队 while(i < graph->vexnum - 1){ //进入循环 min = 32768; mid = path->front->next; node = &(mid->s); //去队列头元素 while(1){ GrNo = graph->vertex[node->vn].fristarc; //得到临接表形态的头指针 while(GrNo != NULL){ if((weigth[node->vn][GrNo->adjvex] + node->l) < min && donevex[GrNo->adjvex] != 1){ min = weigth[node->vn][GrNo->adjvex] + node->l; num = GrNo->adjvex; LiNo[GrNo->adjvex].deep = node->vn; } GrNo = GrNo->nextarc; //邻接表的下一项 } mid = mid->next; //判断队列是否为空 if(mid == NULL){ break; } node = &(mid->s); } node = (QueueDataElem *) malloc (sizeof(QueueDataElem)); node->l = min; node->vn = num; donevex[num] = 1; mid = path->front->next; //指向初始路径 p = &(mid->s); while(1){ if(p->vn == LiNo[node->vn].deep){ break; } mid = mid->next; if(mid == NULL){ break; } p = &(mid->s); } node->datanext = p; QueueEnter(path, *node); //新路径入队 i++; } head = path->front; while(head != NULL){ //头不空则往下找节点 if(head->s.vn == t){ break; } head = head->next; } *node = head->s; h = (List *)malloc(sizeof(List)); ListInit(h); while(node != NULL){ // printf("%d\t", node->vn + 1); // node = node->datanext; ListInsert(h, *node); node = node->datanext; } ListDxu(h); h = h->next; while(h != NULL){ printf("%d\t", h->data.vn); h = h->next; } printf("\n"); return 0; }