当前位置: 首页 > news >正文

数据结构––队列

1.队列的定义

2.队列的分类

2.1循环队

2.2链式队

3.队列的实现

3.1循环队

3.1.1声明

typedef int QDataType;
#define MAXSIZE 50  //定义元素的最大个数
/*循环队列的顺序存储结构*/
typedef struct {QDataType *data;int front;  //头指针int rear;   //尾指针
}Queue;

3.1.2初始化

void QueueInit(Queue* q) 
{q->data = (QDataType*)malloc(sizeof(QDataType )* MAXSIZE);if (q->data == NULL){perror("malloc fail:");return;}q->front = 0;q->rear = 0;
}

3.1.3判断队列是否为空

bool QueueEmpty(Queue*q)
{assert(q);return q->front == q->rear;
}

3.1.4判断队列是否为满

bool QueueFull(Queue*q)
{assert(q);return q->front == (q->rear+1)%MAXSIZE;
}

3.1.5入队

void QueuePush(Queue* q, QDataType x) 
{assert(q);if(QueueFull){printf("队列已满\n");return ;}q->data[q->rear] = x;   q->rear = (q->rear + 1) % MAXSIZE;  //rear指针向后移一位置,若到最后则转到数组头部
}

3.1.6出队

void QueuePop(Queue* q){assert(q);assert(!QueueEmpty(q));q->front = (q->front + 1) % MAXSIZE;    //front指针向后移一位置,若到最后则转到数组头部}

3.1.7打印队列

void QueuePrint(Queue* q){assert(q);int cur = q->front;printf("队头->");while (cur != q->rear){printf("%d->", q->data[cur]);cur = (cur + 1) % MAXSIZE;}printf("队尾\n");}

3.1.8销毁队列

void QueueDestroy(Deque* q)//销毁队列
{assert(q);free(q->data);q->data = NULL;q->front = q->rear = 0;
}

3.2链

3.2.1声明

typedef int QDataType;
typedef struct QueueNode 
{QDataType data;struct QueueNode* next;
}QNode;
typedef struct Queue 
{QNode* front;QNode* rear;size_t size;
}Queue;

3.2.2初始化

void QueueInit(Queue* q)
{q->front = NULL;q->rear = NULL;q->size = 0;
}

3.2.3判断队列是否为空

bool QueueEmpty(Queue* q)
{assert(q);return (q->front == NULL) && (q->rear == NULL);
}
void QueuePush(Queue* q, QDataType x)
{assert(q);QNode* newnode = (QNode*)malloc(sizeof(QNode));newnode->data = x;newnode->next = NULL;if (newnode == NULL){perror("malloc fail");return;}if (q->front == NULL){q->front = q->rear = newnode;}else{q->rear->next = newnode;q->rear = newnode;}q->size++;
}

3.2.4出队

void QueuePop(Queue* q)
{assert(q);assert(!QueueEmpty(q));//1.只有一个结点if (q->front == q->rear){free(q->front);q->front = q->rear = NULL;}//2.有多个结点else{QNode* del = q->front;q->front = q->front->next;free(del);del = NULL;}q->size--;
}

3.2.5打印队列

void QueuePrint(Queue* q)
{assert(q);QNode* cur = q->front;QNode* tail = q->rear;printf("队头->");while (cur != tail->next){printf("%d->",cur->data);cur = cur->next;}printf("队尾\n");
}

3.2.6销毁队列

void QueueDestroy(Queue* q)
{assert(q);QNode* cur = q->front;while (cur){QNode* del = cur;cur = cur->next;free(del);del = NULL;}q->front = q->rear = NULL;
}

http://www.lryc.cn/news/341579.html

相关文章:

  • 010_redhat安装zookeeper
  • 【网络】gateway 可以提供的一些功能之一 “ 提供web静态资源服务 ”
  • MySQL第一次作业
  • 详解LLMOps,将DevOps用于大语言模型开发
  • 牛客NC275 和为S的两个数字【简单 map C++/Java/Go/PHP】
  • ax200/ax201/ax210/ax211/ax411等intel网卡无法开启5G热点问题解决方案汇总
  • JVM的垃圾回收机制(GC机制)
  • 分布式光伏管理系统和一般的光伏管理系统相比有什么区别?
  • Linux migrate_type进一步探索
  • 强化学习:时序差分法【Temporal Difference Methods】
  • 数据结构-二叉树-二叉搜索树
  • Linux 磁盘管理命令df du dd
  • Leetcode 3138. Minimum Length of Anagram Concatenation
  • IT廉连看——UniApp——样式绑定
  • 垃圾的flinkcdc
  • 关于视频号小店,常见问题解答,开店做店各方面详解
  • Debian mariadb 10.11设定表名 大小写不敏感方法
  • 常用六大加密软件排行榜|好用加密文件软件分享
  • 百川2模型解读
  • 云原生专栏丨基于K8s集群网络策略的应用访问控制技术
  • MySQL 优化 - index_merge 导致查询偶发变慢
  • SpringBoot自动连接数据库的解决方案
  • Docker-10 Docker Compose
  • new mars3d.control.MapSplit({实现点击卷帘两侧添加不同图层弹出不同的popup
  • 数据库中虚拟表和临时表的区别?
  • Node.js -- mongoose
  • 保持亮灯:监控工具如何确保 DevOps 中的高可用性
  • DRF版本组件源码分析
  • C#算法之希尔排序
  • 校园餐厅预约系统(请打开git自行访问)