/*顺序队的基本操作*/
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct
{
ElemType queue[MaxSize];
int front,rear;
} queuetype;
void initqueue(queuetype *Q)
{
Q->front=Q->rear=-1;
}
void enter(queuetype *Q,ElemType x)
{
if (Q->rear==MaxSize) printf("队列上溢出!/n");
else
{
Q->rear++; /*队尾指针后移*/
Q->queue[Q->rear]=x; /*新元素赋给队尾单元*/
}
}
void ddelete(queuetype *Q)
{
if (Q->front==Q->rear)
printf("队列为空!/n");
else
Q->front++;
}
ElemType gethead(queuetype *Q)
{
if (Q->front==Q->rear)
printf("队列为空!/n");
else
return(Q->queue[Q->front+1]);
}
// *//*
int empty(queuetype *Q)
{
if (Q->front==Q->rear) return(1); /*为空,则返回true*/
else return(0); /*不为空,则返回flase*/
}
void display(queuetype *Q)
{
int i;
printf("队列元素:");
for (i=Q->front+1;i<=Q->rear;i++)
printf("%c ",Q->queue[i]);
printf("/n");
}
void main()
{
queuetype *qu;
qu = (queuetype *)malloc(sizeof(queuetype));
printf("初始化队列qu/n");
initqueue(qu);
printf("队列空:%d/n",empty(qu));
printf("依次入队a,b,c,d元素/n");
enter(qu,'a');
enter(qu,'b');
enter(qu,'c');
enter(qu,'d');
display(qu);
printf("出队一次/n");
ddelete(qu);
printf("队首元素:%c/n",gethead(qu));
printf("出队一次/n");
ddelete(qu);
display(qu);
}