【数据结构】链式队列的基本操作

概念:链式队列:特殊的单链表,只在单链表上进行头删和尾插操作

1、首先链式队列需要一个一个节点构成,节点的结构是,有元素,有下一个节点的地址
//定义一个节点
typedef int DataType;

typedef struct Node
{
	struct Node* _pNext;
	DataType _Data;	
}Node,*PNode;

2、链式队列结构中有两个指针pHead指向对头元素, pTail指向队尾元素
//定义一个队列结构
typedef struct	Queue
{
	PNode _pHead;
	PNode _pTail;
}Queue;
3、链式队列的几种模型
(1)、队列中没有元素

在这里插入图片描述

(2)、队列中只有一个元素

在这里插入图片描述

(3)、队列中有多个元素

在这里插入图片描述

二、代码展示

编译环境VS2013
Queue.h
#pragma once

#include <assert.h>
#include <stdio.h>
#include <malloc.h>
#include <windows.h>

typedef int DataType;

typedef struct Node
{
	struct Node* _pNext;
	DataType _Data;	
}Node,*PNode;

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

//初始化
void QueueInit(Queue* q);

//创建一个新节点
PNode BuyNode(DataType data);

//入队列
void QueuePush(Queue* q,DataType data);

//出队列
void QueuePop(Queue* q);

//队列元素的个数
int QueueSize(Queue* q);

//队首元素
DataType QueueTop(Queue* q);

//队尾元素
DataType QueueBack(Queue* q);

//判断队列是否为空
int QueueEmpty(Queue* q);

//销毁队列
void QueueDestory();

Queue.c
#include "Queue.h"

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

PNode BuyNode(DataType data)
{
	PNode pNewNode = (PNode)malloc(sizeof(Node));
	if (NULL == pNewNode)
	{
		printf("节点创建失败!!!\n");
		return NULL;
	}
	pNewNode->_pNext = NULL;
	pNewNode->_Data = data;
	return pNewNode;
}

//入队列
void QueuePush(Queue* q,DataType data)
{
	assert(q);
	if (NULL == q->_pHead)
		q->_pHead = q->_pTail = BuyNode(data);
	else
	{
		q->_pTail->_pNext = BuyNode(data);
		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 = q->_pTail = NULL;
	}
	else
	{
		PNode pDel = q->_pHead;
		q->_pHead = q->_pHead->_pNext;
		free(pDel);
	}
}

//队列元素的个数
int QueueSize(Queue* q)
{
	PNode pCur = NULL;
	int count = 0;
	assert(q);
	pCur = q->_pHead;
	while (pCur)
	{
		count++;
		pCur = pCur->_pNext;
	}
	return count;
}

//队首元素
DataType QueueTop(Queue* q)
{
	assert(q || NULL == q->_pHead);
	return q->_pHead->_Data;
}

//队尾元素
DataType QueueBack(Queue* q)
{
	assert(q || NULL == q->_pHead);
	return q->_pTail->_Data;
}

//判断队列是否为空
int QueueEmpty(Queue* q)
{
	assert(q);
	return NULL == q->_pHead;
}

//销毁队列
void QueueDestory(Queue* q)
{
	PNode pDel = NULL;
	assert(q);
	pDel = q->_pHead;
	while (pDel != NULL)
	{
		q->_pHead = q->_pHead->_pNext;
		free(pDel);
		pDel = q->_pHead;
	}
	 q->_pTail = NULL;
}
test.c
#include "Queue.h"

void test();

int main()
{
	test();
	system("pause");
	return 0;
}

void test()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	QueuePush(&q, 5);
	QueuePush(&q, 6);
	printf("size = %d\n", QueueSize(&q));
	printf("front = %d\n", QueueTop(&q));
	printf("back = %d\n", QueueBack(&q));

	QueuePop(&q);
	QueuePop(&q);

	printf("size = %d\n", QueueSize(&q));
	printf("front = %d\n", QueueTop(&q));
	printf("back = %d\n", QueueBack(&q));
	QueueDestory(&q);
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值