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

文章目录

栈的概念和结构

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出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);
}
相关推荐
小字节,大梦想44 分钟前
【C++】二叉搜索树
数据结构·c++
我是哈哈hh1 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
丶Darling.2 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo5202 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
Indigo_code2 小时前
【数据结构】【链表代码】合并有序链表
数据结构·windows·链表
jiyisuifeng19912 小时前
代码随想录训练营第54天|单调栈+双指针
数据结构·算法
我言秋日胜春朝★2 小时前
【C++】红黑树
数据结构
新晓·故知3 小时前
<基于递归实现线索二叉树的构造及遍历算法探讨>
数据结构·经验分享·笔记·算法·链表
gorgor在码农3 小时前
Mysql 索引底层数据结构和算法
数据结构·数据库·mysql
武昌库里写JAVA4 小时前
【Java】Java面试题笔试
c语言·开发语言·数据结构·算法·二维数组