运行结果如下:
C语言实现代码如下:
#include "stdio.h"
#include "stdlib.h"
typedef struct QNode
{
int data;
struct QNode *next;
}QNode;
typedef struct Queue
{
QNode *rear;
}Queue;
void InitQueue(Queue &Q)
{
Q.rear=(QNode *)malloc(sizeof(QNode));
Q.rear->next=Q.rear;
printf("成功初始化队列!\n");
}
void EmptyQueue(Queue Q)
{
if(Q.rear->next!=Q.rear)printf("非空队列\n");
else printf("空队列\n");
}
void EnQueue(Queue &Q , int e)
{
QNode *p=(QNode *)malloc(sizeof(QNode));
if(!p){printf("单元分配失败!");return ;}
p->data=e;
p->next=Q.rear->next;
Q.rear->next=p;
Q.rear=p;
}
void DeQueue(Queue &Q , int &e)
{
QNode *p=(QNode *)malloc(sizeof(QNode));
if(!p){printf("单元分配失败!");return ;}
p=Q.rear->next->next;
Q.rear->next->next=p->next;
if(p==Q.rear)Q.rear=Q.rear->next;
}
void Output(Queue Q)
{
QNode *p=Q.rear->next->next;
while(p!=Q.rear->next)
{
printf("%d ",p->data);
p=p->next;
}
putchar('\n');
}
int main()
{ int num;
Queue Q;
InitQueue(Q);
EmptyQueue(Q);
for(int i=0;i<10;i++)
{
EnQueue(Q,i);
Output(Q);
}
printf("现在");
EmptyQueue(Q);
printf("现在开始出队\n");
for(int i=0;i<10;i++)
{
DeQueue(Q,num);
Output(Q);
}
printf("现在");
EmptyQueue(Q);
return 0;
}
希望对你有帮助,点个赞收藏一下吧!