栈PART 1

目录

[1. 栈](#1. 栈)

[1.1 栈的概念和结构](#1.1 栈的概念和结构)

[1.2 栈的实现](#1.2 栈的实现)

[1.2.1 栈的顺序储存结构](#1.2.1 栈的顺序储存结构)

[1.2.2 栈的基本操作](#1.2.2 栈的基本操作)

[1.3 有效的括号](#1.3 有效的括号)


1. 栈

1.1 栈的概念和结构

堆栈又名栈(stack),它是一种运算受限的线性表。

限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。

向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;

从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。

入数据在栈顶,出数据也在栈顶。

1.2 栈的实现

栈的实现一般可以使用数组或者链表来实现,两者相比较的话,数组的结构实现更优。

1.2.1 栈的顺序储存结构

cpp 复制代码
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

1.2.2 栈的基本操作

(1)初始化

cpp 复制代码
//Stack.h

//初始化
void STInit(ST* ps);
cpp 复制代码
//Stack.c

void STInit(ST* ps)
{
	assert(ps);

	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

注意:这里 top 的值可以是0,或者是-1。

两种写法的区别在于:

这两种写法没有好坏之分。

(2)销毁

cpp 复制代码
//Stack.h

//销毁
void STDestroy(ST* ps);
cpp 复制代码
//Stack.c

void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

(3)入栈

cpp 复制代码
//Stack.h

//入栈
void STPush(ST* ps, STDataType x);
cpp 复制代码
//Stack.c

void STPush(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, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}

		ps->a = tmp;
		ps->capacity = newcapacity;  //更新栈的容量
	}

	ps->a[ps->top] = x;
	ps->top++;  //将元素x插入栈中,并且将栈顶指针上移一位
}

(4)出栈

cpp 复制代码
//Stack.h

//出栈
void STPop(ST* ps);
cpp 复制代码
//Stack.c

void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));  //栈不为空

	ps->top--;  //栈顶指针向后移一位
}

(5)读取栈顶元素

cpp 复制代码
//Stack.h

//读取栈顶元素
STDataType STTop(ST* ps);
cpp 复制代码
//Stack.c

STDataType STTop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));

	return ps->a[ps->top - 1];  //因为前面定义的 top = 0
}

(6)返回栈的长度

cpp 复制代码
//Stack.h

//返回栈的长度
int STSize(ST* ps);
cpp 复制代码
//Stack.c

int STSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

(7)判断栈是否为空

cpp 复制代码
//Stack.h

//判断栈是否为空
bool STEmpty(ST* ps);
cpp 复制代码
//Stack.c

bool STEmpty(ST* ps)
{
	assert(ps);
    if(ps->top == 0)
        return true;
    else
        return false;
}

1.3 有效的括号

示例代码:

cpp 复制代码
typedef char STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

//初始化
void STInit(ST* ps);
//销毁
void STDestroy(ST* ps);

//入栈
void STPush(ST* ps, STDataType x);
//出栈
void STPop(ST* ps);

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

//栈的大小
int STSize(ST* ps);

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

void STInit(ST* ps)
{
	assert(ps);

	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

void STPush(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, 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];
}

int STSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

bool STEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}

bool isValid(char* s) {
    ST st;
    STInit(&st);
    while(*s)
    {
        if (*s == '(' || *s == '[' || *s == '{')
        {
            STPush(&st, *s);
        }
        else
        {
            //右括号比左括号多
            if(STEmpty(&st))
            {
                STDestroy(&st);
                return false;
            }

            char top = STTop(&st);
            STPop(&st);

            //符号不匹配
            if((*s == ']' && top != '[')
             || (*s == ')' && top != '(')
             || (*s == '}' && top != '{'))
            {
                STDestroy(&st);
                return false;
            }
        }

        ++s;
    }
    //左括号比右括号多,栈不为空
    bool ret = STEmpty(&st);
    STDestroy(&st);
    return ret;
}
相关推荐
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
今后1232 天前
【数据结构】二叉树的概念
数据结构·二叉树
元亓亓亓2 天前
LeetCode热题100--105. 从前序与中序遍历序列构造二叉树--中等
算法·leetcode·职场和发展
小莞尔2 天前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing102329292 天前
Day03_刷题niuke20250915
c语言
第七序章2 天前
【C++STL】list的详细用法和底层实现
c语言·c++·自然语言处理·list
仙俊红2 天前
LeetCode每日一题,20250914
算法·leetcode·职场和发展
l1t2 天前
利用DeepSeek实现服务器客户端模式的DuckDB原型
服务器·c语言·数据库·人工智能·postgresql·协议·duckdb
l1t2 天前
利用美团龙猫用libxml2编写XML转CSV文件C程序
xml·c语言·libxml2·解析器