力扣第20题有效的括号

cpp 复制代码
typedef char STDataType;
//动态栈
#define allocator_may_return_null 1

typedef struct ST {
    STDataType* _a;
    int _top;//栈顶元素
    int _capacity;//最大容量
}Stack;
//初始化栈
void StackInit(Stack *pst);

//入栈
void StackPush(Stack* pst, STDataType x);

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

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


//判断栈是否为空,是返回1,非空返回0
bool StackEmpty(Stack* pst);

//打印栈
void StackPrint(Stack* pst);

//销毁栈
void StackDestory(Stack* pst);



//初始化栈
void StackInit(Stack* pst)
{
	assert(pst);
	pst->_a = NULL;
	pst->_top = 0;
	pst->_capacity = 0;
}
//入栈
void StackPush(Stack* pst, STDataType x)
{
	assert(pst);
	if (pst->_top == pst->_capacity)
	{
		int newcapacity = pst->_capacity == 0 ? 4 : (pst->_capacity * 2);
		STDataType* temp = (STDataType*)realloc(pst->_a, sizeof(STDataType) * newcapacity);
		if (temp == NULL)
		{
			printf("realloc fail\n");
			exit(1);
		}
		pst->_a = temp;
		pst->_capacity = newcapacity;
	}
	pst->_a[pst->_top] = x;
	pst->_top++;
}


//出栈
void StackPop(Stack* pst)
{
	assert(pst);
	assert(pst->_top > 0);
	pst->_top--;
}

//获取栈顶元素
STDataType StackTop(Stack* pst)
{
	assert(pst);
	assert(pst->_top>0);
	return pst->_a[pst->_top-1];
}

//获取栈的有效元素个数
int StackSize(Stack* pst)
{
	assert(pst);
	return pst->_top;
}

//判断栈是否为空,是返回1,非空返回0
bool StackEmpty(Stack* pst)
{
	assert(pst);
	if (pst->_top == 0)
		return true;
	else
		return false;
}

//打印栈
void StackPrint(Stack* pst)
{
	while (!StackEmpty(pst))
	{
		printf("%d\n", StackTop(pst));
		StackPop(pst);
	}
}


//销毁栈
void StackDestory(Stack* pst)
{
	assert(pst);
	free(pst->_a);
	pst->_a = NULL;
	pst->_top = pst->_capacity = 0;
}
#include "string.h"
bool isValid(char* s) {
    Stack st;
    StackInit(&st);

    
    int n=strlen(s);
    if(n%2!=0)
    return false;

    

    for(int i=0;i<n;i++,s++){
        switch(*s){
            case '(':
            {
                 StackPush(&st,*s);
            }
            break;
            case '[':
            {
                 StackPush(&st,*s);
            }
            break;
            case '{':
            {
                 StackPush(&st,*s);
            }
            break;
            
            case ')':
            {
                if(st._top==0)
                 return false;
                 char m=StackTop(&st);
                 StackPop(&st);
                 if(m!='(')
                 {
                    return false;
                 }
            }
            break;
            case '}':
            {
                if(st._top==0)
                 return false;
                 char m=StackTop(&st);
                 StackPop(&st);
                 if(m!='{')
                 {
                    return false;
                 }
            }
            break;
            case ']':
            {
                if(st._top==0)
                 return false;
                  char m=StackTop(&st);
                 StackPop(&st);
                 if(m!='[')
                 {
                    return false;
                 }
            }
            break;
        }
    }
        
       if(st._top>0)
        {
            return false;
        }
        
        if(StackEmpty(&st)&&*s!='\0')
        return false;
StackDestory(&st);
    return true;
}
相关推荐
自由生长20246 分钟前
科普-BOM是什么?和UTF-8什么关系?
算法
小年糕是糕手25 分钟前
【数据结构】常见的排序算法 -- 插入排序
c语言·开发语言·数据结构·学习·算法·leetcode·排序算法
墨染点香43 分钟前
LeetCode 刷题【142. 环形链表 II】
算法·leetcode·链表
海琴烟Sunshine1 小时前
leetcode 263. 丑数 python
python·算法·leetcode
信仰_2739932431 小时前
Guava Cache淘汰算法
算法·guava
User_芊芊君子1 小时前
【LeetCode 经典题解】:队列与栈的双向模拟——从原理到代码详解
linux·redis·leetcode
散峰而望1 小时前
C++入门(二) (算法竞赛)
开发语言·c++·算法·github
Cx330❀1 小时前
《C++ 搜索二叉树》深入理解 C++ 搜索二叉树:特性、实现与应用
java·开发语言·数据结构·c++·算法·面试
阿猿收手吧!2 小时前
【C语言】localtime和localtime_r;strftime和strftime_l
linux·c语言·开发语言
不染尘.2 小时前
2025_11_5_刷题
开发语言·c++·vscode·算法·贪心算法·动态规划