棧的應用---平衡符號
判斷一行字串輸入"各種括號"是否是合法的-----------------------棧用陣列實現
如:[()]是合法的(balance)
[(])是不合法的(imbalance)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Mystack *Stack;
struct Mystack {
int Capacity; /* 棧的容量 */
int Top_of_stack; /* 棧頂下標 */
char *Array; /* 存放棧中元素的陣列 */
};
/* 棧的建立 */
Stack CreateStack(int Max)
{
Stack S;
S = malloc(sizeof(struct Mystack));
if (S == NULL)
printf("Create stack error!\n");
S->Array = malloc(sizeof(char) * Max);
if (S->Array == NULL)
printf("Create stack error!\n");
S->Capacity = Max;
S->Top_of_stack = 0;
return S;
}
/* 釋放棧 */
void DisposeStack(Stack S)
{
if (S != NULL)
{
free(S->Array);
free(S);
}
}
/* 判斷一個棧是否是空棧 */
int IsEmpty(Stack S)
{
return !S->Top_of_stack;
}
/* 判斷一個棧是否滿棧 */
int IsFull(Stack S)
{
if (S->Top_of_stack == S->Capacity - 1)
return 1;
else
return 0;
}
/* 資料入棧 */
int Push(int x, Stack S)
{
if (IsFull(S))
printf("The Stack is full!\n");
else
S->Array[S->Top_of_stack++] = x;
}
/* 資料出棧 */
int Pop(Stack S)
{
if (IsEmpty(S))
printf("The Stack is empty!\n");
else
S->Top_of_stack--;
}
/* 將棧頂返回 */
char Top(Stack S)
{
if (!IsEmpty(S))
return S->Array[S->Top_of_stack-1];
printf("The Stack is empty!\n");
return 0;
}
int main()
{
int i, len;
char str[100];
printf("Please input the symbol that you want to balance: \n");
scanf("%s", str);
len = strlen(str);
/* 根據序列的長度來建立棧 */
struct Mystack *my_stack = CreateStack(len+1);
for (i = 0; i < len; i++)
{
if (str[i] == '{' || str[i] == '[' || str[i] == '(' )
Push(str[i], my_stack);
if (str[i] == '}')
{
if (Top(my_stack) == '{')
Pop(my_stack);
else
break;
}
else if (str[i] == ']')
{
if (Top(my_stack) == '[')
Pop(my_stack);
else
break;
}
else if (str[i] == ')')
{
if (Top(my_stack) == '(')
Pop(my_stack);
else
break;
}
}
/* 如果最後佔空則序列是合法的 */
if(IsEmpty(my_stack))
printf("The symbol that you input is balance!\n");
else
printf("The symbol that you input is imbalance!\n");
DisposeStack(my_stack);
return 0;
}
測試:
shang@shang:~/C$ ./a.out
Please input the symbol that you want to balance:
{[]}
The symbol that you input is balance!
shang@shang:~/C$ ./a.out
Please input the symbol that you want to balance:
[(])
The symbol that you input is imbalance!
shang@shang:~/C$ ./a.out
Please input the symbol that you want to balance:
([{}])
The symbol that you input is balance!
shang@shang:~/C$
相關文章
- 棧的應用之平衡符號 【資料結構與演算法分析 c 語言描述】符號資料結構演算法
- 回車符號 ‘ ’ 的實際應用符號
- 讓您的應用支援新式的 Emoji 符號吧!符號
- 棧的應用
- 使用Python實現一個棧, 判斷括號是否平衡Python
- AppID 的英文全稱是 Application Identifier AppID(應用程式識別符號)GUID(全域性唯一識別符號)APPIDE符號GUI
- iOS應用崩潰日誌.crash報告符號化/.dYSM符號表手動解析(Objective-C)iOS應用崩潰符號Object
- 棧的原理與應用
- 棧的實際應用
- Python的符號、對齊和用0填充Python符號
- 棧的應用和實現
- 關於pwn題的棧平衡中ret的作用
- [C]有符號數和無符號數符號
- 棧的應用---字尾表示式
- 表示式計算(棧的應用)
- package.json 中的版本號,符號 ~ 與 ^ 的區別及其他符號的含義PackageJSON符號
- CSS @ 符號CSS符號
- 符號(註釋符+轉義符+接續符)符號
- 連結使用的符號符號
- Html/CSS的符號學HTMLCSS符號
- HTML 中的特殊符號HTML符號
- python的識別符號Python符號
- 簡單計算器(棧的應用)
- 棧的應用 -洛谷P1449
- dotNet符號檔案(pdb),符號包(snupkg)和SourceLink符號
- BOOST應用 無法解析的外部符號 "void __cdecl boost::throw_exception(class std::exception const &)"符號Exception
- 符號連結符號
- 流程圖符號流程圖符號
- 找不到符號符號
- 識別符號符號
- python 中 *符號Python符號
- 從應用場景看棧
- 數學符號的來歷符號
- (2)使用的數學符號符號
- latex 中的存在,任意, ξ 符號符號
- Yaml中特殊符號"| > |+ |-"的作用YAML符號
- Room & Kotlin 符號的處理OOMKotlin符號
- Verilog有符號數、無符號數之間的賦值與運算符號賦值
- 平衡應用內廣告與應用內購買以提升玩家留存