文章目录
⭐️写在前面的话⭐️
📒博客主页: 程序员好冰
🎉欢迎 【点赞👍 关注🔎 收藏⭐️ 留言📝】
📌本文由 程序员好冰 原创,优快云 首发!
📆入站时间: 🌴2022 年 07 月 13 日🌴
✉️ 是非不入松风耳,花落花开只读书。
💭推荐书籍:📚《Java编程思想》,📚《Java 核心技术卷》
💬参考在线编程网站:🌐牛客网🌐力扣
🍭 作者水平很有限,如果发现错误,一定要及时告知作者哦!感谢感谢!🍭
括号匹配问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define MAX_SIZE 100
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode;
typedef struct LinkStack
{
int count;
LNode *top;
}LinkStack;
Status InitStack(LinkStack *L)
{
L->top=(LNode*)malloc(sizeof(LNode));
if(!L->top){
return ERROR;
}
L->top=NULL;
L->count=0;
return OK;
}
Status StackIsEmpty(LinkStack L)
{
if(L.count==0){
return OK;
}
if(L.count!=0){
return ERROR;
}
return OK;
}
int StackLength(LinkStack L)
{
return L.count;
}
ElemType GetTop(LinkStack L)
{
return L.top->data;
}
Status Push(LinkStack *L,ElemType e)
{
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=L->top;
L->top=s;
L->count++;
return OK;
}
Status Pop(LinkStack *L)
{
if(StackIsEmpty(*L)){
return ERROR;
}
LNode *temp;
temp=L->top;
L->top=L->top->next;
free(temp);
L->count--;
return OK;
}
Status PrintStack(LinkStack L)
{
if(StackIsEmpty(L)){
return ERROR;
}
while(L.top){
printf("%d",L.top->data);
L.top=L.top->next;
}
return OK;
}
Status Matching(char *ch)
{
LinkStack L;
InitStack(&L);
int flag=1;//默认匹配成功
int i=0;
int len = strlen(ch);
while(i<len)
{
switch(ch[i])
{
case '[':
case '(':
case '{':
Push(&L,ch[i]);
break;
case ')':
if(!StackIsEmpty(L)&&GetTop(L)=='('){
Pop(&L);
}else
{
flag=0;
}
break;
case ']':
if(!StackIsEmpty(L)&&GetTop(L)=='['){
Pop(&L);
}else
{
flag=0;
}
break;
case '}':
if(!StackIsEmpty(L)&&GetTop(L)=='{'){
Pop(&L);
}else
{
flag=0;
}
break;
default:
break;
}
i++;
}
if(StackIsEmpty(L)&&flag){
printf("括号匹配.\n");
return OK;
}else
{
printf("括号不匹配.\n");
return ERROR;
}
return OK;
}
int main()
{
char ch[MAX_SIZE]={'0'};
printf("请输入括号表达式:");
scanf("%s",ch);
Matching(ch);
return 0;
}
运行效果
🚀先看后赞,养成习惯!🚀
🚀 先看后赞,养成习惯!🚀
🎈觉得文章写得不错的老铁们,点赞评论关注走一波!谢谢啦!🎈