标签:ps capacity top 血脉 小码 ST assert STDataType 农栈
栈
栈的概念及结构
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶
出栈:栈的删除操作叫做出栈。出数据也在栈顶
栈的实现
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
栈节点
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top; //栈顶
int capacity; //容量
}ST;
栈初始化函数StackInit
//栈初始化函数
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
入栈函数StackPush
//入栈函数
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,newcapacity*sizeof(STDataType));
if (!tmp)
{
printf("relloc fail\n");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
//扩容扩好以后把数据给过去
ps->a[ps->top] = x;
ps->top++;
}
提前把栈销毁函数写好
栈销毁函数StackDestroy
//栈销毁函数
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
出栈函数StackPop
//出栈函数
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top>0);
ps->top--;
}
判断栈是否为空 函数StackEmpty
//判断栈是否为空函数
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
取栈顶元素函数StackTop
//取栈顶部函数
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
栈大小函数StackSize
//栈大小函数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
遍历栈
while (!StackEmpty(&stack))
{
printf("%d ", StackTop(&stack));
StackPop(&stack);
}
代码
Stack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top; //栈顶
int capacity; //容量
}ST;
//栈初始化函数
extern void StackInit(ST* ps);
//栈销毁函数
extern void StackDestroy(ST* ps);
//入栈函数
extern void StackPush(ST* ps, STDataType x);
//出栈函数
extern void StackPop(ST* ps);
//取栈顶部函数
extern STDataType StackTop(ST* ps);
//栈大小函数
extern int StackSize(ST* ps);
//判断栈是否为空函数
extern bool StackEmpty(ST* ps);
Stack.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
//栈初始化函数
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
//入栈函数
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,newcapacity*sizeof(STDataType));
if (!tmp)
{
printf("relloc fail\n");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
//扩容扩好以后把数据给过去
ps->a[ps->top] = x;
ps->top++;
}
//栈销毁函数
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
//出栈函数
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top>0);
ps->top--;
}
//取栈顶部函数
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
//栈大小函数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
//判断栈是否为空函数
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void Test1()
{
ST stack = { 0 };
StackInit(&stack);
StackPush(&stack, 1);
StackPush(&stack, 2);
StackPush(&stack, 3);
StackPush(&stack, 4);
//遍历栈
while (!StackEmpty(&stack))
{
printf("%d ", StackTop(&stack));
StackPop(&stack);
}
printf("\n");
StackDestroy(&stack);
}
int main()
{
Test1();
return 0;
}
练习
例1有效的括号
typedef char STDataType;
typedef struct Stack
{
STDataType* a;
int top; //栈顶
int capacity; //容量
}ST;
//栈初始化函数
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
//栈销毁函数
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
//入栈函数
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,newcapacity*sizeof(STDataType));
if (!tmp)
{
printf("relloc fail\n");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
//扩容扩好以后把数据给过去
ps->a[ps->top] = x;
ps->top++;
}
//判断栈是否为空函数
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//取栈顶部函数
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
//出栈函数
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top>0);
ps->top--;
}
bool isValid(char * s){
ST st = {0};
StackInit(&st);
while(*s)
{
//如果是左括号就入栈
if(*s == '('
|| *s == '{'
|| *s == '[')
{
//入栈
StackPush(&st,*s);
s++;
}
else
{
if(StackEmpty(&st))
{
StackDestroy(&st);
return false;
}
//出栈
STDataType tmp = StackTop(&st);
StackPop(&st);
if(*s == '}' && tmp != '{'
|| *s == ']' && tmp != '['
|| *s == ')' && tmp != '(')
{
StackDestroy(&st);
return false;
}
else
{
s++;
}
}
}
//如果栈不是空说明还有左括号
bool ret = StackEmpty(&st);
StackDestroy(&st);
return ret;
}
标签:ps,capacity,top,血脉,小码,ST,assert,STDataType,农栈 来源: https://blog.csdn.net/diandengren/article/details/121055584
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。