【数据结构】栈和队列

一、栈

1.1 栈的概念及结构

栈:一种特殊的线性表,只允许再固定的一端进行插入和删除操作,进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据遵循先进后出的原则

压栈:栈插入操作叫做进栈/压栈/入栈。入数据在栈顶

出栈:栈的删除操作叫做出栈。出数据也在栈顶

无论是压栈还是出栈都是堆栈顶元素进行操作的

1.2 栈的实现

一般看可以使用数组或者链表实现,但是相对而言数组的结构实现更优一些,因为数组在尾插数据的代价比较小,并且栈的特点是先进后出,一般情况下不会进行中间元素的插入和删除,多数情况还是从栈顶将数据放到栈中(相当于链表中的尾插数据),所以数据的结构更加方便

Stack.h

cpp 复制代码
#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 StackInit(ST* pst);
//入栈
void StackPush(ST* pst,STDataType x);
// 出栈 
void StackPop(ST* pst);
// 获取栈顶元素 
STDataType StackTop(ST* pst);
// 获取栈中有效元素个数 
int StackSize(ST* pst);
// 销毁栈 
void StackDestroy(ST* pst);

Stack.c

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include"Stack.h"
//初始化栈
void StackInit(ST* st)
{
	st->a = NULL;
	st->capacity = st->top = 0;
}
//入栈
void StackPush(ST* pst, STDataType x)
{
	if (pst->top == pst->capacity)
	{
		int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newCapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = tmp;
		pst->capacity = newCapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}
// 出栈 
//判断栈是否为空
bool StackEmpty(ST* pst)
{
	return pst->top == 0;
}

void StackPop(ST* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));
	pst->top--;
}
// 获取栈顶元素 
STDataType StackTop(ST* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));
	return pst->a[pst->top - 1];
}
// 获取栈中有效元素个数 
int StackSize(ST* pst)
{
	assert(pst);
	return pst->top;

}
// 销毁栈 
void StackDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->capacity = pst->top = 0;
}

二、队列

2.1队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的特点

入队列:进行插入操作的一端称为队尾

出队列:进行删除操作的一端称为队头

2.2队列的实现

队列可以用数组实现也可以用链表实现,使用链表的结构更优一些,因为如果使用数组的结构,出队列在数组下标为0的位置上出数据,数组后面的数据需要进行大量的移动,效率会比较低,所以采用链表的方式实现队列

Queue.c

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QNode;
typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;
//初始化
void QueueInit(Queue*pq);
//插入数据
void QueuePush(Queue* pq, QDataType x);
//判断队列是否为空
bool QueueEmpty(Queue* pq);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);

Queue.h

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include"Queue.h"
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}
//插入数据
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
	//队列为空
	if (pq->ptail == NULL)
	{
		assert(pq->phead==NULL);
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;

}
//判断队列是否为空
bool QueueEmpty(Queue* pq)
{
	return pq->phead == NULL;
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	if (pq->phead == pq->ptail)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		QNode* node = pq->phead->next;
		
		free(pq->phead);
		pq->phead = node;
	}
	pq->size--;
}
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->phead->data;
}
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->ptail->data;
}
int QueueSize(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->size;
}
相关推荐
im_AMBER15 分钟前
手撕hot100之矩阵!看完这篇就AC~
javascript·数据结构·线性代数·算法·leetcode·矩阵
如君愿37 分钟前
考研复习 Day 30 | 习题--计算机网络 第五章(运输层 上)、数据结构 图(上)
数据结构·计算机网络·课后习题
weixin_4217252640 分钟前
C语言中volatile关键字怎么用C语言volatile在多线程中的作用
c语言·数据结构·运算符优先级·变量命名·volatile关键字
05候补工程师2 小时前
【408 从零到一】线性表逻辑特征、存储结构对比与 C/C++ 动态内存分配避坑指南
c语言·开发语言·数据结构·c++·考研
努力努力再努力wz3 小时前
【MySQL 进阶系列】拒绝滥用root:从 mysql.user 到权限校验,带你彻底理解用户管理与授权机制!
android·c语言·开发语言·数据结构·数据库·c++·mysql
炸膛坦客4 小时前
嵌入式 - 数据结构与算法:(1-4)数据结构 - 单链表的两个核心缺点(引入循环/双向链表)
c语言·数据结构·链表
Hesionberger4 小时前
LeetCode 78:子集生成全攻略
java·开发语言·数据结构·python·算法·leetcode·职场和发展
上弦月-编程6 小时前
高效编程利器:转移表技术解析
c语言·开发语言·数据结构·算法·排序算法
薇茗6 小时前
【初阶数据结构】 左右逢源的分支诗律 二叉树2
c语言·数据结构·算法·二叉树
Wyc724097 小时前
数据结构1
数据结构