使用栈来实现符号平衡----c语言实现
2016-07-31 18:55
911 查看
使用栈来实现符号平衡—-c语言实现
用栈比较容易实现的应用:符号平衡源码如下:
#include <stdio.h> #include <malloc.h> /* 编程思路:为实现符号平衡,先创建一个空栈,先读入一对符号 的左边,直到读入这对符号的右边,把符号弹出,如果这对符号 对称则继续,直到最后符号全部读完,并且栈为空。 */ struct node { char symbol; struct node* next; }; typedef struct node Node; typedef struct node* stack; typedef struct node* position; stack makeStack(void); int isEmpty(stack S); void push(stack S,char X); void pop(stack S); char top(stack S); int match(char p,char q); void checkSymbols(stack S); stack makeStack(void) { stack S=(struct node*)malloc(sizeof(struct node)); if(S==NULL) printf("memory error"); else { S->next=NULL; return S; } } int isEmpty(stack S) { return S->next==NULL; } void push(stack S,char X) { if(S==NULL) printf("Please make a stack first"); position P = (struct node*)malloc(sizeof(struct node)); if(P==NULL) printf("memory error"); else { P->symbol=X; P->next=S->next; S->next=P; } } void pop(stack S) { position P; if(isEmpty(S)) printf("Empty Stack"); else { P=S->next; S->next=S->next->next; free(P); } } char top(stack S) { return S->next->symbol; } int match(char p,char q) { if(abs(p-q)==1) return 1; else if(abs(p-q)==2) return 1; else return 0; } void checkSymbols(stack S) { char ch; while((ch=getchar())!='#') { if(ch=='{'||ch=='['||ch=='(') push(S,ch); else if(ch=='}'||ch==']'||ch==')') { if(match(ch,top(S))) pop(S); else printf("error"); } } if(isEmpty(S)) printf("right\n"); } int main(int argc, char *argv[]) { stack S = makeStack(); checkSymbols(S); return 0; }
相关文章推荐
- 堆栈的应用--用C语言实现平衡符号
- Berkeley db 中queue算法的使用(c语言实现)
- 堆栈的应用(1) 平衡符号 C++实现
- 实现C语言的拷贝函数且将复制后的字符串逆序,不能使用库函数,不能定义其他的变量。
- [C语言]strcat strstr strcpy strcmp strlen 的实现与使用
- 使用C语言将ls命令重定向实现
- 如何使用c语言实现双向链表的插入删除操作
- Linux下使用socket传输文件的C语言简单实现
- 俺使用的C语言面向对象范式(第四节:多态的实现 与 总结)
- c语言实现正序链表的创建(不使用头结点)
- c语言中定义struct型变量时使用“.”符号的意义
- C语言实现的PadLeft,在str的左边使用bychar补齐为指定的长度
- what's in string? c语言string类函数实现汇总 都是学习使用指针的好例子啊(算是读书摘抄和笔记吧)
- 如何在C语言使用位运算实现循环移位
- 对于一个频繁使用的短小函数,在C语言中应用什么实现,在C++中应用什么实现?
- 每日一题 -- 不使用变量实现c语言的strlen函数
- linux下使用Freetds 连接MS SqlServer 2000 (C语言实现)
- 汇编语言使用加减法实现有符号数除法
- C语言使用数组实现简单队列
- Linux下使用socket传输文件的C语言简单实现