文章目录
🍀前言
🏄♂️数组栈
🔍1.栈的结构框架
typedef struct Stack
{
STDataType* a;
int top; //top指向栈顶的位置
int capacity;
}ST;
🔍2.栈的初始化
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0; //如果我们初始化为0,top就指向栈顶元素的下一个位置,初始化为-1,top就是指向栈顶元素.
pst->capacity = 0;
}
📢初始化数组指针为空
📢top不同的初始化
1.top初始化为0
2.top初始化为-1
⭐友友们注意了,对应不同的top值,栈的判空条件也是不一样的
🔍3.栈的释放
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->capacity = pst->top = 0;
}
🔍4.栈的插入数据
void STPush(ST* pst, STDataType x)
{
assert(pst);
if (pst->top == pst->capacity)
{
int newcapacity = pst->capacity==0 ? 4 : pst->capacity * 2 ;
STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
🔍5.栈的删除数据
void STPop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
pst->top--;
}
🔍6.取栈顶元素
STDataType STTop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
return pst->a[pst->top - 1];
}
🔍7.栈的判空
bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
}
🔍8.返回栈空间元素的个数
int STSize(ST* pst)
{
assert(pst);
return pst->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; //top指向栈顶的位置
int capacity;
}ST;
void STInit(ST* pst);
void STDestroy(ST* pst);
void STPush(ST* pst,STDataType x);
STDataType STTop(ST* pst);
void STPop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);
🏂Stack.c代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0; //如果我们初始化为0,top就指向栈顶元素的下一个位置,初始化为-1,top就是指向栈顶元素.
pst->capacity = 0;
}
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->capacity = pst->top = 0;
}
void STPush(ST* pst, STDataType x)
{
assert(pst);
if (pst->top == pst->capacity)
{
int newcapacity= pst->capacity==0 ? 4 : pst->capacity * 2 ;
STDataType* tmp = (STDataType*)realloc(pst->a,newcapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
STDataType STTop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
return pst->a[pst->top - 1];
}
bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
}
void STPop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
pst->top--;
}
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
🏊♂️Test.c代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void TestStack1()
{
ST st;
STInit(&st);
STPush(&st, 1);
STPush(&st, 2);
STPush(&st, 3);
STPush(&st, 4);
while (!STEmpty(&st)) //因为栈是后进先出,所以我们不能随便访问
{
printf("%d ", STTop(&st));
STPop(&st);
}
STDestroy(&st);
}
int main()
{
TestStack1();
return 0;
}
⭐⭐⭐友友们,这里注意一下
🧋代码效果展示
🖋题目描述
💡逻辑分析
❌错误案例1
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top; //top指向栈顶的位置
int capacity;
}ST;
void STInit(ST* pst);
void STDestroy(ST* pst);
void STPush(ST* pst,STDataType x);
STDataType STTop(ST* pst);
void STPop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0; //如果我们初始化为0,top就指向栈顶元素的下一个位置,初始化为-1,top就是指向栈顶元素.
pst->capacity = 0;
}
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->capacity = pst->top = 0;
}
void STPush(ST* pst, STDataType x)
{
assert(pst);
if (pst->top == pst->capacity)
{
int newcapacity = pst->capacity==0 ? 4 : pst->capacity * 2 ;
STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
STDataType STTop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
return pst->a[pst->top - 1];
}
bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
}
void STPop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
pst->top--;
}
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
bool isValid(char * s){
ST q;
STInit(&q);
while(*s)
{
if(*s=='('||*s=='['||*s=='{')
{
STPush(&q,*s);
}
else
{
if(STTop(&q)=='('&&*s!=')'||STTop(&q)=='['&&*s!=']'||STTop(&q)=='{'&&*s!='}')
{
return false;
}
else
{
STPop(&q);
}
}
s++;
}
return true;
}
❌错误案例2
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top; //top指向栈顶的位置
int capacity;
}ST;
void STInit(ST* pst);
void STDestroy(ST* pst);
void STPush(ST* pst,STDataType x);
STDataType STTop(ST* pst);
void STPop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0; //如果我们初始化为0,top就指向栈顶元素的下一个位置,初始化为-1,top就是指向栈顶元素.
pst->capacity = 0;
}
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->capacity = pst->top = 0;
}
void STPush(ST* pst, STDataType x)
{
assert(pst);
if (pst->top == pst->capacity)
{
int newcapacity = pst->capacity==0 ? 4 : pst->capacity * 2 ;
STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
STDataType STTop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
return pst->a[pst->top - 1];
}
bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
}
void STPop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
pst->top--;
}
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
bool isValid(char * s){
ST q;
STInit(&q);
while(*s)
{
if(*s=='('||*s=='['||*s=='{')
{
STPush(&q,*s);
}
else
{
if(STTop(&q)=='('&&*s!=')'||STTop(&q)=='['&&*s!=']'||STTop(&q)=='{'&&*s!='}')
{
return false;
}
else
{
STPop(&q);
}
}
s++;
}
if(!STEmpty(&q))
return false;
return true;
}
✔代码纠正
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top; //top指向栈顶的位置
int capacity;
}ST;
void STInit(ST* pst);
void STDestroy(ST* pst);
void STPush(ST* pst,STDataType x);
STDataType STTop(ST* pst);
void STPop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0; //如果我们初始化为0,top就指向栈顶元素的下一个位置,初始化为-1,top就是指向栈顶元素.
pst->capacity = 0;
}
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->capacity = pst->top = 0;
}
void STPush(ST* pst, STDataType x)
{
assert(pst);
if (pst->top == pst->capacity)
{
int newcapacity = pst->capacity==0 ? 4 : pst->capacity * 2 ;
STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
STDataType STTop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
return pst->a[pst->top - 1];
}
bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
}
void STPop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
pst->top--;
}
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
bool isValid(char * s){
ST q;
STInit(&q);
while(*s)
{
if(*s=='('||*s=='['||*s=='{')
{
STPush(&q,*s);
}
else
{
if(STEmpty(&q))
{
return false;
}
if(STTop(&q)=='('&&*s!=')'||STTop(&q)=='['&&*s!=']'||STTop(&q)=='{'&&*s!='}')
{
return false;
}
else
{
STPop(&q);
}
}
s++;
}
if(!STEmpty(&q))
return false;
return true;
}