利用栈的基本操作,检查括号是否匹配
来源:互联网 发布:还珠格格3知画怀孕 编辑:程序博客网 时间:2024/05/16 12:33
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> typedef char ElemType; typedef struct node { ElemType data; struct node *next; }LStackNode,*LinkStack; void InitStack(LinkStack *top);//将链栈初始化 int StackEmpty(LinkStack top);//判断链栈是否为空 int GetTop(LinkStack top,ElemType *e);//取栈顶元素 int PushStack(LinkStack top,ElemType e);//进栈操作 int PopStack(LinkStack top,ElemType *e);//出栈操作 int StackLength(LinkStack top);//求表长操作 void DestroyStack(LinkStack top);//销毁链表 int Match(ElemType e,ElemType ch);//判断括号 #include "括号配对.h" void InitStack(LinkStack *top)//将链栈初始化 { if((*top = (LinkStack)malloc(sizeof(LStackNode)))== NULL) { exit(-1); } (*top)->next = NULL; } int StackEmpty(LinkStack top)//判断链栈是否为空 { if(top->next == NULL) { return 1; } else { return 0; } } int GetTop(LinkStack top,ElemType *e)//取栈顶元素 { LStackNode *p; p = top->next ; if(!p) { printf("栈已空!"); return 0; } *e = p->data ; return 1; } int PushStack(LinkStack top,ElemType e)//进栈操作 { LStackNode *p; if((p = (LinkStack)malloc(sizeof(LStackNode))) == NULL) { printf("内存分配失败!"); return 0; } p->data = e; p->next = top->next; top->next = p; return 1; } int PopStack(LinkStack top,ElemType *e)//出栈操作 { LStackNode *p; p = top->next ; if(!p) { printf("栈已空!"); return 0; } top->next = p->next ; *e = p->data ; free(p); return 1; } int StackLength(LinkStack top)//求表长操作 { LStackNode *p; int count = 0; p = top; while(p->next != NULL) { p = p->next ; count++; } return count; } void DestroyStack(LinkStack top)//销毁链表 { LStackNode *p,*q; p = top; while(!p) { q = p; p = p->next ; free(q); } } int Match(ElemType e,ElemType ch) { if(e == '('&&ch == ')') { return 1; } else if(e == '['&&ch == ']') { return 1; } else if(e == '{'&&ch == '}') { return 1; } else { return 0; } } #include "括号配对.h" int main(void) { LinkStack S; char *p; ElemType e; ElemType ch[60]; InitStack(&S); printf("请输入带括号的表达式('()','[]','{}').\n"); gets(ch); p = ch; while(*p) { switch(*p) { case '(': case '[': case '{': PushStack(S,*p++); break; case ')': case ']': case '}': if(StackEmpty(S)) { printf("缺少左括号.\n"); return 0; } else { GetTop(S,&e); if(Match(e,*p)) { PopStack(S,&e); } else { printf("左右括号不匹配!\n"); return 0; } } default: p++; } } if(StackEmpty(S)) { printf("括号匹配!\n"); } else { printf("缺少右括号!\n"); } return 0; }
0 0
- 利用栈的基本操作,检查括号是否匹配
- 栈的基本操作及如何判断一个表达式中的左右括号是否匹配
- 利用栈判断括号是否匹配
- 利用栈判断括号是否匹配
- [数据结构-栈]栈的基本操作&括号匹配
- 数据结构与算法学习之路:检查表达式的括号是否匹配——栈
- 左右括号是否匹配用的栈
- 栈的应用----判断括号是否匹配
- C语言中利用栈检测括号是否匹配
- 利用栈判断输入的表达式中的括号是否匹配(假设只含有左、右括号)
- 利用栈判断括号的匹配性
- 利用栈结构进行括号的匹配
- 括号匹配,栈的基本应用;
- 不用栈检查小括号、中括号、大括号的匹配
- 不用栈检查小括号、中括号、大括号的匹配
- 栈的经典应用之一:括号匹配检查
- 检查一个算术表达式中的括号是否匹配
- 利用栈进行括号匹配
- 利用栈的基本操作,将十进制数转换为八进制数
- UITableView 避免奔溃的数组越界处理方法
- Hibernate的中使用<generator>设置主键的生成方式
- 柔性数组
- thinkphp3.2的运用
- 利用栈的基本操作,检查括号是否匹配
- shell脚本的特殊字符
- cell的高度随点击变化
- yum的原理及配置
- [Practical.Vim(2012.9)].Drew.Neil.Tip09学习摘要
- 解压cpio.gz文件
- Centos克隆没有eth0以及修改系统语言为中文
- 利用栈的基本操作编写一个行编辑程序,当前一个字符有误时,输入#消除,当前面一行有误时,输入@消除前面行的字符序列
- Auto DOP and Parallel Statement Queuing