数据结构7:栈

文章目录

头文件

Stack.h

c 复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>

#define CAPACITY_INIT 4

typedef int STDataType;

typedef struct Stack {
	STDataType* a;
	int size;
	int capacity;
}Stack;

//栈的初始化
void StackInit(Stack* ps);

//压栈
void StackPush(Stack* ps, STDataType x);

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

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

//获取栈中有效数据个数
int StackSize(Stack* ps);

//检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps);

//栈的销毁
void StackDestroy(Stack* ps);

实现文件

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

//栈的初始化
void StackInit(Stack* ps)
{
	assert(ps);
	ps->a = (STDataType*)malloc(sizeof(STDataType) * CAPACITY_INIT);
	if (ps->a == NULL)
	{
		perror("malloc fail!");
		return;
	}
	ps->size = 0;
	ps->capacity = CAPACITY_INIT;
}

//判断是否需要扩容
void StackChekCapacity(Stack* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		ps->a = (STDataType*)realloc(ps->a, sizeof(STDataType) * (ps->capacity) * 2);
		if (ps->a == NULL)
		{
			perror("realloc fail!");
			return;
		}
		ps->capacity *= 2;
	}
}

//压栈
void StackPush(Stack* ps, STDataType x)
{
	assert(ps);
	StackChekCapacity(ps);
	ps->a[ps->size++] = x;
}

//出栈
void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->size--;
}

//获取栈顶元素
STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->size - 1];
}

//获取栈中有效数据个数
int StackSize(Stack* ps)
{
	assert(ps);
	return ps->size;
}

//检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps)
{
	assert(ps);
	if (ps->size == 0)
		return 1;
	else
		return 0;
}

//栈的销毁
void StackDestroy(Stack* ps)
{
	assert(ps);
	ps->size = 0;
	ps->capacity = 0;
	free(ps->a);
}

测试文件

test.c

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
#include<stdbool.h>

//int main()
//{
//	Stack a;
//	Stack* ps = &a;
//
//	//初始化
//	StackInit(ps);
//
//	//压栈
//	StackPush(ps, 1);
//	printf("%d ", StackTop(ps));
//
//	StackPush(ps, 2);
//	printf("%d ", StackTop(ps));
//
//	StackPush(ps, 3);
//	printf("%d ", StackTop(ps));
//
//	StackPush(ps, 4);
//	printf("%d ", StackTop(ps));
//
//	StackPop(ps);
//	printf("%d ", StackTop(ps));
//
//
//
//
//	//销毁
//	StackDestroy(ps);
//
//	return 0;
//}

经典题目

c 复制代码
bool isValid(char* s) {
    Stack a;
    Stack* ps = &a;
    StackInit(ps);
    int len = strlen(s);
    for (int i = 0; i < len; i++)
    {
        char tmp = *(s + i);
        if (tmp == '(' || tmp == '[' || tmp == '{')
        {
            StackPush(ps, tmp);
        }
        else
        {
            if (StackEmpty(ps))
                return false;
            char left = StackTop(ps);
            if (left == '(')
                left = ')';
            else if (left == '[')
                left = ']';
            else
                left = '}';
            StackPop(ps);
            if (left != tmp)
                return false;
        }
    }
    if (!StackEmpty(ps))
        return false;
    StackDestroy(ps);
    return true;
}
相关推荐
Hi李耶9 分钟前
【LeetCode】4-寻找两个正序数组的中位数
算法·leetcode·职场和发展
图灵机z21 分钟前
【算法提高课】AcWing 1027. 方格取数 长期攻克提高课-day3(3/219)
算法
hans汉斯31 分钟前
计算机科学与应用|改进MeanShift算法在智能监控视频中的应用研究
图像处理·人工智能·功能测试·深度学习·算法·音视频
来一碗刘肉面1 小时前
树的存储结构
数据结构
一次旅行1 小时前
AutoAWQ完整实战:MIT激活感知AWQ量化,模型显存减半、推理提速且精度无损
人工智能·python·算法
GrowthDiary0072 小时前
算法题:寻找二维数组top k问题
数据结构·python·算法
可编程芯片开发2 小时前
基于全阶观测器的三自由度运动系统状态反馈控制simulink建模与仿真
算法
kobesdu2 小时前
从零推导FAST-LIO的观测雅可比矩阵
人工智能·算法·矩阵
SHARK_pssm2 小时前
【数据结构——栈】
数据结构
乐思智能科技有限公司2 小时前
PLECS软件学习使用(二)直流电机基本系统模型
人工智能·算法·机器学习·面试·职场和发展