梦幻流苏147258 梦幻流苏147258
关注数: 20 粉丝数: 62 发帖数: 3,913 关注贴吧数: 70
分享下最近的学习成果:循环队列 #include <stdio.h> #define QueueSize_UartLen 8 typedef struct { int front; int rear; int counter; int uart_data[QueueSize_UartLen]; }CIRQUEUE_UART; //定义有队头、队尾、计数器、数组宽为QueueSize_UartLe(宏定义)的结构体 void InitQueue(CIRQUEUE_UART *queue){ queue->front = 0; queue->rear = 0; queue->counter = 0; }//初始化队列 int QueueEmpty(CIRQUEUE_UART *queue){ return queue->counter == 0; } //判断队列是否为空 int QueueFull(CIRQUEUE_UART *queue){ return queue->counter == QueueSize_UartLen; } //判断队列是否满 int InQueue(CIRQUEUE_UART * queue , int data){ if(QueueFull(queue)){ return 0; }else{ queue->uart_data[queue->rear] = data; queue->counter++; queue->rear = (queue->rear+1)%QueueSize_UartLen; return 1; } }//进入循环队列 int OutQueue(CIRQUEUE_UART *queue , int *p_data){ if(QueueEmpty(queue)){ //输出空队提示 return 0; }else{ *p_data = queue->uart_data[queue->front]; queue->counter --; queue->front = (queue->front+1) % QueueSize_UartLen; } } int main(void){ CIRQUEUE_UART num; CIRQUEUE_UART *pNum = &num; InitQueue(pNum); int i = 0 , box[8]; int *temp; int *max; printf("请输入8个数字"); while ( i < 8){ scanf("%d",&box[i]); if ( !QueueFull(pNum)){ InQueue(pNum,box[i]); }//如果队列未满则插入数据,否则跳出 i ++ ; } if ( !QueueEmpty(pNum)){ for ( i = 0; i < 8; i ++ ){ if ( *(pNum->uart_data+(pNum->front)) > *(pNum->uart_data+(pNum->front)+1)){ *temp = *(pNum->uart_data+(pNum->front)); *(pNum->uart_data+(pNum->front)+1) = *temp; OutQueue(pNum,(temp)); }else { *temp = *(pNum->uart_data+(pNum->front)+1); OutQueue(pNum,(temp)); } } } printf("这组数据中最大的是:%d",*temp); }
1 下一页