概念
逻辑结构:线性结构
存储结构:链式存储
操作:创建、入列、出列、判空、清空
队列结构体:
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
} linkqueu_node, *linkqueue_list_t;
typedef struct //将头指针和尾指针封装到一个结构体里
{
linkqueue_list_t front; //相当于对列的头指针
linkqueue_list_t rear; //相当于对列的尾指针
} linkqueue_t; //有了链表得头指针和尾指针就可以操作这个链表
创建空队列:
入列:
出列:
结构体
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node//数据结构体
{
datatype data;
struct node *next;
}linksequeue_t,* linksequeue_list_t;
typedef struct sequeue//指针结构体
{
linksequeue_list_t front;//相当于队列的头指针
linksequeue_list_t rear;//相当于队列的尾指针
}linkqueue_t;//有了链表的头指针尾指针就可以操作这个链表
1)创建空队列
//创建一个空的队列,用有头链表。
linkqueue_t *CreatSequeue()
{
//1. 创建队列的空间,包含头尾指针。
linkqueue_t *p=(linkqueue_t *)malloc(sizeof(linkqueue_t));
if(NULL==p)
{
perror("开辟失败\n");
return NULL;
}
//2. 申请链表头节点,让头front和尾rear指针指向头节点p->front=p->rear=(linksequeue_list_t)malloc(sizeof(linksequeue_t));
if(NULL==p->rear)
{
perror("开辟失败\n");
return NULL;
}
//3. 初始化头节点
p->front->next=NULL;
return p;
}
2)入列
//入列 data代表入列的数据
int InsertSequeue(linkqueue_t *p,datatype data)
{
//1.创建一个新节点
linksequeue_list_t pnew=(linksequeue_list_t)malloc(sizeof(linksequeue_t));
if(NULL==pnew)
{
perror("开辟失败\n");
}
//2. 初始化新节点
pnew->data=data;
pnew->next=NULL;
//3. 将新节点连到链表尾巴,然后移动尾巴到新节点
p->rear->next=pnew;//将新节点连到链表尾巴
p->rear=pnew;//移动尾巴到新节点
return 0;
}
3)判空
//判空
int IsVoidSequeue(linkqueue_t *p)
{
return p->rear==p->front;
}
4)出列
//出列
int OutSequeue(linkqueue_t *p)
{
if(IsVoidSequeue(p))//容错判断
{
printf("链式队列为空\n");
return -1;
}
//2.出列
//(1)定义一个pdel指针指向被删除得节点,也就是front所指节点。
linksequeue_list_t pdel;
pdel = p->front;
//(2)front向后移动一个位置
p->front=pdel->next;
//(3)释放被删除节点
free(pdel);
//(4)将数据出列
printf("%d\n",p->front->data);
return p->front->data;
}
5)遍历
void Show(linkqueue_t *p)
{
if(IsVoidSequeue(p))//容错判断
{
printf("链式队列为空\n");
}
//不能改变front指向
linksequeue_list_t pt=p->front;
while(pt->next!=NULL)
{
pt=pt->next;
printf("%d ",pt->data);
}
puts(" ");
}
6)链式队列长度
int LengthSequeue(linkqueue_t *p)
{
int num=0;
linksequeue_list_t pt=p->front;
//遍历长度
while(pt->next!=NULL)
{
pt=pt->next;
num++;
}
printf("此时链式队列长度为%d\n",num);
return num;
}
7)遍历到位空
int ClearSequeue(linkqueue_t *p)
{
while (!IsVoidSequeue(p))
{
OutSequeue(p);
}
puts(" ");
}
综合代码
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
}linksequeue_t,* linksequeue_list_t;
typedef struct sequeue
{
linksequeue_list_t front;//相当于队列的头指针
linksequeue_list_t rear;//相当于队列的尾指针
}linkqueue_t;//有了链表的头指针尾指针就可以操作这个链表
//创建一个空的队列,用有头链表。
linkqueue_t *CreatSequeue()
{
linkqueue_t *p=(linkqueue_t *)malloc(sizeof(linkqueue_t));//1. 创建队列的空间,包含头尾指针。
if(NULL==p)
{
perror("开辟失败\n");
return NULL;
}
//2. 申请链表头节点,让头front和尾rear指针指向头节点。
p->front=p->rear=(linksequeue_list_t)malloc(sizeof(linksequeue_t));
if(NULL==p->rear)
{
perror("开辟失败\n");
return NULL;
}
//3. 初始化头节点
p->front->next=NULL;
return p;
}
//入列 data代表入列的数据
int InsertSequeue(linkqueue_t *p,datatype data)
{
//1.创建一个新节点
linksequeue_list_t pnew=(linksequeue_list_t)malloc(sizeof(linksequeue_t));
if(NULL==pnew)
{
perror("开辟失败\n");
}
//2. 初始化新节点
pnew->data=data;
pnew->next=NULL;
//3. 将新节点连到链表尾巴,然后移动尾巴到新节点
p->rear->next=pnew;//将新节点连到链表尾巴
p->rear=pnew;//移动尾巴到新节点
return 0;
}
//判空
int IsVoidSequeue(linkqueue_t *p)
{
return p->rear==p->front;
}
//出列
int OutSequeue(linkqueue_t *p)
{
if(IsVoidSequeue(p))//容错判断
{
printf("链式队列为空\n");
return -1;
}
//2.出列
//(1)定义一个pdel指针指向被删除得节点,也就是front所指节点。
linksequeue_list_t pdel;
pdel = p->front;
//(2)front向后移动一个位置
p->front=pdel->next;
//(3)释放被删除节点
free(pdel);
//(4)将数据出列
printf("%d\n",p->front->data);
return p->front->data;
}
//遍历
void Show(linkqueue_t *p)
{
if(IsVoidSequeue(p))//容错判断
{
printf("链式队列为空\n");
}
linksequeue_list_t pt=p->front;
while(pt->next!=NULL)
{
pt=pt->next;
printf("%d ",pt->data);
}
puts(" ");
}
int LengthSequeue(linkqueue_t *p)
{
int num=0;
linksequeue_list_t pt=p->front;
while(pt->next!=NULL)
{
pt=pt->next;
num++;
}
printf("此时链式队列长度为%d\n",num);
return num;
}
int ClearSequeue(linkqueue_t *p)
{
while (!IsVoidSequeue(p))
{
OutSequeue(p);
}
puts(" ");
}
int main(int argc, char const *argv[])
{
linkqueue_t *p=CreatSequeue();
int num;
int push[32];
char in;
while(1)
{
printf("请输入命令=");
scanf("%c",&in);//回收一个垃圾字符
getchar();
switch(in)
{
case 'A':
printf("请输入要插入的元素个数:");
scanf("%d",&num);
getchar();
printf("请输入要插入的元素内容:");
for(int i=0;i<num;i++)
{
scanf("%d",&push[i]);
getchar();
}
for(int i=0;i<num;i++)
{
InsertSequeue(p,push[i]);
}break;
case 'a':
printf("请输入要插入的元素内容:");
scanf("%d",&num);
getchar();
InsertSequeue(p,num);break;
case 'b':
Show(p);break;
case 'c'://清空栈
ClearSequeue(p);break;
case 'd':
LengthSequeue(p);break;
case 'e':
//出栈
OutSequeue(p);break;
// // case 'f':
// // ShowForlinkstack(p);break;
// // case 'q':
// // break;break;
}
}
return 0;
}