链队列的各种基本运算
码文不易,如果帮助到您,希望您可以帮我刷一下点击量,与您无害,与我有益谢谢 支持原创 。
欢迎大家阅读我的博客,如果有错误请指正,有问题请提问,我会尽我全力改正错误回答问题。在次谢谢大家。下面开始正式内容
队列,又称为伫列(queue),是先进先出(FIFO, First-In-First-Out)的线性表。在具体应用中通常用链表或者数组来实现。队列只允许在后端(称为rear)进行插入操作,在前端(称为front)进行删除操作。队列的操作方式和堆栈类似,唯一的区别在于队列只允许新数据在后端进行添加。维基百科-队列
单链队列使用链表作为基本数据结果,所以不存在伪溢出的问题,队列长度也没有限制。但插入和读取的时间代价较高。维基百科-单链队列
实验环境
- 语言c/c++
- 编译器devc++5.11/5.40
实验内容与要求
- 初始化链队列;
- 判断链队列是否为空;
- 依次进队元素a,b,c;
- 出队一个元素,并输出该元素;
- 输出链队列的长度;
- 依次进队元素d,e,f;
- 输出链队列的长度;
- 输出出队序列;
- 释放队列。
目录
码文不易,如果帮助到您,希望您可以帮我刷一下点击量,与您无害,与我有益谢谢 支持原创 。
实验解析
结构说明
队列的链式存储结构简称为链队列。它是限制仅在表头删除和表尾插入的单链表。
typedef struct QNode{ //链队列结点的类型定义
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct { //链队列的表头结点的的类型定义
QueuePtr front; //队头指针,指向链表的头结点
QueuePtr rear; //队尾指针,指向队尾结点
}LinkQueue;
定义说明
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef char QElemType;
#define QUEUE_INIT_SIZE 100 // 存储空间初始分配量
#define QUEUE_INCREAMENT 2 // 分配增量
定义常用常量,类型别称
函数说明
链队列函数
初始化
Status InitQueue(LinkQueue &LinkQueue){
LinkQueue.front = (QueuePtr)malloc(sizeof(QElemType));
if (!LinkQueue.front )
exit(0);
LinkQueue.size = 1;
LinkQueue.front->next = NULL;
LinkQueue.rear =LinkQueue.front;
return OK;
}
给链队列分配空间 ,成功返回1,失败则结束程序。 使用链队列一定要先初始化再使用
判空
Status EmptyQueue (LinkQueue &LinkQueue) {
if(LinkQueue.front == LinkQueue.rear)
return TRUE;
else
return FALSE;
}//
判断链队列是否为空,空返回1,不空为0
获取长度
Status GetLength(LinkQueue &LinkQueue){
return LinkQueue.size;
}
返回循环队列长度
入队
Status PushQueue(LinkQueue &LinkQueue , QElemType e ){
LinkQueue.rear->data = e;
LinkQueue.rear->next = (QueuePtr)malloc(sizeof(QElemType));
LinkQueue.rear = LinkQueue.rear->next ;
LinkQueue.rear->next =NULL;
LinkQueue.size++;
return OK;
}
将元素e存入链队列,成功返回1,失败返回0
出队
Status OutQueue(LinkQueue &LinkQueue , QElemType &e ){
if(LinkQueue.front == LinkQueue.rear)
return ERROR;
else{
e= LinkQueue.front->data;
LinkQueue.front = LinkQueue.front->next;
LinkQueue.size--;
return OK;
}
return ERROR;
}
将队尾元素出队存入e,成功返回1,失败返回0
释放
Status DestroyQueue ( LinkQueue &LinkQueue) {
if (!LinkQueue.front)
return ERROR;
LinkQueue.rear == NULL;
free (LinkQueue.front);
LinkQueue.size= 0;
return OK;
}
释放传入的单链表,成功返回1,失败返回0
主函数
int main(){
LinkQueue que;
QElemType e;
printf("1)初始化循环加头队列;\n");
InitQueue(que);
printf("2)判断循环加头队列是否为空;");
printf(EmptyQueue(que)?"ture\n":"flase\n");
printf("3)依次进队元素a,b,c;\n");
PushQueue(que,'a');
PushQueue(que,'b');
PushQueue(que,'c');
printf("4)出队一个元素,并输出该元素;");
OutQueue(que,e);
printf("%c\n",e);
printf("5)输出循环队列的长度;");
printf("%d\n",GetLength(que));
printf("6)依次进队元素d,e,f;\n");
PushQueue(que,'d');
PushQueue(que,'e');
PushQueue(que,'f');
printf("7)输出循环队列的长度;");
printf("%d\n",GetLength(que));
printf("8)输出出队序列;");
while(OutQueue(que,e)==1)
printf("%c",e);
printf("\n");
printf("9)释放队列。");
DestroyQueue(que);
}
结果展示
附录
相关资料
源代码
码文不易,如果帮助到您,希望您可以帮我刷一下点击量,与您无害,与我有益谢谢 支持原创 。
#include "stdio.h"
#include<malloc.h> //malloc()
#include<process.h> //exit()
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef char QElemType;
typedef struct QNode{ //链队列结点的类型定义
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct { //链队列的表头结点的的类型定义
QueuePtr front; //队头指针,指向链表的头结点
QueuePtr rear; //队尾指针,指向队尾结点
int size;
}LinkQueue;
Status InitQueue(LinkQueue &LinkQueue){
LinkQueue.front = (QueuePtr)malloc(sizeof(QElemType));
if (!LinkQueue.front )
exit(0);
LinkQueue.size = 1;
LinkQueue.front->next = NULL;
LinkQueue.rear =LinkQueue.front;
return OK;
}
Status EmptyQueue (LinkQueue &LinkQueue) {
if(LinkQueue.front == LinkQueue.rear)
return TRUE;
else
return FALSE;
}//
Status PushQueue(LinkQueue &LinkQueue , QElemType e ){
LinkQueue.rear->data = e;
LinkQueue.rear->next = (QueuePtr)malloc(sizeof(QElemType));
LinkQueue.rear = LinkQueue.rear->next ;
LinkQueue.rear->next =NULL;
LinkQueue.size++;
return OK;
}
Status OutQueue(LinkQueue &LinkQueue , QElemType &e ){
if(LinkQueue.front == LinkQueue.rear)
return ERROR;
else{
e= LinkQueue.front->data;
LinkQueue.front = LinkQueue.front->next;
LinkQueue.size--;
return OK;
}
return ERROR;
}
Status GetLength(LinkQueue &LinkQueue){
return LinkQueue.size;
}
Status DestroyQueue ( LinkQueue &LinkQueue) {
if (!LinkQueue.front)
return ERROR;
LinkQueue.rear == NULL;
free (LinkQueue.front);
LinkQueue.size= 0;
return OK;
}
int main(){
LinkQueue que;
QElemType e;
printf("1)初始化循环加头队列;\n");
InitQueue(que);
printf("2)判断循环加头队列是否为空;");
printf(EmptyQueue(que)?"ture\n":"flase\n");
printf("3)依次进队元素a,b,c;\n");
PushQueue(que,'a');
PushQueue(que,'b');
PushQueue(que,'c');
printf("4)出队一个元素,并输出该元素;");
OutQueue(que,e);
printf("%c\n",e);
printf("5)输出循环队列的长度;");
printf("%d\n",GetLength(que));
printf("6)依次进队元素d,e,f;\n");
PushQueue(que,'d');
PushQueue(que,'e');
PushQueue(que,'f');
printf("7)输出循环队列的长度;");
printf("%d\n",GetLength(que));
printf("8)输出出队序列;");
while(OutQueue(que,e)==1)
printf("%c",e);
printf("\n");
printf("9)释放队列。");
DestroyQueue(que);
}
码文不易,如果帮助到您,希望您可以帮我刷一下点击量,与您无害,与我有益谢谢 支持原创 。