数据结构----栈

**1.**

1.1****栈的概念及结构

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

2.栈的实现

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

1.栈的初始化

定义栈顶有两种情况:1.栈顶指向元素 2.栈顶指向元素下一个一个位置

复制代码
//初始化
void STInit(ST* ps)
{
	assert(ps);
	//top指向栈顶等于栈顶下一个元素
	ps->top = 0;

	ps->a = NULL;
	ps->capcity = 0;
	//top指向栈顶的元素
	//ps->top=-1
}

2.栈的销毁

复制代码
void StDestroy(ST* ps)//销毁
{
	assert(ps);
	ps->capcity = ps->top = 0;
	free(ps->a);
	ps->a = NULL;
}

3.入栈

复制代码
void STPush(ST* ps, STDataType x)//入栈
{
	assert(ps);
	if (ps->capcity == ps->top)
	{
		int newcapcity = ps->capcity = 0 ? 4 : ps->capcity * 2;
		STDataType* temp = (STDataType*)realloc(ps->a, newcapcity * sizeof(STDataType));
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		ps->a = temp;
		ps->capcity = newcapcity;
	}
	ps->a[ps->top++] = x;
}

4.出栈

复制代码
void STPop(ST* ps)//出栈
{
	assert(ps);
	assert(ps->top > 0);
	//return ps->a[ps->top--];
	ps->top--;
}

5.获取元素个数

复制代码
//获取元素个数
int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

6.获取栈顶数据

复制代码
//获取栈顶数据
STDataType STTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}

7.判空

复制代码
//判空
bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

3.完整代码

复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int capcity;
	int top;
}ST;
//初始化和销毁
void STInit(ST* ps);
void StDestroy(ST* ps);

//入栈和出栈
void STPush(ST* ps, STDataType x);
void STPop(ST* ps);

//获取个数
int STSize(ST* ps);

//获取栈顶数据
STDataType STTop(ST* ps);

//判空
bool STEmpty(ST* ps);

#include"Stcak.h"
//初始化
void STInit(ST* ps)
{
	assert(ps);
	//top指向栈顶等于栈顶下一个元素
	ps->top = 0;

	ps->a = NULL;
	ps->capcity = 0;
	//top指向栈顶的元素
	//ps->top=-1
}
void StDestroy(ST* ps)//销毁
{
	assert(ps);
	ps->capcity = ps->top = 0;
	free(ps->a);
	ps->a = NULL;
}
void STPush(ST* ps, STDataType x)//入栈
{
	assert(ps);
	if (ps->capcity == ps->top)
	{
		int newcapcity = ps->capcity = 0 ? 4 : ps->capcity * 2;
		STDataType* temp = (STDataType*)realloc(ps->a, newcapcity * sizeof(STDataType));
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		ps->a = temp;
		ps->capcity = newcapcity;
	}
	ps->a[ps->top++] = x;
}
void STPop(ST* ps)//出栈
{
	assert(ps);
	assert(ps->top > 0);
	//return ps->a[ps->top--];
	ps->top--;
}
//获取元素个数
int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}
//获取栈顶数据
STDataType STTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}

//判空
bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

#include"Stcak.h"
void test()
{
	ST sl;
	STInit(&sl);
	STPush(&sl, 1);
	STPush(&sl, 2);
	STPush(&sl, 3);
	while (!STEmpty(&sl))
	{
		printf("%d ", STTop(&sl));
		STPop(&sl);
	}
}
int main()
{
	test();
	return 0;
}
相关推荐
MSTcheng.22 分钟前
【数据结构】顺序表和链表详解(下)
数据结构·链表
慢半拍iii1 小时前
数据结构——F/图
c语言·开发语言·数据结构·c++
iceslime2 小时前
旅行商问题(TSP)的 C++ 动态规划解法教学攻略
数据结构·c++·算法·算法设计与分析
witton4 小时前
美化显示LLDB调试的数据结构
数据结构·python·lldb·美化·debugger·mupdf·pretty printer
chao_7894 小时前
链表题解——环形链表 II【LeetCode】
数据结构·leetcode·链表
kaiaaaa5 小时前
算法训练第十一天
数据结构·算法
-qOVOp-6 小时前
408第一季 - 数据结构 - 图II
数据结构
-qOVOp-6 小时前
408第一季 - 数据结构 - 树与二叉树III
数据结构
lifallen6 小时前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
jingfeng5146 小时前
数据结构排序
数据结构·算法·排序算法