首页 > 代码库 > 5.队列
5.队列
一.Queue 基本概念
队列是一种特殊的线性表
队列仅在线性表的两端进行操作
队头(Front):取出数据元素的一端
队尾(Rear):插入数据元素的一端
队列不允许在中间部位进行操作!
常用操作
- 销毁队列
- 清空队列
- 进队列
- 出队列
- 获取队头元素
- 获取队列的长度
二.队列的顺序存储设计与实现
seqlist.h
#ifndef __MY_SEQLIST_H__#define __MY_SEQLIST_H__#define DLL_API __declspec(dllexport) //_declspec(dllexport):导出标志typedef void SeqList;typedef void SeqListNode;// 创建线性表DLL_API SeqList* SeqList_Create(int capacity);// 销毁线性表DLL_API void SeqList_Destroy(SeqList *list);// 清空线性表DLL_API void SeqList_Clear(SeqList *list);// 获得线性表的长度DLL_API int SeqList_Length(SeqList *list);// 获得线性表的容量DLL_API int SeqList_Capacity(SeqList *list);// 向线性表中插入一个元素DLL_API int SeqList_Insert(SeqList *list, SeqListNode *node, int pos);// 获取线性表中某一个位置的元素DLL_API SeqListNode* SeqList_Get(SeqList *list, int pos);// 删除线性表中某一个位置的元素DLL_API SeqListNode* SeqList_Delete(SeqList *list, int pos);#endif
seqlist.c
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "seqlist.h"typedef struct _tag_SeqList{ int capacity; int length; unsigned int *node; // unsigned int array[capacity]}TSeqList;// 创建线性表SeqList* SeqList_Create(int capacity){ TSeqList *ret = NULL; if (capacity < 0) { return NULL; } ret = malloc(sizeof(TSeqList) + sizeof(unsigned int)*capacity); if (ret == NULL) { return NULL; } memset(ret, 0, sizeof(TSeqList)+sizeof(unsigned int)*capacity); ret->node = (unsigned int *)(ret + 1); // ret 向后跳转sizeof(TSeqList) ret->capacity = capacity; ret->length = 0; return ret;}// 销毁线性表void SeqList_Destroy(SeqList *list){ if (list != NULL) { free(list); }}// 清空线性表void SeqList_Clear(SeqList *list){ TSeqList *tList = NULL; if (list == NULL) { return; } tList = (SeqList*)list; tList->length = 0;}// 获得线性表的长度int SeqList_Length(SeqList *list){ TSeqList *tList = NULL; tList = (TSeqList*)list; if (tList == NULL) { return -1; } return tList->length;}// 获得线性表的容量int SeqList_Capacity(SeqList *list){ TSeqList *tList = NULL; tList = (TSeqList*)list; if (tList == NULL) { return -1; } return tList->capacity;}// 向线性表中插入一个元素DLL_API int SeqList_Insert(SeqList *list, SeqListNode *node, int pos){ int i = 0; TSeqList *tList = NULL; tList = (TSeqList*)list; // 保证传入的线性表和元素节点不能为NULL if (list == NULL || node == NULL) { return -1; } // 判断该线性表是否已满 if (tList->length >= tList->capacity) { return -2; } // 判断插入索引是否合法 if (pos < 0 || pos >= tList->capacity) { return -3; } // 若索引值大于线性表当前长度,则将元素插入到线性表的末尾 if (pos >= tList->length) { pos = tList->length; } // 插入算法 // 将pos位置后的元素移次向后移 for (i = tList->length; i > pos; i--) { // 更新后移元素的值 tList->node[i] = tList->node[i - 1]; } // 元素后移完毕后,将元素放到指定的位置 tList->node[pos] = (unsigned int)node; tList->length ++; return 0;}// 获取线性表中某一个位置的元素SeqListNode* SeqList_Get(SeqList *list, int pos){ SeqListNode *ret = NULL; TSeqList *tList = NULL; tList = (TSeqList*)list; // 过滤非法参数 if (list == NULL || pos < 0 || pos >= tList->length) { return NULL; } ret = (SeqListNode*)tList->node[pos]; return ret;}// 删除线性表中某一个位置的元素SeqListNode* SeqList_Delete(SeqList *list, int pos){ int i = 0; TSeqList *tList = NULL; SeqListNode *ret = NULL; tList = (TSeqList*)list; if (list == NULL || pos < 0 || pos >= tList->length) { return NULL; } ret = (SeqListNode*)tList->node[pos]; // 删除算法 for (i=pos+1; i<tList->length; i++) { tList->node[i - 1] = tList->node[i]; } tList->length--; return ret;}
seqqueue.h
#ifndef __MY_SEQ_QUEUE_H__#define __MY_SEQ_QUEUE_H__typedef void SeqQueue;// 创建队列SeqQueue* SeqQueue_Create(int capacity);// 销毁队列void SeqQueue_Destroy(SeqQueue* queue);// 清空队列void SeqQueue_Clear(SeqQueue* queue);// 向队尾添加元素int SeqQueue_Append(SeqQueue* queue, void* item);// 移除队列头部元素void* SeqQueue_Retrieve(SeqQueue* queue);// 获取队列头部元素void* SeqQueue_Header(SeqQueue* header);// 获取队列长度int SeqQueue_Length(SeqQueue* length);// 获取队列容量int SeqQueue_Capacity(SeqQueue* queue);#endif
seqqueue.c
#include "seqqueue.h"#include "seqlist.h"// 创建队列SeqQueue* SeqQueue_Create(int capacity){ return SeqList_Create(capacity);}// 销毁队列void SeqQueue_Destroy(SeqQueue* queue){ SeqList_Destroy(queue);}// 清空队列void SeqQueue_Clear(SeqQueue* queue){ SeqList_Clear(queue);}// 向队尾添加元素,相当于向线性表的尾部插入元素int SeqQueue_Append(SeqQueue* queue, void* item){ return SeqList_Insert(queue, item, SeqList_Length(queue));}// 移除队列头部元素void* SeqQueue_Retrieve(SeqQueue* queue){ return SeqList_Delete(queue, 0);}// 获取队列头部元素void* SeqQueue_Header(SeqQueue* queue){ return SeqList_Get(queue, 0);}// 获取队列长度int SeqQueue_Length(SeqQueue* queue){ return SeqList_Length(queue);}// 获取队列容量int SeqQueue_Capacity(SeqQueue* queue){ return SeqList_Capacity(queue);}
运行结果:
current queue capacity is: 10
current queue length is: 6
current student age is: 12
current new length is: 0
请按任意键继续. . .
5.队列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。