分享下最近的学习成果:循环队列
c语言吧
全部回复
仅看楼主
level 9
#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 = #
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);
}
2014年04月17日 04点04分 1
1