C语言,队列

来源:百度知道 编辑:UC知道 时间:2024/05/26 01:57:45
#include<stdio.h>
#include<ctype.h>
#include<string.h>
typedef struct linked_queue
{
int data;
struct linked_queue * next;
}LqueueTp;
typedef struct queueptr
{
LqueueTp *front,*rear;
}QueptrTp;

void InitQueue(QueptrTp *lq)
{
LqueueTp *p;
p=(LqueueTp *)malloc(sizeof(LqueueTp));
lq->front=p;
lq->rear=p;
(lq->front)->next=NULL;
}

void EnQueue(QueptrTp * lq,int x)
{
LqueueTp *p;
p=(LqueueTp *)malloc(sizeof(LqueueTp));
p->data=x;
p->next=NULL;
(lq->rear)->next=p;
lq->rear=p;
}

OutQueue(QueptrTp *lq,int *x)
{
LqueueTp *s;
if(lq->front==lq->rear)
{
printf("队空");
return 0;
}
else
{
s=(lq->front)->next;
*x=s->data;
(lq->front)->next=s->next;
if(s->next==NULL)

函数scanf格式化读取输入字符、数字的时候,只读取固定大小的数据,多余的数据(换行符,多输入的字符就留在了输入流中),继续作为输入。

void main()
{
QueptrTp lq;
int n;
char ch;
InitQueue(&lq);
while(1)
{
printf("\n请输入命令:");
scanf("%c",&ch);
fflush(stdin); //刷新缓冲区,清除缓冲区中多余的字符、换行符
/*if(ch>90)
{
ch=ch-32;
}*/
switch(toupper(ch))
{
case 'A':
printf("输入病历号\n");
scanf("%d",&n);
fflush(stdin);//刷新缓冲区,清除缓冲区中多余的字符、换行符
EnQueue(&lq,n);
break;
case 'N':
if(!EmptyQueue(lq))
{
OutQueue(&lq,&n);
printf("病历号为%d的病人就诊",n);
}
else
printf("无病人等待就诊\n");
break;
case 'Q':
printf("排队等候的病人依次就诊\n");
break;
}
if(toupper(ch)=='Q')
{
while(!EmptyQueue(lq))
{