#include<stdio.h>
#include<stdlib.h>
//定义节点
typedef struct node
{
int data;
struct node* next;
}Qnode;
//队列
typedef struct Qlist
{
Qnode* f;//头指针
Qnode* r;//尾指针
}*Queue;
//初始化
Queue Init(Queue q)
{// 开辟队列空间
q = (Queue)malloc(sizeof(struct node));
//开辟头结点和尾节点
q->f = (Qnode*)malloc(sizeof(Qnode));
q->r = q->f;
return q;
}
//入队
Queue Enter(Queue q, int k)
{
Qnode* s = (Qnode*)malloc(sizeof(Qnode));
s->data = k;
s->next = q->r->next;
q->r->next = s;
q->r = s;
return q;
}
//出队
Queue Deletqueue(Queue q)
{
Qnode* p = q->f->next;
q->f->next = p->next;
printf("%d ", p->data);
free(p);
p = NULL;
if (q->f->next == NULL)//如果没有元素则把尾指针指向头指针
{
q->r = q->f;
}
return q;
}
int main()
{
Queue q=NULL;
q=Init(q);
q=Enter(q, 1);
q = Enter(q, 2);
q=Deletqueue(q);
q = Deletqueue(q);
return 0;
}
链式队列c语言实现
最新推荐文章于 2025-05-09 12:34:10 发布