数据结构:栈和队列

由于栈和队列结构逻辑较为简单,此文只做简单介绍并附上代码参考

1. 栈

1.1 栈的概念

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

1.2 栈的实现

栈的实现可以使用数组和链表结构,使用数组更优,因为数组在尾上插入删除数据上代价小一些

1.2.1 Stack.h

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

//初始化
void STInit(ST* pst);
//销毁
void STDestory(ST* pst);
//入栈 出栈
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
//取栈顶元素
STDataType STTop(ST* pst);
//判空
bool STEmpty(ST* pst);
//数据个数
int STSize(ST* pst);

1.2.2 Stack.c

cpp 复制代码
#include"Stack.h"

//初始化
void STInit(ST* pst)
{
	assert(pst);

	pst->a = NULL;
	//top赋值为0表示栈顶元素的下一个元素
	pst->top = 0;
	pst->capacity = 0;
}

//销毁
void STDestory(ST* pst)
{
	assert(pst);

	free(pst->a);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}
//入栈 出栈
void STPush(ST* pst, STDataType x)
{
	assert(pst);

	//扩容
	if (pst->top == pst->capacity)
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("STPush::realloc fail");
			return;
		}
		pst->capacity = newcapacity;
		pst->a = tmp;
	}

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

void STPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);

	pst->top--;
}

//取栈顶元素
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);

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

//判空
bool STEmpty(ST* pst)
{
	assert(pst);

	return pst->top == 0;
}

//数据个数
int STSize(ST* pst)
{
	assert(pst);

	return pst->top;
}

1.2.3 test.c

cpp 复制代码
#include"Stack.h"

int main()
{
	ST s;
	STInit(&s);
	STPush(&s, 1);
	STPush(&s, 2);
	STPush(&s, 3);
	STPush(&s, 4);

	printf("%d\n", STTop(&s));
	//printf("%d\n", s.a[s.top]);  切忌这样访问 因为top不代表栈顶元素

	while (!STEmpty(&s))
	{
		printf("%d ", STTop(&s));
		STPop(&s);
	}
	STDestory(&s);
	return 0;
}

2. 队列

2.1 队列概念

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

2.2 队列的实现

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

2.2.1 Queue.h

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

typedef int QDataType;

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

//将头尾指针单独封装结构体 可避免使用二级指针 简化逻辑 还可减少函数参数
typedef struct Queue
{
	QueueNode* phead;
	QueueNode* ptail;
	int size;
}Queue;

//初始化
void QueueInit(Queue* pq);
//队列尾插
void QueuePush(Queue* pq, QDataType x);
//队列头删
void QueuePop(Queue* pq);
//队列数据个数
int QueueSize(Queue* pq);
//取队头数据
QDataType QueueFront(Queue* pq);
//取队尾数据
QDataType QueueBack(Queue* pq);
//判空
bool QueueEmpty(Queue* pq);
//队列销毁
void QueueDestory(Queue* pq);

////以下形式也可使用 但由于使用二级指针 较为复杂 不推荐
//void QueuePush(QueueNode** pphead, QueueNode** pptail, QDataType x);
//void QueuePop(QueueNode** pphead, QueueNode** pptail);

2.2.2 Queue.c

cpp 复制代码
#include"Queue.h"

//初始化
void QueueInit(Queue* pq)
{
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}
//队列尾插
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		perror("QueuePush::malloc fail");
		return;
	}
	newnode->next = NULL;
	newnode->val = x;
	//队列无节点
	if (pq->phead == NULL)
	{
		pq->phead = pq->ptail = newnode;
		pq->size++;
	}
	//队列有节点
	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->phead);
		pq->phead = pq->ptail = NULL;
	}
	//多个节点
	else
	{
		QueueNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	pq->size--;
}
//队列数据个数
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}
//取队头数据
QDataType QueueFront(Queue* pq)
{
	assert(pq);

	return pq->phead->val;
}
//取队尾数据
QDataType QueueBack(Queue* pq)
{
	assert(pq);

	return pq->ptail->val;
}
//判空
bool QueueEmpty(Queue* pq)
{
	return pq->size == 0;
}
//队列销毁
void QueueDestory(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->phead;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

2.2.3 test.c

cpp 复制代码
#include"Queue.h"

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

	//QueuePop(&q);
	while (!QueueEmpty(&q))
	{
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");
	return 0;
}
相关推荐
CSharp精选营20 小时前
关系型 vs 非关系型:从原理到选型,一文搞定数据库核心分类
数据结构·nosql·关系型数据库·非关系型数据库·技术选型
刘马想放假4 天前
Modbus 全栈技术解析:TCP、RTU、ASCII、RTU over TCP
数据结构·网络协议
北域码匠5 天前
冒泡排序太慢?鸡尾酒排序双向优化,原生 C# 零第三方库完整代码
数据结构·排序算法·泛型·c# 算法·鸡尾酒排序·原生 c# 开发·冒泡排序优化·嵌入式算法
Darling噜啦啦12 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
小小工匠13 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾13 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres82113 天前
算法复键——树状数组
数据结构·算法
牛油果子哥q13 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒13 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
WL学习笔记13 天前
单项不带头不循环链表
数据结构·链表