当前位置: 首页 > news >正文

数据结构:详解【栈和队列】的实现

目录

  • 1. 栈
    • 1.1 栈的概念及结构
    • 1.2 栈的实现
    • 1.3 栈的功能
    • 1.4 栈的功能的实现
    • 1.5 完整代码
  • 2. 队列
    • 2.1 队列的概念及结构
    • 2.2 队列的实现
    • 2.3 队列的功能
    • 2.4 队列的功能的实现
    • 2.5 完整代码

1. 栈

1.1 栈的概念及结构

:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

压栈:栈的插入操作叫做进栈/压栈/入栈。入数据在栈顶
出栈:栈的删除操作叫做出栈。出数据也在栈顶

在这里插入图片描述
在这里插入图片描述

1.2 栈的实现

栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾插和头删的实际复杂度为O(1),是非常合适的。

在这里插入图片描述

1.3 栈的功能

  • 初始化栈
  • 销毁栈
  • 入栈
  • 出栈
  • 获取栈顶元素
  • 获取栈内有效元素的个数
  • 判断栈内是否为空,如果为空返回非0结果,不为空返回0

1.4 栈的功能的实现

(1)定义一个动态增长的栈

typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	STDataType top;//定义栈顶
	size_t capacity;//栈的容量
}ST;

(2)初始化栈

void StackInit(ST* ps)
{
	//初始化空间
	 ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (ps->a == NULL)
	{
		printf("malloc fail!\n");
		return;
	}

	ps->top = 0;
	ps->capacity = 4;//初始化4个空间
}

(3)销毁栈

void StackDestory(ST* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

(4)入栈(相当于顺序表的尾插)

void StackPush(ST* ps, STDataType x)
{
	//插入数据之前判断是否增容
	if (ps->top == ps->capacity)
	{
		STDataType* tmp = (STDataType*)realloc(ps->a, ps->capacity * sizeof(STDataType) * 2);
		if (tmp == NULL)
		{
			printf("realloc fail!\n");
			return;
		}
		else
		{
			ps->a = tmp;
			ps->capacity *= 2;
		}
	}

	ps->a[ps->top] = x;
	ps->top++;
}

(5)出栈(相当于顺序表的头删)

void StackPop(ST* ps)
{
	assert(ps);//断言,栈内为空则终止程序

	ps->top--;
}

(6)获取栈顶元素

STDataType StackTop(ST* ps)
{
	assert(ps);//断言,栈内为空则终止程序
	assert(ps->top > 0);

	return ps->a[ps->top - 1];
}

(7)获取栈内有效元素的个数

int StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

(8)判断栈内是否为空,如果为空返回非0结果,不为空返回0

bool StackEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}

1.5 完整代码

Stack.h

#define _CRT_SECURE_NO_WARNINGS 

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

typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	STDataType top;//定义栈顶
	size_t capacity;//栈的容量
}ST;

//初始化栈
void StackInit(ST* ps);

//销毁栈
void StackDestory(ST* ps);

//从栈顶插入数据
void StackPush(ST* ps, STDataType x);

//从栈顶删除数据
void StackPop(ST* ps);

//获取栈顶元素
STDataType StackTop(ST* ps);

//获取栈内有效元素个数
int StackSize(ST* ps);

//判断栈内是否为空,如果为空返回非0结果,不为空返回0
bool StackEmpty(ST* ps);

Stack.c

#define _CRT_SECURE_NO_WARNINGS 

#include "Stack.h"

void StackInit(ST* ps)
{
	//初始化空间
	 ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (ps->a == NULL)
	{
		printf("malloc fail!\n");
		return;
	}

	ps->top = 0;
	ps->capacity = 4;//初始化4个空间
}

void StackDestory(ST* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

void StackPush(ST* ps, STDataType x)
{
	//插入数据之前判断是否增容
	if (ps->top == ps->capacity)
	{
		STDataType* tmp = (STDataType*)realloc(ps->a, ps->capacity * sizeof(STDataType) * 2);
		if (tmp == NULL)
		{
			printf("realloc fail!\n");
			return;
		}
		else
		{
			ps->a = tmp;
			ps->capacity *= 2;
		}
	}

	ps->a[ps->top] = x;
	ps->top++;
}


void StackPop(ST* ps)
{
	assert(ps);//断言,栈内为空则终止程序

	ps->top--;
}

STDataType StackTop(ST* ps)
{
	assert(ps);//断言,栈内为空则终止程序
	assert(ps->top > 0);

	return ps->a[ps->top - 1];
}

int StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

bool StackEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}

Test.c

#define _CRT_SECURE_NO_WARNINGS 

#include "Stack.h"

void StackTest()
{
	ST st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);

	//打印栈内的数据,由于不能破坏栈的特性,所以不能遍历
	while (!StackEmpty(&st))
	{
		printf("%d ", StackTop(&st));
		StackPop(&st);
	}

	StackDestory(&st);
}

int main()
{
	StackTest();

	return 0;
}

2. 队列

2.1 队列的概念及结构

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

2.2 队列的实现

队列也可以数组和链表的结构实现,使用单链表的结构实现更优一些。
因为如果使用数组的结构,出队列在数组头上出数据,这时需要挪动数据,时间复杂度为O(n),效率会比较低。
而单链表的尾插和头删的时间复杂度为O(1),十分合适。
在这里插入图片描述

2.3 队列的功能

  • 初始化队列
  • 销毁队列
  • 入队列
  • 出队列
  • 获取队列头部元素
  • 获取队列尾部元素
  • 获取队列中有效元素的个数
  • 判断队列是否为空,为空返回非0,不为空返回0

