網站首頁 編程語言 正文
一、了解棧的結構特點
棧是一種特殊的線性表,只允許從一端進出數據,稱為后進先出,先進后出。
壓棧:棧的插入操作叫做進棧/壓棧/入棧,入數據在棧頂
出棧:棧的刪除操作叫做出棧。出數據也在棧頂
二、具體實現
由于棧實質是一種線性表,因此可以用兩種方式來實現:順序表 ?或 ?鏈表
這里我使用的是類似順序表的方式來實現。
代碼如下:
typedef char Stacktype;
typedef struct Stack
{
int top;
Stacktype* data;
int capacity;
}Stack;
void Stack_init(Stack* pphead); //棧的初始化
void Stack_destory(Stack* pphead); //棧的清空
void Stack_push(Stack* pphead, Stacktype data); //插入數據,壓棧
void Stack_pop(Stack* pphead); //出棧(刪除數據)
bool Stack_Empty(Stack* pphead); //判斷棧是否為空
Stacktype Stack_Top(Stack* pphead); //調出棧頂元素
int Stack_Size(Stack* pphead); //查看數據個數
//棧的初始化
void Stack_init(Stack* pphead)
{
pphead->top = 0;
pphead->capacity = 0;
pphead->data = NULL;
}
//棧的清空
void Stack_destory(Stack* pphead)
{
pphead->top = 0;
pphead->capacity = 0;
free(pphead->data);
pphead->data = NULL;
}
//插入數據,壓棧
void Stack_push(Stack* pphead, Stacktype data)
{
assert(pphead);
if (pphead->top == pphead->capacity)
{
int Newcapacity = (pphead->capacity == 0) ? 4 : ((pphead->top) * 2);
Stacktype* temp = NULL;
temp = (Stacktype*)realloc(pphead->data, sizeof(Stacktype) * Newcapacity);
if (temp == NULL)
{
printf("Stack_push");
exit(-1);
}
pphead->data = temp;
pphead->top = Newcapacity;
}
(pphead->data)[pphead->capacity] = data;
pphead->capacity++;
}
//出棧(刪除數據)
void Stack_pop(Stack* pphead)
{
assert(pphead);
assert(Stack_Empty(pphead));
pphead->capacity--;
}
//判斷棧是否為空
bool Stack_Empty(Stack* pphead)
{
assert(pphead);
return pphead->capacity != 0;
}
//調出棧頂元素
Stacktype Stack_Top(Stack* pphead)
{
assert(pphead);
assert(Stack_Empty(pphead));
return pphead->data[pphead->capacity - 1];
}
//查看數據個數
int Stack_Size(Stack* pphead)
{
assert(pphead);
return pphead->top;
}
補充?棧的用處
我們好不容易實現了一個棧,接下來我們來做個題看看棧有什么用吧。
題目描述
給定一個只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判斷字符串是否有效。
有效字符串需滿足:
左括號必須用相同類型的右括號閉合。
左括號必須以正確的順序閉合。
基礎框架
C語言的基礎框架如下
bool isValid(char * s){
???????}
解題思路
左括號一定要和右括號對齊,非常滿足棧的特性
我們可以將所有的左括號存入一個棧中。
然后遇到右括號,就出棧,判斷是否匹配。
直到棧為空且字符串中的括號也遍歷完了,那么所有括號就正確的匹配了。
代碼詳解
// 1.因為C語言并沒有現成的棧,所以我們需要自己造輪子,先寫個棧再說
typedef char STDateType; // 更改數據類型為char
typedef struct Stack
{
STDateType* a;
int top;
int capacity;
}Stack;
void StackInti(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
void StackDestory(Stack* ps)
{
assert(ps);
free(ps->a);
ps->capacity = 0;
ps->top = 0;
}
void StackPush(Stack* ps, STDateType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newCapcity = ps->capacity == 0 ? 4 : ps->capacity * 2;
ps->a = (int*)realloc(ps->a, sizeof(int) * newCapcity);
if (ps->a == NULL)
{
printf("ralloc error");
exit(-1);
}
ps->capacity = newCapcity;
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(Stack* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}
STDateType StackTop(Stack* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
bool isValid(char * s){
Stack a;
StackInti(&a);
while(*s)
{
if (*s == '(' || *s == '[' || *s == '{') //入棧
{
StackPush(&a, *s);
}
else //出棧
{
if(StackEmpty(&a)) //右括號多一個的情況
{
return false;
}
char tmp = StackTop(&a);
StackPop(&a);
if ((*s == ')' && tmp != '(')
||(*s == ']' && tmp != '[')
||(*s == '}' && tmp != '{') )
{
return false;
}
}
s++;
}
return StackEmpty(&a); //防止出現多一個左括號的情況
}
原文鏈接:https://blog.csdn.net/MT_125/article/details/125470074
相關推薦
- 2022-07-22 C語言輸出所有水仙花數
- 2022-08-16 C#?winform?請求http的實現(get,post)_C#教程
- 2022-09-20 C#先判斷是否存在再創建文件夾或文件與遞歸計算文件夾大小_C#教程
- 2023-02-05 c++?梅森數源碼示例解析_C 語言
- 2022-03-31 C#值類型、引用類型、泛型、集合、調用函數的表達式樹實踐_C#教程
- 2022-05-08 總結Python函數參數的六種類型_python
- 2024-07-15 GIT同步修改后的遠程分支
- 2022-05-26 Python編程中內置的NotImplemented類型的用法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支