数据结构—栈的实现

我们要实现的栈的功能有栈的初始化、栈的销毁、入栈、出栈、判断栈是否为空、返回栈顶元素、计算栈中元素个数这七个函数

首先先创建一个结构体:

这是固定大小的栈

#define N 100

//栈中元素类型重定义(就是给int换个名字)
typedef int STDataType;

typedef struct Stack
{
	STDataType a[N];//能存储N个元素的栈
	int top;//记录栈中元素的个数
}ST;

也可以是动态的,且我们这种讲动态版本的,可以扩容的

typedef int STDataType;

typedef struct Stack
{
	STDataType* a;//栈
	int top;//记录栈中元素个数
	int capacity;//栈中能容纳的元素个数
}ST;

栈的初始化:

void StackInit(ST* ps)
{
	assert(ps);//断言ps是否为空,为空则报错

	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

栈的销毁:

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

	free(ps->a);//释放掉栈
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

入栈:

void StackPush(ST* ps, STDataType x)
{
	assert(ps);

	if (ps->top == ps->capacity)//判断是否需要扩容
	{
        //如果原来的容量为0则开辟四个元素的容量,否则开辟原来容量的二倍
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);
		if (tmp == NULL)
		{
            //如果开辟空间失败则退出
			printf("realloc fail\n");
			exit(-1);
		}
        //把新开辟的空间的指针赋值给栈
		ps->a = tmp;
        更新容量
		ps->capacity = newCapacity;
	}
    
	ps->a[ps->top] = x;
	ps->top++;
}

判断栈是否为空

bool StackEmpty(ST* ps)
{
	assert(ps);
    //如果top等于0返回true,表示栈为空,反之返回false,表示栈中不为空
	return ps->top == 0;
}

出栈

void StackPop(ST* ps)
{
	assert(ps);
    //如果栈为空则报错(栈中没有元素不能出栈)
	assert(!StackEmpty(ps));
	ps->top--;
}

返回栈顶元素

STDataType StackTop(ST* ps)
{
	assert(ps);
    //栈中没有元素不能返回栈顶元素
	assert(!StackEmpty(ps));
    
    //栈顶元素是栈中元素个数减一
	return ps->a[ps->top - 1];
}

返回栈中元素个数

int StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

再看一下完整的源码

头文件Stack.h:

#pragma once//防止头文件被反复包含

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>



typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}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);

源文件Stack.c

#include "Stack.h"

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

void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 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, sizeof(STDataType) * newCapacity);
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			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->top--;
}

STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}

bool StackEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}

int StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值