循环队列
queue.h
#ifndef _QUEUE_H #define _QUEUE_H #include "all.h" #define LEN 3 //定义一个描述队列中每一个结点的结构体 typedef struct queuenode { Player *pArr; int MaxLen; int front; int rear; }queue_node; //创建空队列 int empty_queue(queue_node **ppQueue,int len); //入队 int in_queue(queue_node *pQueue,Player Data); //显示队内容 int show_queue(queue_node *pQueue); //销毁队 int destruction_queue(queue_node **ppQueue); #endif
queue.c
#include "../include/queue.h" //创建空队列 int empty_queue(queue_node **ppQueue,int len) { if(NULL == ppQueue) { return ERROR; } //分配空间 *ppQueue = (queue_node *)malloc(sizeof(queue_node)); if(NULL == *ppQueue) { printf("malloc error"); return ERROR; } memset(*ppQueue,0,sizeof(queue_node)); //为队列分配空间 (*ppQueue)->pArr = (Player *)malloc(sizeof(Player) * len); if(NULL == (*ppQueue)->pArr) { printf("malloc error"); return ERROR; } memset((*ppQueue)->pArr,0,sizeof(Player)); //初始化 (*ppQueue)->MaxLen = len; (*ppQueue)->front = 0; (*ppQueue)->rear = 0; return OK; } //入队 int in_queue(queue_node *pQueue,Player Data) { if(NULL == pQueue) { return ERROR; } if(pQueue->front == (pQueue->rear + 1) % pQueue->MaxLen) { pQueue->front = (pQueue->front + 1) % pQueue->MaxLen; } pQueue->rear = (pQueue->rear + 1) % pQueue->MaxLen; pQueue->pArr[pQueue->rear] = Data; return OK; } //显示队内容 int show_queue(queue_node *pQueue) { int i; if(NULL == pQueue) { return ERROR; } printw("Information of recent retired players is as follows:\n"); for(i = 0; pQueue->rear != (pQueue->front + i) % pQueue->MaxLen; i++) { printw("************************\n"); printw("Name:\t%s\n",pQueue->pArr[(pQueue->front + i + 1) % pQueue->MaxLen].name); printw("ID:\t%s\n",pQueue->pArr[(pQueue->front + i + 1) % pQueue->MaxLen].id); printw("Pos:\t%s\n",pQueue->pArr[(pQueue->front + i + 1) % pQueue->MaxLen].position); printw("Team\t%s\n",pQueue->pArr[(pQueue->front + i + 1) % pQueue->MaxLen].team); printw("************************\n"); } return OK; } //销毁队 int destruction_queue(queue_node **ppQueue) { if(NULL == ppQueue) { return ERROR; } free((*ppQueue)->pArr); (*ppQueue)->pArr = NULL; free(*ppQueue); *ppQueue = NULL; return OK; }