数据结构:栈和队列

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

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;
}
相关推荐
YSL07012432 分钟前
一文通关-----树和二叉树
数据结构·算法
小糯米60133 分钟前
数据结构 数组与链表 内存与缓存
数据结构
若亦_44 分钟前
排序>>交换排序
java·数据结构·算法
努力努力再努力wz1 小时前
【Lua 入门系列】Lua数据结构:从 Table 出发,一文串联数组、链表、栈、队列、集合、映射与树的实现原理
数据结构·链表·lua
漂流瓶jz10 小时前
UVA-1612 猜名次 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·题解·排序·aoapc·算法竞赛入门经典
A boy CDEF girl16 小时前
算法题专栏:优选算法
数据结构·算法·leetcode
图灵-M20 小时前
【数据结构】静态 & 动态顺序表
c语言·数据结构·笔记
careathers20 小时前
【数据结构】基于比较的排序算法 - 选择排序
数据结构·算法·排序算法
XiYang-DING1 天前
P4104.环形链表判断
数据结构·链表
逆向编程1 天前
MATLAB高级数据结构:cell、struct与table深度应用
开发语言·数据结构·matlab