#include<stdio.h>
#include<stdlib.h>
//初始化队列节点所包含的内容
struct node
{
int Data;
struct node *next;
};
typedef struct node* Node;
//初始化一个空的链表队列
struct Qnode
{
Node Front,Rear;
};
typedef struct Qnode* Q;
//判断队列是否为空
bool isEmpty(Q q)
{
if(q->Front==NULL)
{
printf("这是一个空队列\n");
return true;
}
else
{
printf("队列未空\n");
return false;
}
}
//出队
int OutQueue(Q q)
{
if(isEmpty(q))
{
printf("出队失败\n");
return -1;
}
Node FrontCell;
int FrontData;
//这波很关键
FrontCell=q->Front;
//这波很关键
q->Front=q->Front->next;
FrontData=FrontCell->Data;
free(FrontCell);
return FrontData;
printf("出队成功\n");
}
//创造一个链表队列
Q CreateQ( Q q)
{
q->Front=(Node)malloc(sizeof(struct node));
q->Rear=(Node)malloc(sizeof(struct node));
q->Front=q->Rear;
q->Rear->next=NULL;
printf("已创造队列\n");
return q;
}
//入队
void InsertOneQ(int x,Q q)
{
printf("正在尝试入队\n");
q->Rear->Data=x;
q->Rear->next=(Node)malloc(sizeof(struct node));
q->Rear=q->Rear->next;
q->Rear->next=NULL;
printf("已成功入队\n");
}
int main()
{
printf("新建一个队列\n");
Q q=(Q)malloc(sizeof(struct Qnode));;
q=CreateQ(q);
printf("新建队列成功\n");
printf("请输入一个入队元素\n");
int x;
scanf("%d",&x);
InsertOneQ(x,q);
printf("入队成功\n");
int y;
y=OutQueue(q);
printf("%d",y);
return 0;
}
c语言实现链式队列
最新推荐文章于 2024-05-08 17:25:09 发布