嘿,妖怪!栈住别跑!!(有关栈的实现,含习题)

一 .栈


    1.概念与结构


        1)概念:
            栈:⼀种特殊的线性表,其只允许在固定的⼀端进⾏插⼊和删除元素操作。进⾏数据插⼊和删除操作的⼀端称为栈顶,另⼀端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
            压栈:栈的插⼊操作叫做进栈/压栈/⼊栈,⼊数据在栈顶。
            出栈:栈的删除操作叫做出栈。出数据也在栈顶。

        2)结构:栈的实现⼀般可以使⽤数组或者链表实现,相对⽽⾔数组的结构实现更优⼀些。因为数组在尾上插⼊数据的代价⽐较⼩。

stack.h----头文件

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

//定义栈的结构
typedef int STDataType;
typedef struct Stack
{
	STDataType* arr;
	int capacity; //栈的空间大小
	int top; //栈顶
}ST;

//栈的初始化
void STInit(ST* ps);

//判断栈是否为空
bool StackEmpty(ST* ps);

//入栈
void StackPush(ST* ps, STDataType x);

//出栈
void StackPop(ST* ps);

//取栈顶元素
STDataType StackTop(ST* ps);

//栈的销毁
void STDestroy(ST* ps);

//获取栈中有效元素个数
int STSize(ST* ps);

stack.c----源文件

#include"stack.h"

//栈的初始化
void STInit(ST* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}

//判断栈是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

//入栈
void StackPush(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->arr, newcapacity * sizeof(STDataType));
		if (tmp==NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
	//空间足够
	ps->arr[ps->top++] = x;
}

//出栈
void StackPop(ST* ps)
{
	assert(ps);
	//判断栈是否没有数据,是空栈
	//assert(ps->arr)
	assert(!StackEmpty(ps));

	--ps->top;
}

//取栈顶元素
STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->arr[ps->top - 1];
}

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

//栈的销毁
void STDestroy(ST* ps)
{
	assert(ps);
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}


    2.有关习题


        有效的括号(leetcode/OJ平台20)
        [思路:遍历字符串,遍历到左括号,入栈,遍历到右括号,取栈顶元素,并进行比较,匹配*ps,出栈,ps++;不匹配,返回false]

 #include<stdio.h>
          #include<stdbool.h>
          #include<stdlib.h>
         //首先定义一个栈,并写出栈的相关操作
          typedef int STDataType;
          typedef struct Stack
         {
          STDataType* arr;
          int capacity; //栈的空间大小
        int top; //栈顶
        }ST;
        
        void STInit(ST* ps)
        {
        assert(ps);
        ps->arr = NULL;
        ps->capacity = ps->top = 0;
        }

//判断栈是否为空
bool StackEmpty(ST* ps)
{
    assert(ps);
    return ps->top == 0;
}

//入栈
void StackPush(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->arr, newcapacity * sizeof(STDataType));
        if (tmp == NULL)
        {
            perror("realloc fail!");
            exit(1);
        }
        ps->arr = tmp;
        ps->capacity = newcapacity;
    }
    //空间足够
    ps->arr[ps->top++] = x;
}

//出栈
void StackPop(ST* ps)
{
    assert(ps);
    //判断栈是否没有数据,是空栈
    //assert(ps->arr)
    assert(!StackEmpty(ps));

    --ps->top;
}

//取栈顶元素
STDataType StackTop(ST* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));

    return ps->arr[ps->top - 1];
}

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

//栈的销毁
void STDestroy(ST* ps)
{
    assert(ps);
    if (ps->arr)
    {
        free(ps->arr);
    }
    ps->arr = NULL;
    ps->top = ps->capacity = 0;
}

bool isValid(char* s)
{
    ST st;
    STInit(&st);

    //遍历字符串s
    char* ps = s;
    while (*ps != '\0')
    {
        //左括号入栈
        if (*ps == '(' || *ps == '[' || *ps == '{')
        {
            StackPush(&st, *ps);
        }
        else
        {
            //右括号,比较
            //为空(只有一个左括号)直接false
            if (StackEmpty(&st))
            {
                return false;
            }
            char a = StackTop(&st);
            if ((*ps == ')' && a == '(')
                || (*ps == ']' && a == '[')
                || (*ps = '}' && a == '{'))
            {
                StackPop(&st);
            }
            else
            {
                STDestroy(&st);
                return false;
            }
        }
        ps++;
    }
    bool ret = StackEmpty(&st) == true;
    //销毁
    STDestroy(&st);
    return ret;
}


         

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值