【数据结构】栈的实现(顺序栈)

文章目录

栈的概念和结构

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

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

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

虽然入栈和出栈都只能在栈顶进行,但是入栈和出栈的时间我们可以自行控制,比如进栈序列为 1,2,3,4 ,进栈过程中可以出栈的。

那么可以有14种出栈序列,我们讲这个就是想说明栈是:一种入栈顺序,多种出栈顺序

栈的实现

栈的实现一般可以使用数组或者链表实现,但是相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。

首先新建一个工程:

Stack.h(顺序栈的类型定义、接口函数声明、引用的头文件)

Stack.c(顺序栈接口函数的实现)

test.c(主函数、测试栈各个接口功能)

完整的代码放在后面(包括测试代码),这里就不会展示测试的效果图。大家可以自己别敲边按测试代码测试。图解会写的很详细的,么么😙

1.顺序存储结构

栈的定义

c 复制代码
// 下面是定长的静态栈的结构,实际中一般不实用,所以我们主要实现下面的支持动态增长的栈
typedef int STDataType;
#define N 10
typedef struct Stack
{
	STDataType a[N];
	int top; // 栈顶
}ST;







// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;// 栈顶
	int capacity;// 容量

}ST;

初始化栈

这里初始化的话,如果将top置为0;那么top指向的就是栈顶元素的下一个位置,如果将top置为-1,那么top指向的就是栈顶元素。上图标识了可以仔细阅读。我们这里实现的是top为0。

c 复制代码
// 初始化栈
void STInit(ST* pst)
{
	pst->a = NULL;
	pst->capacity = 0;
	//表示指向栈顶元素的下一个位置
	pst->top = 0;
	指向栈顶元素
	//pst->top = -1;
}

入栈

c 复制代码
// 入栈
void STPush(ST* pst, STDataType x)
{
	assert(pst);
	if (pst->top == pst->capacity)//判断是否满栈
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;//三字母词, pst->capacity == 0 则将4赋值给它,不为零则扩容其二倍 pst->capacity * 2
		STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc");
			return;
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}

出栈

c 复制代码
// 出栈
void STPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	pst->top--;
}

获取栈顶元素

c 复制代码
// 获取栈顶元素
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(pst->top);
	return pst->a[pst->top - 1];
}

获取栈中有效元素个数

c 复制代码
// 获取栈中有效元素个数
int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}

检测栈是否为空,如果为空返回非零结果,如果不为空返回0

c 复制代码
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}

销毁栈

c 复制代码
// 销毁栈
void STDestroy(ST* pst)
{
	assert(pst);
	free(pst -> a);
	pst->a = NULL;
	pst->top = 0;
	pst->capacity=0;
}

栈的打印

栈的打印:栈的实现,不能像顺序表一样,去实现一个打印函数来遍历栈并输出,这样就不符合栈的特点了(只能在栈顶插入删除,后进先出),所以我们这样来实现出栈:获取并打印栈顶元素,再删除栈顶元素,继续获取新的栈顶元素。我写在测试代码里面了,需要的可以去看看。

完整代码(包括测试代码)

Stack.h

c 复制代码
#pragma once
#include<stdio.h>	
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

// 下面是定长的静态栈的结构,实际中一般不实用,所以我们主要实现下面的支持动态增长的栈
//typedef int STDataType;
//#define N 10
//typedef struct Stack
//{
//	STDataType a[N];
//	int top; // 栈顶
//}ST;


typedef int STDataType;
// 支持动态增长的栈
typedef struct Stack
{
	STDataType* a;
	int top;// 栈顶
	int capacity;// 容量

}ST;

// 初始化栈
void STInit(ST* pst);
// 入栈
void STPush(ST* pst, STDataType x);
// 出栈
void STPop(ST* pst);
// 获取栈顶元素
STDataType STTop(ST* pst);
// 获取栈中有效元素个数
int STSize(ST* pst);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool STEmpty(ST* pst);
// 销毁栈
void STDestroy(ST* pst);

Stack.c

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include"Stack.h"


// 初始化栈
void STInit(ST* pst)
{
	pst->a = NULL;
	pst->capacity = 0;
	//表示指向栈顶元素的下一个位置
	pst->top = 0;
	//指向栈顶元素
	//pst->top = -1;
}
// 入栈
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, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc");
			return;
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	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);
	return pst->a[pst->top - 1];
}
// 获取栈中有效元素个数
int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}
// 销毁栈
void STDestroy(ST* pst)
{
	assert(pst);
	free(pst -> a);
	pst->a = NULL;
	pst->top = 0;
	pst->capacity=0;
}

test.c

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

int main()
{
	ST st;
	STInit(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	printf("%d \n", STTop(&st));
	STPop(&st);
	STPush(&st, 3);
	STPush(&st, 4);
	STPush(&st, 5);
	while (!STEmpty(&st))
	{
		printf("%d ", STTop(&st));
		STPop(&st);
	}
	printf("\n");


	int  r = STSize(&st);
	printf("%d\n", r);

	bool b = STEmpty(&st);
	if (b)
	{
		printf("true");

	}
	else
	{
		printf("false");
	}
	STDestroy(&st);
}
相关推荐
极客智造9 小时前
线性数据结构深度解析:数组、链表、栈与队列的实现与应用
数据结构·链表
Zhu_S W10 小时前
Redis跳表:高效有序数据结构的深度剖析
数据结构·数据库·redis
是那盏灯塔10 小时前
【算法】——动态规划之01背包问题
数据结构·c++·算法·动态规划
jinmo_C++11 小时前
数据结构_深入理解堆(大根堆 小根堆)与优先队列:从理论到手撕实现
java·数据结构·算法
Excuse_lighttime12 小时前
排序数组(快速排序算法)
java·数据结构·算法·leetcode·eclipse·排序算法
南方的狮子先生13 小时前
【数据结构】(C++数据结构)查找算法与排序算法详解
数据结构·c++·学习·算法·排序算法·1024程序员节
学编程就要猛13 小时前
数据结构初阶:Java中的ArrayList
java·开发语言·数据结构
试试勇气14 小时前
算法工具箱之双指针
数据结构
在等晚安么14 小时前
力扣面试经典150题打卡
java·数据结构·算法·leetcode·面试·贪心算法
Dobby_0515 小时前
【Go】C++转Go:数据结构练习(一)排序算法
数据结构·golang