C实现队列

Queue.h

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

typedef int QUDataType;
typedef struct QueueNode
{
	struct QueueNode* _next;
	QUDataType _data;
}QueueNode;
typedef struct Queue
{
	QueueNode* _front; // 队头
	QueueNode* _rear; // 队尾
}Queue;

void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
QueueNode* BuyQueueNode(QUDataType x);

void QueuePush(Queue* pq, QUDataType x);
void QueuePop(Queue* pq);
void QueuePrint(Queue* pq);

QUDataType QueueFront(Queue* pq);
QUDataType QueueBack(Queue* pq);

int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);

void TestQueue();

Queue.c

#include"Queue.h"

void QueueInit(Queue* pq)
{
	pq->_front = pq->_rear = NULL;
}

void QueueDestory(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->_front;
	QueueNode* next = cur;
	while (next)
	{
		next = cur->_next;
		free(cur);
		cur = next;
	}
	pq->_front = pq->_rear = NULL;
	cur = NULL;
}

QueueNode* BuyQueueNode(QUDataType x)
{
	QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
	newNode->_data = x;
	newNode->_next = NULL;
	return newNode;
}

void QueuePush(Queue* pq, QUDataType x)
{
	assert(pq);
	QueueNode* newNode = BuyQueueNode(x);
	if (pq->_rear == NULL)
	{
		pq->_front = pq->_rear = newNode;
	}
	else
	{
		pq->_rear->_next = newNode;
		pq->_rear = newNode;
	}
}

void QueuePop(Queue* pq)
{
	assert(pq);
	if (pq->_front == NULL)
	{
		return;
	}
	else
	{
		QueueNode* old = pq->_front;
		pq->_front = pq->_front->_next;
		free(old);
		old = NULL;
		if (pq->_front == NULL)
		{
			pq->_rear = NULL;
		}
	}
}

void QueuePrint(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->_front;
	while (cur)
	{
		printf("%d -> ", cur->_data);
		cur = cur->_next;
	}
	printf("\n");
}

QUDataType QueueFront(Queue* pq)
{
	assert(pq);
	if (pq->_front != NULL)
	{
		return pq->_front->_data;
	}
	else
	{
		return 0;
	}
}

QUDataType QueueBack(Queue* pq)
{
	assert(pq);
	if (pq->_rear != NULL)
	{
		return pq->_rear->_data;
	}
	else
	{
		return 0;
	}
}

int QueueEmpty(Queue* pq)
{
	if (pq->_front != NULL && pq->_rear == NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int QueueSize(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->_front;
	int size = 0;
	while (cur)
	{
		size++;
		cur = cur->_next;
	}
	return size;
}

void TestQueue()
{
	Queue pq;
	QueueInit(&pq);
	QueuePush(&pq, 1);
	QueuePush(&pq, 2);
	QueuePush(&pq, 3);
	QueuePush(&pq, 4);
	QueuePush(&pq, 5);
	QueuePush(&pq, 6);
	QueuePrint(&pq);

	QueuePop(&pq);
	QueuePrint(&pq);

	printf("%d ", QueueFront(&pq));
	printf("\n");
	printf("%d ", QueueBack(&pq));
	printf("\n"); 
	printf("%d ", QueueSize(&pq));
	printf("\n");
	printf("%d ", QueueEmpty(&pq));
	printf("\n");

	QueueDestory(&pq);
}

int main()
{
	TestQueue();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值