参考:
http://www.cnblogs.com/newwy/archive/2010/10/10/1847461.html
代码
//
//顺序队列的初始化,建立,插入,查找,删除。//
//Author:Wang Yong //
//Date: 2010.8.19 //
//
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef int ElemType;
/
//定义队列类型
typedef struct QNode
{
ElemType data[MAX];
int front,rear;
}SeqQueue;
/
//顺序队列的初始化
SeqQueue SeqQueueInit()
{
SeqQueue Q; //队头和队尾相等即为空队列
Q.front = Q.rear = 0;
return Q;
}
/
//顺序队列的入队
void SeqQueueEnter(SeqQueue &Q,ElemType x)
{
if( (Q.rear+1) % MAX == Q.front )//入队必须判断队列是否满了
printf("Queue full\n");
Q.rear = (Q.rear+1) % MAX;
Q.data[Q.rear] = x;
}
///
//顺序队列的出队列
ElemType SeqQueueOut(SeqQueue &Q)
{
if(Q.rear == Q.front) //出队列时候必须判断队列是否为空
printf("Queue empty\n");
ElemType x;
Q.front = (Q.front+1) % MAX;
x = Q.data[Q.front];
return x;
}
//
int main()
{
SeqQueue queue;
queue = SeqQueueInit();
ElemType x;
printf("请输入入队列的元素:");
while(scanf("%d",&x) != EOF)
{
SeqQueueEnter(queue,x);
}
while(queue.front != queue.rear)
{
printf("%d ",SeqQueueOut(queue));
}
return 0;
}