简单实现一个栈

什么是栈

一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除的一端称为栈顶,另一端称为栈低。栈中的数据元素遵守后进先出LIFO的原则。
栈的插入操作称为压栈或进栈,栈的删除操作称为退栈或出栈。栈的主要特点是“后进先出(last in first out,LIFO)”。

栈的性质

在操作系统中,栈是向下生长的。栈顶和栈底不是上下决定,而是有入栈方向决定!
(1)集合性。栈是由多个元素集合而成,当没有元素的空集合称为空栈;
(2)线性结构。除栈底元素和栈顶元素外,栈中任一元素均有唯一的前驱元素和后继元素;
(3)受限制的运算。只允许在栈顶实施压入或弹出操作,且栈顶位置由栈指针所指示;
(4)数学性质。当多个编号元素依某种顺序压入,且可任意时刻弹出时,所获得的编号元素排列的数目,恰好满足Catalan数列(组合数学数列)的计算

入栈和出栈

栈的插入操作称为压栈或进栈,栈的删除操作称为退栈或出栈。栈的主要特点是“后进先出(last in first out,LIFO)”。
在这里插入图片描述

初始化栈

void StackInit(ST* ps);

void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

栈的销毁

void StackDestory(ST* ps);

void StackDestory(ST* ps)
{
	assert(ps);
	free(ps->a);//释放
	ps->a = NULL;//置空
	ps->capacity = ps->top = 0;//容量置0
}

入栈

void StackPush(ST* pS, STDataType x);

void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{

			perror("realloc fail");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newCapacity;
	}
		ps->a[ps->top] = x;
		ps->top++;
}

出栈

void StackPop(ST* ps);

void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));//ps不为空才Pop
	--ps->top;
}

获取栈顶元素

STDataType StackTop(ST* ps);

STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];//访问top的前一个位置
}

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

bool StackEmpty(ST* ps);

bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;

}

获取栈顶中有效元素个数

int StackSize(ST* ps);

int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

Stack.h的代码:

#pragma once
#include<stdbool.h>
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int STDataType;
typedef struct  stack
{
	STDataType* a;//定义一个数组a
	int top;//标识插入栈顶的位置
	int capacity;//容量
}ST;
//初始化栈
void StackInit(ST* ps);
//栈的销毁
void StackDestory(ST* ps);
//入栈
void StackPush(ST* pS, STDataType x);
//出栈
void StackPop(ST* ps);
//获取栈顶元素
STDataType StackTop(ST* ps);
//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps);
//获取栈顶中有效元素个数
int StackSize(ST* ps);

Stack.c的代码:

#include"Stack.h"
void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
void StackDestory(ST* ps)
{
	assert(ps);
	free(ps->a);//释放
	ps->a = NULL;//置空
	ps->capacity = ps->top = 0;//容量置0
}
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{

			perror("realloc fail");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newCapacity;
	}
		ps->a[ps->top] = x;
		ps->top++;
}
void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));//ps不为空才Pop
	--ps->top;
}
STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];//访问top的前一个位置
}
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;

}
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

test.c的代码:

#include"Stack.h"
void TestStack()
{
	ST st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);

	printf("%d ", StackTop(&st));
	StackPop(&st);
	printf("%d ", StackTop(&st));
	StackPop(&st);

	StackPush(&st, 4);
	StackPush(&st, 5);
	while (!StackEmpty(&st))
	{
		printf("%d ", StackTop(&st));
		StackPop(&st);
	}
	printf("\n");
}
int main()
{
	TestStack();
	return 0;
}

栈的性质是后进先出或者说先进后出,在test.c代码块中1、2、3是先进的,先出2个元素就是3和2,4和5又是后进的,所以要比1先出,正确的顺序是32541.
在这里插入图片描述

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神之子-小佳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值