C学习(数据结构)-->栈

目录

一、概念与结构

二、 栈的实现

1、 栈的结构

2、栈的初始化与销毁

初始化

销毁

 3、判断栈顶是否在栈底的位置

4、入栈与出栈

入栈

出栈

 三、总代码

头文件 Stack.h

函数 Stack.cpp

测试 main.cpp


一、概念与结构

栈是一种特殊的线性表,只允许在固定的一段进行元素的插入和删除。进行数据插入和删除的一端称作栈顶,另一端称为栈底。栈的数据元素遵循后进先出的原则

压栈:栈的插入操作叫做 进栈/压栈/入栈,入数据在栈顶

出栈:栈的删除操作叫做出栈。出数据在栈顶。

 栈一般通过数组(顺序表)或链表实现。

二、 栈的实现

本文章通过顺序表实现栈,顺序表详情见C学习(数据结构)-->顺序表-CSDN博客

1、 栈的结构

//定义栈的结构
typedef int STDataType;
typedef struct Stack
{
	STDataType* arr;
	int capacity;//当前空间大小
	int top;//指向栈顶的,同时也是有效数据个数
}ST;

2、栈的初始化与销毁

初始化

//初始化
void STInit(ST* ps)
{
	assert(ps);

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

销毁

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

	if (ps->arr)
	{
		free(ps->arr);
	}

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

 3、判断栈顶是否在栈底的位置

//判断栈顶是否在栈底的位置
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

4、入栈与出栈

入栈

//栈的入栈
void StackPush(ST* ps, STDataType x)
{
	assert(ps);

	//判断空间是否足够
	if (ps->capacity == ps->top)
	{
		int newCapacity = (ps->capacity == 0) ? 4 : 2 * ps->capacity;
		//扩展数组空间
		STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!!!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
	//空间足够
	ps->arr[ps->top++] = x;
}

出栈

//出栈
void StackPop(ST* ps)
{
	assert(ps);
	//判断栈顶是否在栈底的位置
	assert(!StackEmpty(ps));

	--ps->top;
}

 三、总代码

头文件 Stack.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1

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

//定义栈的结构
typedef int STDataType;
typedef struct Stack
{
	STDataType* arr;
	int capacity;//当前空间大小
	int top;//指向栈顶的,同时也是有效数据个数
}ST;

//初始化
void STInit(ST* ps);

//栈的销毁
void STDestroy(ST* ps);

//栈的入栈
void StackPush(ST* ps, STDataType x);

//判断栈顶是否在栈底的位置
bool StackEmpty(ST* ps);

//出栈
void StackPop(ST* ps);

//取栈顶元素
STDataType StackTop(ST* ps);

//获取栈顶中有效元素个数
int Size(ST* ps);

函数 Stack.cpp

#include"Stack.h"

//初始化
void STInit(ST* ps)
{
	assert(ps);

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

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

	if (ps->arr)
	{
		free(ps->arr);
	}

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

//栈的入栈
void StackPush(ST* ps, STDataType x)
{
	assert(ps);

	//判断空间是否足够
	if (ps->capacity == ps->top)
	{
		int newCapacity = (ps->capacity == 0) ? 4 : 2 * ps->capacity;
		//扩展数组空间
		STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!!!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
	//空间足够
	ps->arr[ps->top++] = x;
}

//判断栈顶是否在栈底的位置
bool StackEmpty(ST* ps)
{
	assert(ps);
	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->arr[ps->top - 1];
}

//获取栈顶中有效元素个数
int Size(ST* ps)
{
	assert(ps);
	return ps->top;
}

测试 main.cpp

#include"Stack.h"

void StackTest01()
{
	ST st;
	STInit(&st);

	//入栈
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPush(&st, 5);
	//有效元素个数
	printf("size: %d\n", Size(&st));
	//循环出栈,直到栈为空
	while (!StackEmpty(&st))
	{
		STDataType data = StackTop(&st);
		printf("%d ", data);

		//出栈
		StackPop(&st);
	}

	//有效元素个数
	printf("size: %d\n", Size(&st));

	//栈的销毁
	STDestroy(&st);
}

int main()
{
	StackTest01();
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值