SDUT-2134_数据结构实验之栈与队列四:括号匹配
数据结构实验之栈与队列四:括号匹配
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。
Input
输入数据有多组,处理到文件结束。
Output
如果匹配就输出“yes”,不匹配输出“no”
Sample Input
sin(20+10)
{[}]
Sample Output
yes
no
Hint
Source
ma6174
括号匹配,栈的经典题目,将左括号入站,然后遇到右括号就询问栈顶是不是与之相匹配的左括号,如果是,出栈,不是,匹配失败。
注意这个题目是多组输入,而且包含空格,所以需要用 gets读入。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char data;
struct node *next;
}Node;
typedef struct stack
{
Node *base,*top;
}Stack;
Node *newnode()//建立节点
{
Node *t;
t = (Node *)malloc(sizeof(Node));
t->next = NULL;
return t;
};
Stack *Newstack()//建立新栈
{
Stack *t;
t = (Stack *)malloc(sizeof(Stack));
t->top = newnode();
t->base = t->top;
return t;
}
void push(Stack *t,char x)//入站
{
Node *p = newnode();
p->data = x;
p->next = t->top->next;
t->top->next = p;
t->base = p;
}
char top(Stack *t)//询问栈顶元素
{
return t->top->next->data;
}
void pop(Stack *t)//出栈
{
Node *p;
p = t->top->next;
t->top->next = t->top->next->next;
free(p);
}
int empty(Stack *t)//询问栈是否为空
{
if(t->top->next==NULL)
return 1;
return 0;
}
void del(Stack *t)//清空栈
{
while(!empty(t))
pop(t);
}
int main()
{
char s[55];
Stack *t;
int n,i;
t = Newstack();
while(gets(s)!=NULL)
{
n = strlen(s);
for(i=0;i<n;i++)
{
if(s[i]=='('||s[i]=='{'||s[i]=='[')
push(t,s[i]);
else if(s[i]==')'||s[i]=='}'||s[i]==']')
{
if(empty(t))
break;
else
{
if(s[i]==')'&&top(t)!='(')
break;
else if(s[i]=='}'&&top(t)!='{')
break;
else if(s[i]==']'&&top(t)!='[')
break;
else
pop(t);
}
}
}
if(i!=n)
printf("no\n");
else
{
if(!empty(t))//注意最后要判定是否所有的左括号都已经匹配
printf("no\n");
else
printf("yes\n");
}
del(t);
}
return 0;
}
线性表
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Stack
{
char *top,*base;
}Stack;
Stack newStack()
{
Stack t;
t.top = (char *)malloc(100050*sizeof(char));
t.base = t.top;
return t;
}
void push(Stack *t,char x)
{
*(t->top++) = x;
}
char top(Stack t)
{
return *(t.top-1);
}
void pop(Stack *t)
{
t->top--;
}
int Empty(Stack t)
{
if(t.top==t.base)
return 1;
return 0;
}
void del(Stack *t)
{
while(!Empty(*t))
pop(t);
}
int main()
{
char s[55];
Stack t;
int n,i;
t = newStack();
while(gets(s)!=NULL)
{
n = strlen(s);
del(&t);
for(i=0;i<n;i++)
{
if(s[i]=='('||s[i]=='['||s[i]=='{')
push(&t,s[i]);
else if(s[i]==')')
{
if(Empty(t)||top(t)!='(')
break;
else
pop(&t);
}
else if(s[i]==']')
{
if(Empty(t)||top(t)!='[')
break;
else
pop(&t);
}
else if(s[i]=='}')
{
if(Empty(t)||top(t)!='{')
break;
else
pop(&t);
}
}
if(i!=n||!Empty(t))
printf("no\n");
else
printf("yes\n");
}
return 0;
}
分类:
数据结构-栈和队列
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现