#include<stdio.h>
#include<stdlib.h>
typedef struct QNode *P_QNode;
struct QNode
{
int Data;
P_QNode Next;
};
struct MyQueue
{
P_QNode Front,Rear;
};
typedef struct MyQueue *P_MyQueue;
P_MyQueue CreateQueue()
{
P_MyQueue q=(P_MyQueue)malloc(sizeof(struct MyQueue));
q->Front=q->Rear=NULL;
return q;
}
int IsEmpty(P_MyQueue q)
{
return(q->Front==NULL);
}
void Add(P_MyQueue q,int elem)
{
P_QNode n;
n=(P_QNode)malloc(sizeof(struct QNode));
if(IsEmpty(q))
{
n->Data=elem;
n->Next=NULL;
q->Front=n;
q->Rear=n;
}
else
{
q->Rear->Next=n;
n->Data=elem;
n->Next=NULL;
q->Rear=q->Rear->Next;
}
}
int Dele(P_MyQueue q)
{
if(IsEmpty(q))
{
printf("队列空");
return NULL;
}
else
{
P_QNode p;
p=q->Front;
int elem;
elem=q->Front->Data;
q->Front=q->Front->Next;
free(p);
return elem;
}
}
void PrintAll(P_MyQueue q)
{
P_QNode p=q->Front;
while(p)
{
printf("%d ",p->Data);
p=p->Next;
}
}
int main()
{
P_MyQueue Q1=CreateQueue();
while(1)
{
printf("输入需要的操作:\nA.显示队列状态\nB.入队\nC.出队\nD.退出\n");
char c;
scanf("%c",&c);
if(c=='A')
{
printf("队列状态:");
PrintAll(Q1);
printf("\n");
}
else if(c=='B')
{
printf("输入:");
int a;
scanf("%d",&a);
Add(Q1,a);
}
else if(c=='C')
{
int a=Dele(Q1);
printf("输出:%d",a);
printf("\n");
}
else if(c=='D')
{
break;
}
else
{
printf("无效输入\n");
}
char m=getchar();
}
return 0;
}
C语言实现队列链式存储
最新推荐文章于 2022-01-21 12:00:00 发布