2.4 队列的功能的实现

(1)定义一个队列

typedef int QTDataType;

typedef struct QNode
{
	struct QNode* next;
	QTDataType data;
}QNode;

typedef struct Queue
{
	struct QNode* tail;
	struct QNode* head;
}Queue;

(2)初始化队列

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
}

(3)销毁队列

void QueueDestory(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		cur = cur->next;
		free(cur);
	}
	pq->head = pq->tail = NULL;
}

(4)入队列(相当于单链表的尾插)

void QueuePush(Queue* pq, QTDataType x)
{
	assert(pq);

	QNode* newnode = (QTDataType*)malloc(sizeof(QTDataType));
	if (newnode == NULL)
	{
		printf("malloc fail!\n");
		return;
	}

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

	//第一个结点
	if (pq->tail == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	//多个节点
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}

}

(5)出队列(相当于单链表的头删)

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head);

	//只有一个节点
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		//保存下一个节点的地址
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}

(6)获取队列头部元素

QTDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->head);

	return pq->head->data;
}

(7)获取队列尾部元素

QTDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->head);

	return pq->tail->data;
}

(8)获取队列中有效元素的个数

int QueueSize(Queue* pq)
{
	assert(pq);
	int size = 0;
	QNode* cur = pq->head;
	while (cur)
	{
		size++;
		cur = cur->next;
	}

	return size;
}

(9)判断队列是否为空,为空返回非0,不为空返回0

int QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->head == NULL;
}

2.5 完整代码

Queue.h

#pragma once

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

typedef int QTDataType;

typedef struct QNode
{
	struct QNode* next;
	QTDataType data;
}QNode;

typedef struct Queue
{
	struct QNode* tail;
	struct QNode* head;
}Queue;

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

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

//队尾入队列
void QueuePush(Queue* pq, QTDataType x);

//队头出队列
void QueuePop(Queue* pq);

//获取队列头部元素
QTDataType QueueFront(Queue* pq);

//获取队列尾部元素
QTDataType QueueBack(Queue* pq);

//判断队列是否为空,为空返回非0,不为空返回0
int QueueEmpty(Queue* pq);

//获取队列中有效元素的个数
int QueueSize(Queue* pq);

Queue.c

#define _CRT_SECURE_NO_WARNINGS 

#include "Queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
}

void QueueDestory(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		cur = cur->next;
		free(cur);
	}
	pq->head = pq->tail = NULL;
}

void QueuePush(Queue* pq, QTDataType x)
{
	assert(pq);

	QNode* newnode = (QTDataType*)malloc(sizeof(QTDataType));
	if (newnode == NULL)
	{
		printf("malloc fail!\n");
		return;
	}

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

	//第一个结点
	if (pq->tail == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	//多个节点
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}

}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head);

	//只有一个节点
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		//保存下一个节点的地址
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}

int QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->head == NULL;
}

QTDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->head);

	return pq->head->data;
}

QTDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->head);

	return pq->tail->data;
}

int QueueSize(Queue* pq)
{
	assert(pq);
	int size = 0;
	QNode* cur = pq->head;
	while (cur)
	{
		size++;
		cur = cur->next;
	}

	return size;
}

Test.c

#define _CRT_SECURE_NO_WARNINGS 

#include "Queue.h"

void QueueTest()
{
	Queue q;
	QueueInit(&q);
	
	QueuePush(&st, 1);
	QueuePush(&st, 2);
	QueuePush(&st, 3);
	QueuePush(&st, 4);
	
    //打印队列内的数据,由于不能破坏队列的特性,所以不能遍历
	while (!QueueEmpty(&st))
	{
		printf("%d ", QueueFront(&st));
		QueuePop(&st);
	}

	QueueDestory(&q);

}


int main()
{
	QueueTest();

	return 0;
}

相关文章:

  • 贪心算法(算法竞赛、蓝桥杯)--奶牛晒衣服
  • 基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的水下目标检测系统(深度学习模型+UI界面+训练数据集)
  • mapbox 获取当前比例尺 scale
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • Temu,藏在拼多多财报里的中国制造红利
  • 鸿蒙Harmony应用开发—ArkTS-全局UI方法(时间滑动选择器弹窗)
  • 大数据-基础架构设施演进的过程
  • golang实现枚举
  • AWS中所有与数据科学有关的服务一览表(MLS-C01)
  • <爬虫部署,进阶Docker>----第二章 安装Docker
  • 几个不错的 Jupyter Notebook 云端展示平台
  • 基于springboot的牙科就诊管理系统
  • Lua | 一篇文章讲清Lua语法及热更新
  • dfs剪枝
  • Java类的多态作用及解析
  • 【设计模式】第二讲:单例模式
  • NoSQL
  • Vue+SpringBoot打造陕西非物质文化遗产网站
  • python练习3
  • 外包干了5天,技术退步明显。。。。
  • 兵韬志略|美2026国防预算未达1万亿,但仍寻求“暗度陈仓”
  • 云南省安委会办公室:大理州安全生产形势比较严峻,事故总量一直居高不下
  • 名帅大挪移提前开启,意属皇马的阿隆索会是齐达内第二吗
  • 巴基斯坦首都及邻近城市听到巨大爆炸声
  • 东方红资管官宣:41岁原国信资管董事长成飞出任新总经理
  • 新华每日电讯:给“男性妇科病论文”开一剂复方药