队列---链式队列【数据结构】

本文介绍了一种基于单链表实现的链式队列数据结构,详细解释了其基本操作如创建新节点、初始化、入队列、出队列等,并提供了完整的C语言代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链式队列:

即是满足队列特性的单链表

链式队列的基本操作:

构成:

typedef struct Node
{
	QDataType _data;
	struct Node* _pNext;
}Node, *PNode;


typedef struct Queue
{
	PNode _pHead;
	PNode _pTail;
}Queue;

创建新节点:

PNode BuyNode(QDataType data)
{
	PNode pNewNode = NULL;

	pNewNode = (PNode)malloc(sizeof(Node));

	if (NULL == pNewNode)
	{
		return NULL;
	}
		pNewNode->_data = data;
		pNewNode->_pNext = NULL;

		return pNewNode;
}

初始化:

void QueueInit(Queue* q)
{
	assert(q);
	q->_pHead = NULL;
	q->_pTail = NULL;
}

入队列:

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	PNode pCur = BuyNode(data);

	if (NULL == q->_pHead && NULL == q->_pTail)
	{
		q->_pHead = pCur;
		q->_pTail = pCur;
	}
	else
	{
		q->_pTail->_pNext = pCur;
		q->_pTail = q->_pTail->_pNext;
	}
}

出队列:

void QueuePop(Queue* q)
{
	assert(q);
	
	if (NULL == q->_pHead)
	{
		printf("队空\n");
		return;
	}
	else if (q->_pHead == q->_pTail)		//只有队头
	{
		free(q->_pHead);
		q->_pHead = NULL;
		q->_pTail = NULL;
	}
	else
	{
		PNode pDel = q->_pHead;			//标记队头
		q->_pHead = pDel->_pNext;		//移动队头
		free(pDel);
	}
}

队头元素:

QDataType QueueFront(Queue* q)
{
	assert(q && q->_pHead);

	return q->_pHead->_data;
}

队尾元素:

QDataType QueueBack(Queue* q)
{
	assert(q && q->_pHead);

	return q->_pTail->_data;
}
队列销毁:

void QueueDestory(Queue*q)
{
	assert(q);
	PNode pcur = q->_pHead;
	while (pcur)
	{
		q->_pHead = pcur->_pNext;
		free(pcur);
		pcur = q->_pHead;
	}
	q->_pTail = NULL;
}
整代码:

queue.h

#pragma once

typedef int QDataType;

typedef struct Node
{
	QDataType _data;
	struct Node* _pNext;
}Node, *PNode;

typedef struct Queue
{
	PNode _pHead;
	PNode _pTail;
}Queue;

PNode BuyNode(QDataType data);
void QueueInit(Queue* q);				//初始化队列
void QueuePush(Queue* q, QDataType data);//插入
void QueuePop(Queue* q);					//队列删除
int QueueEmpty(Queue* q);					//队列判空
int QueueSize(Queue* q);					//队列大小
QDataType QueueFront(Queue* q);					//队列头元素
QDataType QueueBack(Queue* q);					//队列尾元素
void QueueDestory(Queue*q);						//队列销毁

void TestQueue();

queue.c

#include "Queue.h"
#include <stdio.h>
#include <assert.h>
#include <malloc.h>

void QueueInit(Queue* q)
{
	assert(q);
	q->_pHead = NULL;
	q->_pTail = NULL;
}

PNode BuyNode(QDataType data)
{
	PNode pNewNode;

	pNewNode =	(PNode)malloc(sizeof(Node));

	if (NULL == pNewNode)
	{
		return NULL;
	}
		pNewNode->_data = data;
		pNewNode->_pNext = NULL;

		return pNewNode;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	if (NULL == q)
	{
		return;
	}
	PNode pCur = BuyNode(data);

	if (NULL == q->_pHead && NULL == q->_pTail)
	{
		q->_pHead = pCur;
		q->_pTail = pCur;
	}
	else
	{
		q->_pTail->_pNext = pCur;
		q->_pTail = q->_pTail->_pNext;
	}
}

void QueuePop(Queue* q)
{
	assert(q);
	
	if (NULL == q->_pHead)
	{
		printf("队空\n");
		return;
	}
	else if (q->_pHead == q->_pTail)		//只有队头
	{
		free(q->_pHead);
		q->_pHead = NULL;
		q->_pTail = NULL;
	}
	else
	{
		PNode pDel = q->_pHead;			//标记队头
		q->_pHead = pDel->_pNext;		//移动队头
		free(pDel);
	}

}

QDataType QueueFront(Queue* q)
{
	assert(q && q->_pHead);

	return q->_pHead->_data;
}

QDataType QueueBack(Queue* q)
{
	assert(q && q->_pHead);

	return q->_pTail->_data;
}

int QueueSize(Queue* q)
{
	int count = 0;
	PNode pcur = q->_pHead;
	assert(q);
	while (pcur)
	{
		count++;
		pcur = pcur->_pNext;
	}
	return count;
}


int QueueEmpty(Queue* q)
{
	assert(q);
	return NULL == q->_pHead;
}

void QueueDestory(Queue*q)
{
	assert(q);
	PNode pcur = q->_pHead;
	while (pcur)
	{
		q->_pHead = pcur->_pNext;
		free(pcur);
		pcur = q->_pHead;
	}
	q->_pTail = NULL;
}

void TestQueue()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	printf("size = %d\n", QueueSize(&q));
	printf("front = %d\n", QueueFront(&q));
	printf("back = %d\n", QueueBack(&q));
	QueuePop(&q);
	QueuePop(&q);
	printf("size = %d\n", QueueSize(&q));
	printf("front = %d\n", QueueFront(&q));
	printf("back = %d\n", QueueBack(&q));
	QueueDestory(&q);
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值