数据结构——栈

栈是一种特殊的线性表,在固定的一端进行插入和删除,这端也成为栈顶,而不能进行插入删除的一端叫做栈底。 

声明

typedef int STDatatype;
typedef struct Stack
{
	STDatatype* a;
	int capacity;
	int top;
}ST;

 初始化

void StackInit(ST* ps)
{
//不初始化空间
	/*ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;*/
	//初始化空间
	ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);
	if (ps->a == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	ps->top = 0;
	ps->capacity = 4;
}

Push

入栈,往栈里存放数据。

检查空间

static void check_capacity(ST* ps)
{
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity * 2;
		STDatatype* tmp = (STDatatype*)realloc(ps->a, newCapacity *sizeof(STDatatype));
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capacity == newCapacity;
		}
	}
}
void StackPush(ST* ps, STDatatype x)
{
	assert(ps);
	check_capacity(ps);
	ps->a[ps->top] = x;
	ps->top++;//指向下一个
}

销毁

void StackDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

 判空

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

Pop

出栈,删除栈顶元素。

void StackPop(ST* ps)//出栈
{
	assert(ps);
	//if(ps->top>0)
	assert(!StackEmpty);
		ps->top--;
}

栈顶元素 

STDatatype StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty);
	return ps->a[ps->top-1];//top为最后一个元素的下一个
}

Size

返回栈的元素个数。

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

有的人会说,可以直接用ps.top来得到栈的个数,为什么还要封装,答案是封装可以不关注实现,我们这里是的top指向的是当前元素的下一个位置,那如果我从-1开始(没有元素),得到的top也不一样,需要注意这点。

	StackSize(&ps);
	//ps.top

完整代码 

//stack.cpp
#include "Stack.h"
void StackInit(ST* ps)
{
	/*ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;*/
	//初始化空间
	ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);
	if (ps->a == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	ps->top = 0;
	ps->capacity = 4;
}

static void check_capacity(ST* ps)
{
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity * 2;
		STDatatype* tmp = (STDatatype*)realloc(ps->a, newCapacity *sizeof(STDatatype));
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capacity == newCapacity;
		}
	}
}
void StackPush(ST* ps, STDatatype x)
{
	assert(ps);
	check_capacity(ps);
	ps->a[ps->top] = x;
	ps->top++;//指向下一个
}
void StackPop(ST* ps)//出栈
{
	assert(ps);
	//if(ps->top>0)
	assert(!StackEmpty(ps));
		ps->top--;
}

STDatatype StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top-1];//top为最后一个元素的下一个
}
void StackDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
bool StackEmpty(ST* ps)//判空
{
	assert(ps);
	return ps->top == 0;
}
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}
//stack.h
#pragma once
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef int STDatatype;
typedef struct Stack
{
	STDatatype* a;
	int capacity;
	int top;
}ST;

void StackInit(ST* ps);
void StackDestroy(ST* ps);
void StackPush(ST* ps, STDatatype x);
void StackPop(ST* ps);//出栈
STDatatype StackTop(ST* ps);

bool StackEmpty(ST* ps);
int StackSize(ST* ps);
//test.cpp
#include "Stack.h"
void TestStack1()
{
	ST ps;
	StackInit(&ps);
	StackPush(&ps, 1);
	StackPush(&ps, 1);
	StackPush(&ps, 1);
	StackPush(&ps, 1);
	StackPush(&ps, 1);
	StackDestroy(&ps);
}
void TestStack2()
{
	ST ps;
	StackInit(&ps);
	StackPush(&ps, 2);
	printf("%d",StackTop(&ps));
	StackPop(&ps);
	StackSize(&ps);
	//ps.top
	StackDestroy(&ps);

}

int main()
{
	//TestStack1();
	TestStack2();

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小C您好

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

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

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

打赏作者

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

抵扣说明:

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

余额充值