栈的实现及其括号匹配问题

一、栈的基本概念及其实现

1.什么是栈?
  栈是一种特殊的线性表,只允许栈顶一段插入和删除数据,栈底就是一个容器的底子,栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
基本概念
 压栈:向栈里面放数据(我也叫给栈压数据)
 出栈:从栈顶取数据

1
我对栈的理解
  我把栈理解为一个垃圾桶,入栈把垃圾放进栈内,出栈把栈顶的垃圾取出来!
1

二、栈的数据结构实现

//定义栈
typedef int STDataType;
typedef struct Stack
{
	//存储数据的指针
	STDataType* a;
	//定义栈顶
	int top;
	//定义栈容器的大小
	int capacity;
}ST;

//下面实现常用的接口
//初始化栈,这个一定要初始化,不然栈操作问题很大!
实现的接口:
//初始化栈
void STInit(ST* ps);
//销毁栈
void STDestory(ST* ps);
//给栈压数据
void STPush(ST* ps,STDataType x);
//从栈顶出数据
void STPop(ST* ps);
//获取栈顶元素
STDataTye STTop(ST* ps);
//判断栈是否为空
bool STEmpty(ST* ps);
//获取栈的元素个数
int STSize(ST* ps);

实现上面列出的接口
//栈初始化

//初始化
void STInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	//刚开始是栈顶是0
	ps->top = 0;
}

//销毁栈

//销毁栈 
void STDestory(ST* ps)
{
	assert(ps)free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

//给栈压数据,压数据,首先要检测是否容量足够,若不够,直接扩容为原来的2倍,下面压数据

//压数据
void STPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		//扩容
		int newcapacity = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
		STDataType* tmp = (STDataType*)realloc(ps->a,newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

//出数据,要有数据出,检测栈不为空,当然,传来ps不为NULL,直接top- -

//出数据
void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}

//获取栈顶元素
//还是检测传来的指针不为NULL,栈不为空,栈若为空就没有元素取了

//获取栈顶元素
STDataType STTop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	return ps->a[ps->top - 1];
}

//判空

//判空
bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

//获取元素个数

//获取元素个数
int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

栈实现的整个代码

//stack.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

typedef int STDataType;
typedef struct Stack
{
	//存数据
	STDataType* a;
	//设置栈顶
	int top;
	//容量
	int capacity;
}ST;

//初始化
void STInit(ST* ps);
//销毁栈 
void STDestory(ST* ps);
//压数据
void STPush(ST* ps,STDataType x);
//出数据
void STPop(ST* ps);
//获取栈顶元素
STDataType STTop(ST* ps);
//判空
bool STEmpty(ST* ps);
//获取元素个数
int STSize(ST* ps);

//Stack.c
#include "Stack.h"

//初始化
void STInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	//刚开始是栈顶是0
	ps->top = 0;
}
//销毁栈 
void STDestory(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

//压数据
void STPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		//扩容
		int newcapacity = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
		STDataType* tmp = (STDataType*)realloc(ps->a,newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}
//出数据
void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}

//获取栈顶元素
STDataType STTop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	return ps->a[ps->top - 1];
}
//判空
bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

//获取元素个数
int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}


//Test.c
#include "Stack.h"

void STTest()
{
	ST st;
	STInit(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	STPush(&st, 3);
	STPush(&st, 4);
	while (!STEmpty(&st))
	{
		int Top = STTop(&st);
		printf("%d ", Top);
		STPop(&st);
	}
	STDestory(&st);
}
int main()
{
	STTest();
	return 0;
}

三、oj题括号匹配问题解答

1
//解题思路:
//1.先把左括号入栈‘(’,‘[’,‘{’,拿到左括号入栈
//2.拿到右括号‘)’,‘]’,‘}’,拿到右括号,把入栈的左括号出栈,看是否匹配,如果匹配,则继续执行上面操作,如果不匹配,则返回false。
//3.考虑如果左括号和右括号的数量一样多,则入栈,出栈,最后的栈内元素个数为0,栈为空!
//4.左括号比右括号多的问题,遇到左括号入栈,遇到右括号出栈,则一定左括号的元素没有出完,检测栈是否为空,不为空,返回false
//5.右括号比左括号多的问题,在拿到右括号出栈之前检测栈是否为空,如果为空,则右括号多,返回false!

//代码实现!

typedef char STDataType;
typedef struct Stack
{
	//存数据
	STDataType* a;
	//设置栈顶
	int top;
	//容量
	int capacity;
}ST;


//初始化
void STInit(ST* ps)
{
	ps->a = NULL;
	ps->capacity = 0;
	//刚开始是栈顶是0
	ps->top = 0;
}
//销毁栈 
void STDestory(ST* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

//压数据
void STPush(ST* ps, STDataType x)
{
	if (ps->capacity == ps->top)
	{
		//扩容
		int newcapacity = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
		STDataType* tmp = (STDataType*)realloc(ps->a,newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}
//判空
bool STEmpty(ST* ps)
{
	return ps->top == 0;
}
//出数据
void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}

//获取栈顶元素
STDataType STTop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	return ps->a[ps->top - 1];
}

//获取元素个数
int STSize(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	return ps->top;
}


bool isValid(char* s) {
    ST st;
    STInit(&st);
    while(*s)
    {
        //左括号入栈
        if(
           (*s == '(')||
           (*s == '{')||
           (*s == '[')
          )
        {
            STPush(&st,*s);
        }
        //右括号出栈
        else{
            //右括号多的问题
            if(STEmpty(&st))
            {
                STDestory(&st);
                return false;
            }
            char* Top = STTop(&st);
            STPop(&st);
            if(
                ((Top == '(')&&(*s != ')'))||
                ((Top == '{')&&(*s != '}'))||
                ((Top == '[')&&(*s != ']'))
              )
            {
                return false;
            }
        }
        s++;
    }
    //看是不是左括号多
    bool ret = STEmpty(&st);
    STDestory(&st);
    return ret;
}

完结!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值