栈实现有效括号的匹配

链接:力扣

解析:*s为左括号就进栈, *s为右括号就让栈里的栈顶元素出栈,如果栈顶元素和*s不匹配那就return false,如果栈顶元素和*s匹配那就pop掉栈顶元素,并使s++直到*s='\0',而且要stackempty判断栈为空为止,若结束后栈内为空则return true; 若不为空,如例子*s="(",此时栈里有"(",但*s='\0'了,此例子仍然是错的,要return false;

本人版本: 

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;//法1.初始化top=-1;则top先++,再放入元素到top处,此时放完最后一个元素后,top等于最后一个元素的下标,即栈顶的下标,此时的top+1才等于capacity;
	//法2.初始化top=0;则先放元素到top处,再top++;此时放完最后一个元素,也就是放完栈顶的那个元素后,top等于栈顶(最后的那个元素)的下标+1,此时的top=capacity;
	int capacity;
}ST;

STDataType StackTop(ST* ps)
{
	assert(ps);
	return ps->a[ps->top - 1];
}




ST* CheckCapactiy(ST* ps)
{
	if (ps->top == ps->capacity)
	{
		STDataType* tmp = NULL;
		int NewCapacity = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
		tmp = (STDataType*)realloc(ps->a, sizeof(STDataType)*NewCapacity);
		if (tmp != NULL)
		{
			ps->a = tmp;//!!!注意是给ps->a开辟的空间,而不是给ps
			ps->capacity = NewCapacity;
			printf("扩容成功\n");
			return ps;//!!!记得要把ps return 回去
		}
		else
		{
			printf("扩容失败\n");
			perror("CheckCapacity::realloc");
			exit(-1);
		}
	}
	return ps;
}

void StackPush(ST* ps, STDataType x)
{
	ps = CheckCapactiy(ps);
	ps->a[ps->top] = x;
	ps->top++;
}


void StackPop(ST* ps)
{
	if (ps->top <= 0)
	{
		printf("栈为空\n");
		return;
	}
	else
	{
		ps->top--;
	}
}



void StackDestroy(ST* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

bool StackEmpty(ST* ps)//若为空则返回1;若不为空则返回负的随机值
{
	return ps->top == 0;
}

int StackSize(ST* ps)
{
	return ps->top - 1;
}






bool isValid(char* s)
{

	ST st;
	StackInit(&st);
	while (*s)
	{
		if (*s == '(' || *s == '{' || *s == '[')
		{
			StackPush(&st, *s);
			s++;
		}
		else
		{
			if (StackEmpty(&st))
			{
				StackDestroy(&st);
				return false;
			}

			STDataType top = StackTop(&st);
			StackPop(&st);

			if ((*s == ')' && top == '(') || (*s == '}' && top == '{') || (*s == ']' && top == '['))
			{
				s++;
			}
			else
			{
				StackDestroy(&st);
				return false;
			}
		}
	}
	bool ret = StackEmpty(&st);
	StackDestroy(&st);
	return ret;
}

答案版本: 

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;//法1.初始化top=-1;则top先++,再放入元素到top处,此时放完最后一个元素后,top等于最后一个元素的下标,即栈顶的下标,此时的top+1才等于capacity;
	//法2.初始化top=0;则先放元素到top处,再top++;此时放完最后一个元素,也就是放完栈顶的那个元素后,top等于栈顶(最后的那个元素)的下标+1,此时的top=capacity;
	int capacity;
}ST;

STDataType StackTop(ST* ps)
{
    assert(ps);
	return ps->a[ps->top-1];
}




ST* CheckCapactiy(ST* ps)
{
    if(ps->top==ps->capacity)
    {
	STDataType* tmp = NULL;
	int NewCapacity = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
	tmp = (STDataType*)realloc(ps->a, NewCapacity);
	if (tmp != NULL)
	{
		ps->a = tmp;//!!!注意是给ps->a开辟的空间,而不是给ps
		ps->capacity = NewCapacity;
		printf("扩容成功\n");
		return ps;//!!!记得要把ps return 回去
	}
	else
	{
		printf("扩容失败\n");
		perror("CheckCapacity::realloc");
		exit(-1);
	}
    }
    return ps;
}

void StackPop(ST* ps)
{
	if (ps->top <= 0)
	{
		printf("栈为空\n");
		return;
	}
	else
	{
		ps->top--;
	}
}

void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType)*newCapacity);
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity = newCapacity;
	}

	ps->a[ps->top] = x;
	ps->top++;
}

void StackDestroy(ST* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

bool StackEmpty(ST* ps)//若为空则返回1;若不为空则返回负的随机值
{
	return ps->top == 0;
}

int StackSize(ST* ps)
{
	return ps->top - 1;
}






bool isValid(char* s)
{
    
	ST st;
	StackInit(&st);
    while(*s)
    {
            if (*s == '(' || *s == '{' || *s == '[')
            {
                StackPush(&st, *s);	
                s++;
            }
            else
            {   
                if(StackEmpty(&st))
                {
                    StackDestroy(&st);
                    return false;
                }

                STDataType top=StackTop(&st);
                StackPop(&st);

                if ((*s== ')'&& top == '(') || (*s == '}' && top == '{') || (*s == ']' && top == '['))
                {
                    s++;
                }
                else
                {
                    StackDestroy(&st);
                    return false;
                }
            }
    }
    bool ret=StackEmpty(&st);
    StackDestroy(&st);
    return ret;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值