# 关于队列的有关问题,大家帮忙看下

www.MyException.Cn  网友分享于：2013-02-15  浏览：4次

#include   <stdio.h>
#include   <stdlib.h>

#define   MAXSIZE   10

typedef   int   ElemType;
typedef   struct
{
ElemType   data[MAXSIZE];
int   front,   rear;
}Queue;

/*   构造一个空环行队列   */
void   InitQueue(Queue   qu)
{
qu.rear   =   qu.front   =   0;
}
/*   将元素x入队   */
void   EnQueue(Queue   qu,   ElemType   x)
{
qu.data[qu.rear]   =   x;
qu.rear   =   (qu.rear+1)%MAXSIZE;
}
/*   打印建立的队列   */
void   Print(Queue   qu)
{
int   i;
for   (i=qu.front;   i <qu.rear;   i++)
printf( "%d ",   qu.data[i]);
printf( "\n ");
}

int   main()
{
Queue   qu;
ElemType   x;

InitQueue(qu);
printf( "Please   input   elem   end   with   0\n ");
scanf( "%d ",   &x);
while   (   x   !=   0)
{
EnQueue(qu,x);
scanf( "%d ",   &x);
}
Print(qu);

system( "pause ");
return   0;
}

------解决方案--------------------

qu.rear = qu.front = 0;
qu.data[qu.rear] = x;
qu.rear = (qu.rear+1)%MAXSIZE;

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10

typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int front, rear;
}Queue;

int main()
{
Queue qu;
ElemType x;
int i;
for (i = 0; i <MAXSIZE; i++)
qu.data[i] = 0;

qu.rear = qu.front = 0;
printf( "Please input elem end with 0\n ");
scanf( "%d ", &x);
while ( x != 0)
{
qu.data[qu.rear] = x;
qu.rear = (qu.rear+1)%MAXSIZE;
scanf( "%d ", &x);
}
for (i=qu.front; i <qu.rear; i++)
printf( "%-2d ", qu.data[i]);
printf( "\n ");

system( "pause ");
return 0;
}