队列

本文详细介绍了队列这一数据结构的基本概念,包括其定义、特点以及实现方式。重点讲解了链式存储队列的创建、元素的插入与删除操作,并提供了具体的C语言实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

队列:只允许在一端进行删除,另一端进行插入的特殊线性表

特点:先进先出

顺序存储
#define max 100
typedef struct node
{
     datatype data;
     struct node *next;
}Qnode;//链式存储的节点类型
typedef struct
{
     Qnode *rear,*front;
}Lqueue;//链队列,将头尾指针装在一起
int empty(Lqueue *s)
{
     if(s->front==s->rear) return 0;
     else return 1;
}
Lqueue *init_Lqueue()
{
     Lqueue *s=(Lqueue*)malloc(sizeof(Lqueue));
     Qnode *p=(Qnode*)malloc(sizeof(Qnode));
     p->next=NULL;
     s->front=s->rear=p;
     return s;
}
void InLqueue(Lqueue *s,datatype x)
{
     Qnode *p=(Qnode*)malloc(sizeof(Qnode));
     p->data=x;//p->next=NULL;
     p->next=s->rear->next;
     s->rear->next=p;s->rear=p;
}
datatype OutLqueue(Lqueue *s)
{
     datatype x;
     if(empty(s)==0) return 0;
     Qnode *p=(Qnode*)malloc(sizeof(Qnode));
     p=s->front->next;x=p->data;
     s->front->next=p->next;
     if(p->next=NULL) s->rear=s->front;
     free(p);
     return x;
}


链式存储

#define max 10
typedef struct node
{
     datatype data;
     struct node *next;
}Qnode;//链式存储的节点类型
typedef struct
{
     Qnode *rear,*front;
}Lqueue;//链队列,将头尾指针装在一起
int empty(Lqueue *s)
{
     if(s->front==s->rear) return 0;
     else return 1;
}
Lqueue *init_Lqueue()
{
     Lqueue *s=(Lqueue*)malloc(sizeof(Lqueue));
     Qnode *p=(Qnode*)malloc(sizeof(Qnode));
     p->next=NULL;
     s->front=s->rear=p;
     return s;
}
void InLqueue(Lqueue *s,datatype x)
{
     Qnode *p=(Qnode*)malloc(sizeof(Qnode));
     p->data=x;//p->next=NULL;
     p->next=s->rear->next;
     s->rear->next=p;s->rear=p;
}
datatype OutLqueue(Lqueue *s)
{
     datatype x;
     if(empty(s)==0) return 0;
     Qnode *p=(Qnode*)malloc(sizeof(Qnode));
     p=s->front->next;x=p->data;
     s->front->next=p->next;
     if(p->next=NULL) s->rear=s->front;//记住出队一定要判断当前结点是不是队列最后一个结点,如果是把队列的队头和队尾置为相同,才能继续入队
     free(p);
     return x;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值