栈的各种接口的实现(C)

栈的概念

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

栈的实现

栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
用链表实现栈,栈顶在头节点,出栈和入栈用头删和头插
![[Pasted image 20240920222337.png]]

栈的定义
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;
  • 创建Stack结构体,重命名为ST,方便后续代码的书写
  • 重命名int为STDataType,方便后续对数据类型的修改
  • a是一个指针,用来表示数组
  • capacity表示栈的容量
初始化

top初始化为-1,意味着top是栈顶元素
初始化为0,意味着top是栈顶元素的下一个位置

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

![[Pasted image 20240920224845.png]]

销毁
void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
  • 判断ps是否为空
  • 释放空间
  • 指针置空
  • top和capacity也赋给0
入栈
void STPush(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)
		{
			perror ("realloc fail");
			exit(-1);
		}

		pa->a = tmp;
		ps->capacity = newCapacity;
	}

	ps->a[ps->top] = x;
	ps->top++;
}

realloc如果第一个指针是空指针,效果和malloc一样

  • assert判断ps是否为空
    如果top等于capacity,进行扩容
  • 定义新容量,如果是0,就赋给4,否则乘以2倍
  • realloc新空间赋给一个新节点tmp,判断创建空间是否成功
  • 将tmp赋给a,新容量赋给capacity
出栈
void STPop(ST* ps)
{
	assert(ps);
	
	assert(ps->top > 0);
	
	--ps->top;
}
  • 判断ps是否为空
  • 判断栈是否为空
  • 将top–
获取栈顶元素
STDataType STTop(ST* ps)
{
	assert(ps);

	assert(ps->top > 0);

	return ps->a[ps->top - 1];
}
  • 判断ps是否为空
  • 判断栈是否为空
  • 由于top初始化为0,top表示栈顶元素的下一个位置,top-1就能获取栈顶元素
返回个数
int STSize(ST* ps)
{
	assert(ps);

	return ps->top;
}
  • 判断ps是否为空
  • 直接返回top
判断是否为空
bool STEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}
  • 判断ps是否为空
  • 返回判断top是否等于0,等于0返回true,不等于0返回false

栈声明定义分离实现

#pragma once

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

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

void STInit(ST* ps);
void STDestroy(ST* ps);
void STPush(ST* ps, STDataType x);
void STPop(ST* ps);
STDataType STTop(ST* ps);
int STSize(ST* ps);
bool STEmpty(ST* ps);
void STInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

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

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

void STPush(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)
		{
			perror ("realloc fail");
			exit(-1);
		}

		pa->a = tmp;
		ps->capacity = newCapacity;
	}

	ps->a[ps->top] = x;
	ps->top++;
}

void STPop(ST* ps)
{
	assert(ps);
	
	assert(ps->top > 0);
	
	--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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值