数据结构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;
}
相关推荐
wuyk5551 小时前
AI 时代的嵌入式该怎么学-11-C不是语法是对内存的显式建模
c语言·开发语言·stm32·单片机·嵌入式硬件·mcu·算法
吞下星星的少年·-·1 小时前
LeetCode 热题 100 最长连续序列(哈希表)
算法·leetcode·散列表
风落青芜1 小时前
C++入门:类和对象(三)
开发语言·数据结构·c++·算法
Pniubi2 小时前
力扣25.K个一组反转链表
算法·leetcode·链表
16月6日-晴2 小时前
力扣22题,括号生成
算法
老唐7773 小时前
具身智能视觉感知详解:从图像识别到机器人环境理解,核心技术、算法架构与应用实践
人工智能·深度学习·算法·机器人·大模型·具身智能
杜 硕3 小时前
从零开始的C++生活——第十五讲
开发语言·数据结构·c++·生活
小白的高手之路3 小时前
【LeetCode 热题 100】560. 和为 K 的子数组|前缀和、哈希表(Python)
数据结构·笔记·python·算法·leetcode·力扣·散列表
专注仿真4 小时前
仿真智能体训练(评估篇):鲁棒性/泛化测试算法详解与实战
人工智能·python·算法·afsim智能体训练·评估算法·鲁棒性算法·泛化测试算法