数据结构
队列
队列和栈很相似,都是一种顺序表,链表;不过栈的特性为先进后出,而队列的特性为先进先出,且在生活中用处很多,比如说排号系统,银行的叫号系统都是用先进先出的特性完成的
链式队列
一、定义链式队列
Ps:用链表来实现队列
typedef int ElemType;
typedef struct Node //链表
{
ElemType data;
Node *next;
}*NodePtr;
typedef struct LinkQue //队列
{
NodePtr front;
NodePtr rear;
}*LinkQuePtr;
二、链式队列所实现的功能
1.链式队列初始化
int Init_LinkQue(LinkQuePtr que) //初始化
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d Init is error\n",__FILE__,__LINE__);
exit(0);
}
que->front = que->rear = NULL;
return true;
}
2.入队列
int Push_LinkQue(LinkQuePtr que,ElemType val) //入队列
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d Push is error\n",__FILE__,__LINE__);
exit(0);
}
NodePtr s = Apply_Node(val);
if(Empty_LinkQue(que))
{
que->front = s;
que->rear = s;
}
else
{
que->rear->next = s;
que->rear = s;
}
return true;
}
3.出队列
int Pop_LinkQue(LinkQuePtr que) //出队列
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d Pop is error\n",__FILE__,__LINE__);
exit(0);
}
if(Empty_LinkQue(que))
{
return false;
}
NodePtr s = que->front;
que->front = s->next;
free(s);
return true;
}
4.获得队列头元素
ElemType Get_QueHead(LinkQuePtr que) //获得队头元素
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d GetHead is error\n",__FILE__,__LINE__);
exit(0);
}
if(Empty_LinkQue(que))
{
return false;
}
else
{
return que->front->data;
}
}
5.申请新空间
static NodePtr Apply_Node(ElemType val) //申请空间
{
NodePtr s = (NodePtr)malloc(sizeof(Node));
assert(s!=NULL);
s->data = val;
s->next = NULL;
return s;
}
6.销毁链式队列
int Destroy_LinkQue(LinkQuePtr que) //销毁
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d Destroy is error\n",__FILE__,__LINE__);
exit(0);
}
if(Empty_LinkQue(que))
{
return false;
}
else
{
while(que->front->next!=NULL)
{
Pop_LinkQue(que);
}
que->front = que->rear = NULL;
return true;
}
}
7.判空
int Empty_LinkQue(LinkQuePtr que) //判空
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d IsEmpty is error\n",__FILE__,__LINE__);
exit(0);
}
if(que->front == NULL)
{
return true;
}
return false;
}
总代码:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int ElemType;
typedef struct Node
{
ElemType data;
Node *next;
}*NodePtr;
typedef struct LinkQue
{
NodePtr front;
NodePtr rear;
}*LinkQuePtr;
static NodePtr Apply_Node(ElemType val)
{
NodePtr s = (NodePtr)malloc(sizeof(Node));
assert(s!=NULL);
s->data = val;
s->next = NULL;
return s;
}
int Empty_LinkQue(LinkQuePtr que)
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d IsEmpty is error\n",__FILE__,__LINE__);
exit(0);
}
if(que->front == NULL)
{
return true;
}
return false;
}
int Init_LinkQue(LinkQuePtr que)
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d Init is error\n",__FILE__,__LINE__);
exit(0);
}
que->front = que->rear = NULL;
return true;
}
int Push_LinkQue(LinkQuePtr que,ElemType val)
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d Push is error\n",__FILE__,__LINE__);
exit(0);
}
NodePtr s = Apply_Node(val);
if(Empty_LinkQue(que))
{
que->front = s;
que->rear = s;
}
else
{
que->rear->next = s;
que->rear = s;
}
return true;
}
int Pop_LinkQue(LinkQuePtr que)
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d Pop is error\n",__FILE__,__LINE__);
exit(0);
}
if(Empty_LinkQue(que))
{
return false;
}
NodePtr s = que->front;
que->front = s->next;
free(s);
return true;
}
ElemType Get_QueHead(LinkQuePtr que)
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d GetHead is error\n",__FILE__,__LINE__);
exit(0);
}
if(Empty_LinkQue(que))
{
return false;
}
else
{
return que->front->data;
}
}
int Destroy_LinkQue(LinkQuePtr que)
{
assert(que != NULL);
if(que == NULL)
{
printf("%s:%d Destroy is error\n",__FILE__,__LINE__);
exit(0);
}
if(Empty_LinkQue(que))
{
return false;
}
else
{
while(que->front->next!=NULL)
{
Pop_LinkQue(que);
}
que->front = que->rear = NULL;
return true;
}
}
int main()
{
//以下为测试内容-------------------
LinkQue que;
Init_LinkQue(&que);
for(int i=0;i<10;i++)
{
Push_LinkQue(&que,i);
}
printf("%d ",Get_QueHead(&que));
//printf("%d\n",que.front->data);
Pop_LinkQue(&que);
printf("%d ",Get_QueHead(&que));
//printf("%d\n",que.front->data);
Pop_LinkQue(&que);
printf("%d ",Get_QueHead(&que));
Pop_LinkQue(&que);
printf("%d ",Get_QueHead(&que));
Pop_LinkQue(&que);
printf("%d ",Get_QueHead(&que));
Pop_LinkQue(&que);
printf("%d ",Get_QueHead(&que));
return 0;
}