【数据结构】 -- 队列

引入

队列是一种线性表,队列遵循先进先出的特性。先放入队列的数据,一定先出队列。

倘若数据以ABCD的顺序入队,出队的顺序一定也是ABCD。

队列的实现

队列要求先入先出,用数组实现要挪数据,效率非常低。双向链表实现非常方便但是占用内存比单链表多,所以在这里我们选择用单链表实现。

头文件:

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

typedef int QDataType;

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

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


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

//队头删除
void QueuePop(Queue* pq);

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

//销毁
void QueueDestory(Queue* pq);

//显示数据个数
int QueueSize(Queue* pq);

//取队头
QDataType QueueFront(Queue* pq);

//取队尾
QDataType QueueBack(Queue* pq);

//判空
bool QueueEmpty(Queue* pq);

 这里的关键在于重新设置一个结构体Queue用来操作phead和ptaill,如果没有这个结构体,需要传入二级指针来实现,会更加复杂。同时Queue中的size还可以记录数据的个数;如果没有size需要计数器遍历链表才能得到链表的长度。

图示:

实现文件:

 

#define _CRT_SECURE_NO_WARNINGS 1

#include "Queue.h"


void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}


//队尾插入
void QueuePush(Queue* pq, QDataType x)
{
	//扩容
	QNode* newNode = (QNode*)malloc(sizeof(QNode));
	if (newNode == NULL)
	{
		perror("mailloc fail");
		exit(-1);
	}
	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++;

}


//显示数据个数
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

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

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

	return pq->phead->val;

}

//取队尾
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->ptail);

	return pq->ptail->val;
}



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


//销毁
void QueueDestory(Queue* pq)
{
	assert(pq);

	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* cmp = pq->phead->next;
		free(cur);
		cur = cmp;
		pq->size--;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

测试文件:

 

#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"


int main()
{
	Queue s;
	QueueInit(&s);
	QueuePush(&s, 1);
	QueuePush(&s, 2);
	QueuePush(&s, 3);

	int b = QueueFront(&s);
	QueuePush(&s, 4);
	
	QueuePop(&s);


	QueueDestory(&s);
	return 0;
}

相关推荐

  1. 数据结构-队列

    2024-06-06 17:46:03       55 阅读
  2. 数据结构队列

    2024-06-06 17:46:03       68 阅读
  3. 数据结构-队列

    2024-06-06 17:46:03       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 17:46:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 17:46:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 17:46:03       82 阅读
  4. Python语言-面向对象

    2024-06-06 17:46:03       91 阅读

热门阅读

  1. C++基础-编程练习题和答案(数组)

    2024-06-06 17:46:03       27 阅读
  2. 查看电脑品牌

    2024-06-06 17:46:03       36 阅读
  3. Android基础-AndroidManifest.xml详解

    2024-06-06 17:46:03       26 阅读
  4. 说明 1px、1em、1rem、1vw、1vh 的区别

    2024-06-06 17:46:03       81 阅读
  5. springboot中使用RestTemplate 请求http接口

    2024-06-06 17:46:03       32 阅读
  6. 上传code至github的步骤

    2024-06-06 17:46:03       29 阅读
  7. 电脑问题和解决方法记录

    2024-06-06 17:46:03       24 阅读
  8. MyEclipse 新手使用教程

    2024-06-06 17:46:03       35 阅读
  9. 深度解读:Apache Kafka如何超越消息引擎的界限

    2024-06-06 17:46:03       25 阅读
  10. C#语言进阶(二)—事件 第三篇(事件访问器)

    2024-06-06 17:46:03       34 阅读
  11. WebRTC 在 iOS 端实现一对一通信

    2024-06-06 17:46:03       26 阅读