赞
踩
hello!
目录
1、概念:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的特性。
入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为队头
2、队列底层结构的选择
队列也可以数组和链表的结构实现,使⽤链表的结构实现更优⼀些,因为如果使⽤数组的结构,出队列在数组头上出数据,效率会比较低。
- #pragma once
- #include<stdio.h>
- #include<assert.h>
- #include<stdlib.h>
- #include<stdbool.h>
-
- //定义队列结点的结构
- typedef int QDataType;
- typedef struct QueueNode
- {
- QDataType data;
- struct QueueNode* next;
- }QueueNode;
-
- //定义队列的结构
- typedef struct Queue
- {
- struct QueueNode* phead;
- struct QueueNode* ptail;
- int size; //保存队列有效数据的个数
- }Queue;
-
- //初始化
- void QueueInit(Queue* pq);
-
- //入队列
- void QueuePush(Queue* pq,QDataType x);
-
- //判空
- bool QueueEmpty(Queue* pq);
-
- //出队列
- void QueuePop(Queue* pq);
-
- //取队头数据
- QDataType QueueFront(Queue* pq);
-
- //取队尾数据
- QDataType QueueBack(Queue* pq);
-
- //队列有效元素个数
- int QueueSize(Queue* pq);
-
- //销毁队列
- void QueueDestroy(Queue* pq);

- #define _CRT_SECURE_NO_WARNINGS 1
- #include"Queue.h"
-
- //初始化
- void QueueInit(Queue* pq)
- {
- assert(pq);
- pq->phead = pq->ptail = NULL;
- pq->size = 0;
- }
-
- //入队列
- void QueuePush(Queue* pq, QDataType x)
- {
- assert(pq);
- //申请新结点
- QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
- if (newnode == NULL)
- {
- perror("malloc fail!");
- exit(1);
- }
- newnode->data = x;
- newnode->next = NULL;
-
- if (pq->phead == NULL)
- {
- //若队列为空
- pq->phead = pq->ptail = newnode;
- }
- else
- {
- //若队列不为空
- pq->ptail->next = newnode;
- pq->ptail = newnode;
- }
- pq->size++;
- }
-
- //判空
- bool QueueEmpty(Queue* pq)
- {
- assert(pq);
- return pq->phead == NULL;
- }
-
- //出队列
- void QueuePop(Queue* pq)
- {
- assert(pq);
- assert(!QueueEmpty(pq));
-
- //若队列里只有一个结点,避免ptail变成野指针
- if (pq->phead == pq->ptail)
- {
- free(pq->phead);
- pq->phead = pq->ptail = NULL;
- }
- else
- {
- //队列不止一个结点
- QueueNode* next = pq->phead->next;
- free(pq->phead);
- pq->phead = next;
- }
- --pq->size;
- }
-
- //取队头数据
- QDataType QueueFront(Queue* pq)
- {
- assert(pq);
- assert(!QueueEmpty(pq));
-
- return pq->phead->data;
- }
-
- //取队尾数据
- QDataType QueueBack(Queue* pq)
- {
- assert(pq);
- assert(!QueueEmpty(pq));
-
- return pq->ptail->data;
- }
-
- //队列有效元素个数
- int QueueSize(Queue* pq)
- {
- assert(pq);
-
- return pq->size;
- }
-
- //销毁队列
- void QueueDestroy(Queue* pq)
- {
- assert(pq);
- assert(!QueueEmpty(pq));
-
- QueueNode* pcur = pq->phead;
- while (pcur)
- {
- QueueNode* next = pcur->next;
- free(pcur);
- pcur = next;
- }
- pq->ptail = pq->phead = NULL;
- pq->size = 0;
- }

- #define _CRT_SECURE_NO_WARNINGS 1
- #include"Queue.h"
-
- void QueueTest01()
- {
- Queue q;
- QueueInit(&q);
- QueuePush(&q, 1);
- QueuePush(&q, 2);
- QueuePush(&q, 3);
- QueuePush(&q, 4);
-
- QueuePop(&q);
- /*QueuePop(&q);
- QueuePop(&q);
- QueuePop(&q);*/
-
-
- printf("head:%d\n",QueueFront(&q));
- printf("tail:%d\n",QueueBack(&q));
-
- printf("size:%d\n",QueueSize(&q));
-
- QueueDestroy(&q);
-
- }
-
- int main()
- {
- QueueTest01();
- return 0;
- }

我是云边有个稻草人
期待与你的下一次相遇!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。