#include"stdio.h"
#include"stdlib.h"
typedef int elemtype;
typedef struct queue
{elemtype data;
struct queue *next;
} ;
typedef struct{
struct queue *front;
struct queue *rear;
}linkqueue;
elemtype inilist(linkqueue l)
{l->front=l->rear=(struct queue)malloc(sizeof(struct queue));
if(!l->front)/建立链队列头指针所指结点/
/建立链队列的头结点/
return 0;/Q指针所指的front指针指向p/
/Q指针所指的rear指针指向p/
l->front->next=NULL;/初始化链队列函数/
}
elemtype pank(linkqueue l)
{
if(l.front==l.rear)/判断队空函数/
return 1;/链队为空/
else
return 0;
}
elemtype ru(linkqueue *l,elemtype e)/入队函数/
{
struct queue p;
p=(struct queue)malloc(sizeof(struct queue));/生成新结点/
if(!p)
return 0;
p->data=e;p->next=NULL; /将x存入新结点的数据域/
l->rear->next=p;/将新结点插入链队之后/
l->rear=p; /队尾指针指向队尾元素/
}
elemtype chu(linkqueue *l,elemtype e) /出队函数/
{struct queue *p;
if(l->rearl->front)/调用判空函数EmptyQueue(Q),判断队列是否为空/
return 0;/队不为空/
p=l->front->next;/p指向队头元素/
e=p->data;/队头元素取出赋给x/
l->front->next=p->next; /队头指针的指针域存放新队头元素的地址/
if(pl->rear)
l->front=l->rear;/队列中只含有一个元素出队/
free§; /出队后队尾指针指向队头指针,此时队空/
return e;
}
elemtype get(linkqueue l,elemtype e)/获取队头元素函数/
{struct queue *q;
if(!pank(l))
return 1;/调用判空函数EmptyQueue(Q),判断队列是否为空/
q=l.front->next;/队不为空/
e=q->data;/队头元素赋给变量x/
return e;
}
elemtype bianli(linkqueue l) /显示队中元素函数/
{struct queue *p;
p=l.front;
while(p->next)
{p=p->next;
printf("%2d",p->data);
}
}
void MenuQueue()
{ /显示菜单子函数/
printf("\n 队列子系统");
printf("\n =================================================");
printf("\n| 1——初始化队列 |");
printf("\n| 2——入队操作 |");
printf("\n| 3——出队操作 |");
printf("\n| 4——求队头元素 |");
printf("\n| 5——显示队中所有元素 |");
printf("\n| 0——返回 |");
printf("\n =================================================");
printf("\n请输入菜单号(0-5)😊;
}
int main()
{
int i,n,flag;
linkqueue l;
elemtype e;
char ch1,ch2,a;
ch1=‘y’;
while(ch1==‘y’||ch1==‘Y’)
{ MenuQueue();
scanf("%c",&ch2);
getchar();
switch(ch2)
{
case ‘1’:
inilist(&l);
printf(“队列的初始化完成!”);
break;
case ‘2’:
printf(“请输入要入队的元素个数:”);
scanf("%d",&n);
printf(“请输入%d个整数进行入队:”,n);
for(i=0;i<n;i++)
{
scanf("%d",&e);
ru(&l,e);
}
printf(“入队操作完成”);
break;
case ‘3’:
printf(“请输入要出队的元素个数:”);
scanf("%d",&n);
printf(“出队的元素顺序依次为:”);
for(i=0;i<n;i++)
{
e=chu(&l,e);
printf("%5d",e);
}
break;
case '4':
printf("当前的队头元素值为:%d",get(l,e));
break;
case '5':
bianli(l);
break;
case '0':
ch1='n';break;
default:
printf("输入有误,请输入0-4进行选择!");
}
if(ch2!='0')
{ printf("\n按回车键继续,按任意键返回主菜单!\n");
a=getchar();
if(a!='\xA')
{
getchar();ch1='n';
}
}
}
}