数据结构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;
}
相关推荐
m0_639906944 小时前
Redis入门|五种数据结构、使用场景、持久化RDB&AOF
数据结构·数据库·redis
ZhengEnCi5 小时前
Agent 架构怎么选:从单 Agent 到多 Agent 编排的选型决策指南
算法·架构
秋天的怀念_David6 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Pniubi6 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
hhb_6186 小时前
大模型落地三重优化实战
人工智能·算法·机器学习
云和数据.ChenGuang6 小时前
光哥说Python RAG 系统需要哪些评估指标:从检索到生成的全链路评测指南
网络·人工智能·python·算法·langchain
2601_970703838 小时前
工业除湿机租赁:把一份租金合同拆开看
大数据·数据结构·算法·数据库架构·推荐算法
心中有你02148 小时前
Java实现Dijkstra最短路径算法(邻接表 + 优先队列)
java·数据库·算法
ZhengEnCi9 小时前
同样的模型,为什么本地和云 API 差别那么大:量化精度、聊天模板与采样参数的三大元凶
人工智能·算法·llm
Flynn Liang9 小时前
RAG 答非所问/幻觉怎么破?Spring AI QuestionAnswerAdvisor 召回优化实战(TopK/重排/混合检索)
人工智能·算法·spring·rag·幻觉·springai