数据结构––队列

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;
}
相关推荐
自然常数e18 分钟前
深入理解指针(6)
c语言·数据结构·算法·visual studio
一杯美式 no sugar25 分钟前
数据结构——栈
c语言·数据结构·
蒙奇D索大1 小时前
【数据结构】考研408 | 冲突解决精讲: 拉链法——链式存储的艺术与优化
数据结构·笔记·考研·改行学it
一直都在5721 小时前
数据结构入门:二叉排序树的构建与相关算法
数据结构·算法
_Minato_2 小时前
数据结构知识整理——复杂度的计算
数据结构·经验分享·笔记·算法·软考
月明长歌2 小时前
【码道初阶】【LeetCode 102】二叉树层序遍历:如何利用队列实现“一层一层切蛋糕”?
java·数据结构·算法·leetcode·职场和发展·队列
聆风吟º2 小时前
【数据结构手札】顺序表实战指南(一):线性表定义 | 顺序表定义
数据结构·顺序表·线性表
啊董dong3 小时前
noi-2025年12月16号作业
数据结构·c++·算法·noi
white-persist3 小时前
【攻防世界】reverse | simple-check-100 详细题解 WP
c语言·开发语言·汇编·数据结构·c++·python·算法
长安er3 小时前
LeetCode 01 背包 & 完全背包 题型总结
数据结构·算法·leetcode·动态规划·背包问题