队列的顺序存储结构及实现
//队列的顺序存储结构及实现#include"stdio.h"
#include"stdlib.h"
#define MAXSIZE 100
typedef int ElemType;
typedef struct
{ElemType elem[MAXSIZE];
int front,rear;
}SeQueue;
void creat(SeQueue *Q);
void AddQ(SeQueue *Q,ElemType x);
ElemType Del(SeQueue *Q);
void outlin(SeQueue Q);
void main()
{
SeQueue se;ElemType x,y;int cord;
do
{ printf("\n 主菜单 \n");
printf("\n 1 建立顺序队列 \n");
printf("\n 2 入队一个元素 \n");
printf("\n 3 删除一个元素 \n");
printf("\n 4 结束程序运行 \n");
printf("--------------------\n");
printf("请输入您的选择(1,2,3,4)");scanf("%d",&cord);
switch(cord)
{case 1:{creat(&se);outlin(se);
}break;
case 2:{printf("\n x=");scanf("%d",&x);
AddQ(&se,x);
outlin(se);
}break;
case 3:{y=Del(&se);printf("\n x=%d",y);
outlin(se);
}break;
case 4:exit(0);
}
}while(cord<=4);
}
void creat(SeQueue *Q)
{int i,n,x;
Q->front=-1;
Q->rear=-1;
printf("n= ");scanf("%d",&n);
for(i=1;i<=n;i++)
{printf("\n data= ");scanf("%d",&x);
AddQ(Q,x);
}
}
void AddQ(SeQueue *Q,ElemType x)
{if((Q->rear+1)%MAXSIZE==Q->front)printf("Queue is FULL!\n");
else{Q->rear=(Q->rear+1)%MAXSIZE;
Q->elem[Q->rear]=x;
}
}
ElemType Del(SeQueue *Q)
{if(Q->front==Q->rear)
{printf("Queue is EMPTY!\n");
return -1;
}
else {Q->front=(Q->front+1)%MAXSIZE;
return(Q->elem[Q->front]);
}
}
void outlin(SeQueue Q)
{int i;
if(Q.front==Q.rear)printf("\n Queue is EMPTY!\n");
else
{printf("\n队列如下:");
for(i=Q.rear;i>=0;i--)
printf("\n%6d",Q.elem[i]);}
}