栈的概念及结构

栈:

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

  • 压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
  • 出栈:栈的删除操作叫做出栈,出数据也在栈顶。
    Alt

栈的实现

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

定义一个栈的结构体

typedef int STDataType;
typedef struct Stack
{
	STDataType* _a;
	int _top;
	int _capacity;
}Stack;

初始化

//初始化
void StackInit(Stack* pst)
{
	assert(pst);
	pst->_a = malloc(sizeof(Stack)*4);
	pst->_top = 0;
	pst->_capacity = 4;
}

销毁

//销毁
void StackDestory(Stack* pst)
{
	assert(pst);
	free(pst->_a);
	pst->_a = NULL;
	pst->_top = pst->_capacity = 0;
}

进栈

//进栈
void StackPush(Stack* pst, STDataType x)
{
	assert(pst);
	if (pst->_top == pst->_capacity)
	{
		pst->_capacity *= 2;
		Stack* tmp = (Stack*)realloc(pst->_a, sizeof(Stack) * pst->_capacity);
	}
	pst->_a[pst->_top] = x;
	pst->_top++;
}

出栈

//出栈
void StackPop(Stack* pst)
{
	assert(pst);
	assert(pst->_top != 0);
	pst->_top--;
}

获取数据个数

//获取数据个数
int StackSize(Stack* pst)
{
	assert(pst);
	return pst->_top;
}

返回1是空,返回0是非空

//返回1是空,返回0是非空
int StackEmpty(Stack* pst)
{
	assert(pst);
	return pst->_top == 0 ? 1 : 0;
}

获取栈顶数据

//获取栈顶数据
STDataType StackTop(Stack* pst)
{
	assert(pst);
	assert(pst->_top > 0);
	return pst->_a[pst->_top - 1];
}

总代码

Stack.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int STDataType;
typedef struct Stack
{
	STDataType* _a;
	int _top;
	int _capacity;
}Stack;

//初始化
void StackInit(Stack* pst);
//销毁
void StackDestory(Stack* pst);
//进栈
void StackPush(Stack* pst, STDataType x);
//出栈
void StackPop(Stack* pst);
//获取数据个数
int StackSize(Stack* pst);
//返回1是空,返回0是非空
int StackEmpty(Stack* pst);
//获取栈顶数据
STDataType StackTop(Stack* pst);

Stack.c

#include"stack.h"

//初始化
void StackInit(Stack* pst)
{
	assert(pst);
	pst->_a = malloc(sizeof(Stack)*4);
	pst->_top = 0;
	pst->_capacity = 4;
}
//销毁
void StackDestory(Stack* pst)
{
	assert(pst);
	free(pst->_a);
	pst->_a = NULL;
	pst->_top = pst->_capacity = 0;
}
//进栈
void StackPush(Stack* pst, STDataType x)
{
	assert(pst);
	if (pst->_top == pst->_capacity)
	{
		pst->_capacity *= 2;
		Stack* tmp = (Stack*)realloc(pst->_a, sizeof(Stack) * pst->_capacity);
	}
	pst->_a[pst->_top] = x;
	pst->_top++;
}
//出栈
void StackPop(Stack* pst)
{
	assert(pst);
	assert(pst->_top != 0);
	pst->_top--;
}
//获取数据个数
int StackSize(Stack* pst)
{
	assert(pst);
	return pst->_top;
}
//返回1是空,返回0是非空
int StackEmpty(Stack* pst)
{
	assert(pst);
	return pst->_top == 0 ? 1 : 0;
}
//获取栈顶数据
STDataType StackTop(Stack* pst)
{
	assert(pst);
	assert(pst->_top > 0);
	return pst->_a[pst->_top - 1];
}

test.c

#include"stack.h"

int main()
{
	Stack pst;
	StackInit(&pst);
	StackPush(&pst, 1);
	StackPush(&pst, 2);
	StackPush(&pst, 3);
	StackPop(&pst);
	while (!StackEmpty(&pst))
	{
		printf("%d ", StackTop(&pst));
		StackPop(&pst);
	}
	printf("/n");
	StackDestory(&pst);
}

栈的作用:

  1. 如果有后进先出需求的地方,比如迷宫问题。
  2. 递归改成非递归。
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值