【数据结构】队列的实现

队列

队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头
在这里插入图片描述

队列的实现

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。

Queue.h

#define _CRT_SECURE_NO_WARNINGS 1
#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(Queue* pq, QUDataType x);
void QueuePush(Queue* pq, QUDataType x); 
void QueuePop(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 = (Queue*) malloc(sizeof(QueueNode));
	if (pq->_front == NULL)
		printf("初始化失败!");
	else
		printf("初始化成功!");
	pq->_front->_next = NULL;
}

void QueueDestory(Queue* pq)
{
	while (pq->_front != NULL) {
		pq->_rear = pq->_front->_next;
		free(pq->_front);
		pq->_front = pq->_rear;
	}
	printf("摧毁队列成功...\n");
}

void QueuePush(Queue* pq, QUDataType x)
{
	QueueNode *P = (QueueNode*)malloc(sizeof(QueueNode));   
	if (P == NULL) {
		printf("内存分配失败,无法插入数据%d...",x);
		exit(-1);
	}
	P->_data = x;    
	P->_next = NULL; 
	//pq->_rear = P;
	pq->_rear->_next = P;    
	pq->_rear = P; 
	printf("插入数据成功...\n");
}

void QueuePop(Queue* pq)
{
	QueueNode*p;
	int x = QueueEmpty(pq);
	if (x == 0)
	{
		printf("队列为空无法删除!");
	}
		p = pq->_front->_next;
		pq->_front->_next = p->_next;
		if (pq->_rear == p)
			pq->_rear = pq->_front;
		free(p);    //    释放头队列
		p = NULL;    //    防止产生野指针
		printf("出栈成功\n");
}

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

int QueueSize(Queue* pq)
{
	int x = 0;
	QueueNode*p = pq->_front;
	if (QueueEmpty(pq) == 0)
		return 0;

	while (p->_next)
	{
		p = p->_next;
		x++;
	}
	return x;
}

QUDataType QueueFront(Queue* pq)
{
	printf("%d", pq->_front->_next->_data);
	return pq->_front;
}

QUDataType QueueBack(Queue* pq)
{
	QUDataType*p = pq->_front;
	printf("%d", pq->_rear->_data);
	return p;
}

QueueNode* BuyQueueNode(Queue* pq,QUDataType x)
{
	QueueNode*p = pq->_front->_next;
	if (pq->_front == pq->_rear)
		return NULL;
	while (p)
	{
		if (p->_data == x)
			return p;
	}
	return NULL;
}

textQueue.c

#include"Queue.h"

int main()
{
	int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7 };
	Queue pq;
	QueueInit(&pq);
	//QueueDestory(&pq);
	QueuePush(&pq, 5);
	QueuePush(&pq, 8);
	QueuePush(&pq, 9);
	//QueuePop(&pq);
	int t = QueueEmpty(&pq);
	printf("%d ", t);
	int y = QueueSize(&pq);
	printf("%d ", y);
	//QueueFront(&pq);
	QueueFront(&pq);
	QueueBack(&pq);
	QueueNode*p;
	p = BuyQueueNode(&pq, 5);
	printf("%d",p->_data);
	system("pause");
	return 0;

}
发布了28 篇原创文章 · 获赞 71 · 访问量 1902

猜你喜欢

转载自blog.csdn.net/famur/article/details/104987419