作者在 2013-01-02 16:05:47 发布以下内容
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */
typedef int Status;
/* QElemType类型根据实际情况而定,这里假设为int */
typedef int QElemType;
/* 循环队列的顺序存储结构 */
typedef struct
{
QElemType data[MAXSIZE];
int front; /* 头指针 */
int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
}SqQueue;
/* 初始化一个空队列Q */
Status InitQueue(SqQueue *Q)
{
Q->front=0;
Q->rear=0;
return OK;
}
Status visit(QElemType c)
{
printf("%d ",c);
return OK;
}
/* 从队头到队尾依次对队列Q中每个元素输出 */
Status QueueTraverse(SqQueue Q)
{
int i;
i=Q.front;
while((i+Q.front)!=Q.rear)
{
visit(Q.data[i]);
i=(i+1)%MAXSIZE;
}
printf("\n");
return OK;
}
/* 若队列未满,则插入元素e为Q新的队尾元素 */
Status EnQueue(SqQueue *Q,QElemType e)
{
if ((Q->rear+1)%MAXSIZE == Q->front) /* 队列满的判断 */
return ERROR;
Q->data[Q->rear]=e; /* 将元素e赋值给队尾 */
Q->rear=(Q->rear+1)%MAXSIZE;/* rear指针向后移一位置, */
/* 若到最后则转到数组头部 */
return OK;
}
int main()
{
int opp, j;
SqQueue Q;
QElemType d;
InitQueue(&Q);
printf("\n1.给队列附初始值 \n2.遍历队列 \n3.入队 ");
printf("\n0.退出 \n请选择你的操作:\n");
while(opp != '0')
{
scanf("%d",&opp);
switch(opp)
{
case 1:
srand(time(0));
for(j=1; j<=10; j++)
{
d = rand()%100+1;
EnQueue(&Q,d);
}
QueueTraverse(Q);
break;
case 2:
QueueTraverse(Q);
break;
case 3:
printf("请输入需要入队的元素:");
scanf("%d", &d);
EnQueue(&Q,d);
QueueTraverse(Q);
break;
case 0:
exit(0);
}
}
return 0;
}
#include <stdlib.h>
#include <time.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */
typedef int Status;
/* QElemType类型根据实际情况而定,这里假设为int */
typedef int QElemType;
/* 循环队列的顺序存储结构 */
typedef struct
{
QElemType data[MAXSIZE];
int front; /* 头指针 */
int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
}SqQueue;
/* 初始化一个空队列Q */
Status InitQueue(SqQueue *Q)
{
Q->front=0;
Q->rear=0;
return OK;
}
Status visit(QElemType c)
{
printf("%d ",c);
return OK;
}
/* 从队头到队尾依次对队列Q中每个元素输出 */
Status QueueTraverse(SqQueue Q)
{
int i;
i=Q.front;
while((i+Q.front)!=Q.rear)
{
visit(Q.data[i]);
i=(i+1)%MAXSIZE;
}
printf("\n");
return OK;
}
/* 若队列未满,则插入元素e为Q新的队尾元素 */
Status EnQueue(SqQueue *Q,QElemType e)
{
if ((Q->rear+1)%MAXSIZE == Q->front) /* 队列满的判断 */
return ERROR;
Q->data[Q->rear]=e; /* 将元素e赋值给队尾 */
Q->rear=(Q->rear+1)%MAXSIZE;/* rear指针向后移一位置, */
/* 若到最后则转到数组头部 */
return OK;
}
int main()
{
int opp, j;
SqQueue Q;
QElemType d;
InitQueue(&Q);
printf("\n1.给队列附初始值 \n2.遍历队列 \n3.入队 ");
printf("\n0.退出 \n请选择你的操作:\n");
while(opp != '0')
{
scanf("%d",&opp);
switch(opp)
{
case 1:
srand(time(0));
for(j=1; j<=10; j++)
{
d = rand()%100+1;
EnQueue(&Q,d);
}
QueueTraverse(Q);
break;
case 2:
QueueTraverse(Q);
break;
case 3:
printf("请输入需要入队的元素:");
scanf("%d", &d);
EnQueue(&Q,d);
QueueTraverse(Q);
break;
case 0:
exit(0);
}
}
return 0;
}