使用链表来实现队列的基本操作(c语言版)

一:创建头文件

           

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
#include<stdbool.h>

typedef int QDataType;

typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType val;
}QNode;

typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

void QueueInit(Queue* pq);//创建队列
void QueueDestroy(Queue* pq);//销毁队列

// 队尾插入
void QueuePush(Queue* pq, QDataType x);
// 队头删除
void QueuePop(Queue* pq);

// 取队头和队尾的数据
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);

int QueueSize(Queue* pq);//获取队列大小
bool QueueEmpty(Queue* pq);//判空

1.关于assert头文件的引用:

 assert()函数可以判断括号内的指针是否为空,为空则会直接终止程序的运行,这也叫暴力停止。一些使用if语句判断其是否往下进行的则较为温和。

2.关于Queue结构体的创立:

队列最基本的头指针与尾指针我就不多赘述了,我在这里额外定义了一个size,代表队列的大小,可以用来编写获取队列大小的函数。

二:实现函数

          1:部分思路

       (1)难点1:队列的销毁

                需要再定义一个指针变量,来指向pq的头指针的下一个,然后销毁pq指针,令pq = 该指针变量

void QueueDestroy(Queue* pq)//队列的销毁
{
	assert(pq);
	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

      (2) 难点2 :队头的删除,需要分仅一个节点和多个节点的情况

           

void QueuePop(Queue* pq)//队头删除
{
	assert(pq);
	assert(pq->size != 0);
	if (pq->phead->next == NULL)//仅有一个节点的时候
	{
		free(pq);
		pq->phead = pq->ptail = NULL;
	}
	else //有多个节点的时候
	{
		QNode* tmp = pq->phead->next;
		free(pq->phead);
		pq = tmp;
	}
}  

    (3)难点3:队尾数据的插入

               需要再定义一个链表,然后将数据插入进去,最后将其与队尾相连接。连接前还需要判断队列中是否含有元素,如若没有元素则队头等于队尾等于该链表,若有则正常插入。

void QueuePush(Queue* pq, QDataType x)//队尾的插入
{
	assert(pq);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}

	newnode->next = NULL;
	newnode->val = x;

	if (pq->ptail == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}

	pq->size++;
}

2:完整代码

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
#include<stdbool.h>

typedef int QDataType;

typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType val;
}QNode;

typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

void QueueInit(Queue* pq);//创建队列
void QueueDestroy(Queue* pq);//销毁队列

// 队尾插入
void QueuePush(Queue* pq, QDataType x);
// 队头删除
void QueuePop(Queue* pq);

// 取队头和队尾的数据
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);

int QueueSize(Queue* pq);//获取队列大小
bool QueueEmpty(Queue* pq);//判空
#include"team.h"

void QueueInit(Queue* pq)//队列的建立
{
	assert(pq);
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

void QueueDestroy(Queue* pq)//队列的销毁
{
	assert(pq);
	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, QDataType x)//队尾的插入
{
	assert(pq);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}

	newnode->next = NULL;
	newnode->val = x;

	if (pq->ptail == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}

	pq->size++;
}

void QueuePop(Queue* pq)//队头删除
{
	assert(pq);
	assert(pq->size != 0);
	if (pq->phead->next == NULL)//仅有一个节点的时候
	{
		free(pq);
		pq->phead = pq->ptail = NULL;
	}
	else //有多个节点的时候
	{
		QNode* tmp = pq->phead->next;
		free(pq->phead);
		pq = tmp;
	}
}  

QDataType QueueFront(Queue* pq)//获取队头值
{
	assert(pq);
	assert(pq->phead);
}

QDataType QueueBack(Queue* pq)//获取队尾值
{
	assert(pq);
	assert(pq->ptail);
	return pq->ptail->val;
}


int QueueSize(Queue* pq)//获取队列大小
{
	assert(pq);
	return pq->size;
}

bool QueueEmpty(Queue* pq)//判空
{
	assert(pq);
	return pq->size = 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值