标签:
感觉写不出的什么东西。最近都在疯狂补习指针 拿个自己写的链栈的符号匹配的应用
//definition.h文件
#ifndef DEFINITION_H_
#define DEFINITION_H_
#include <stdio.h>
#include <stdlib.h>
typedef char DataType;
typedef struct node{
DataType data;
struct node *next;
}stack,*LinkStack;
#endif
//function函数
#include "definition.h" LinkStack Init_LinkStack(stack *top);//置空栈 int Empty_Stack(stack *top);//判断栈空 LinkStack Push_LinkStack(stack *top,DataType x);//压栈 DataType Pop_LinkStack(stack *top);//出栈 DataType Top_LinkStack(stack *top);//栈首元素 int BracketsCheck(stack *top, DataType a[]);//括号配对
LinkStack Init_LinkStack(stack *top){ top = NULL; return top; } int Empty_Stack(stack *top){ if (top == NULL) return 1; else return 0; } LinkStack Push_LinkStack(stack *top,DataType x){ stack *s; s = (stack *)malloc(sizeof(stack)); s->data = x; s->next = top; top = s; return top; } DataType Pop_LinkStack(stack *top){ stack *s; if (Empty_Stack(top)) return NULL; else{ DataType x; x = top->data; s = top; top = top->next; free(s); return x; }
} DataType Top_LinkStack(stack *top){ if (Empty_Stack(top)) return NULL; else return top->data; } int BracketsCheck(stack *top,DataType a[]){ int i = 0;//从0开始依次扫描整个字符串 while (a[i]){ DataType ch = a[i++];//将扫描到的字符变量给ch switch (ch){ //switch 的作用对字符串里面的括号分情况处理 case ‘{‘: case ‘[‘: case ‘(‘: Push_LinkStack(top, ch); //任意一种左括号入栈 break; case ‘}‘: if (!Empty_Stack(top) && (Top_LinkStack(top) == ‘}‘))//将栈顶的括号和扫描到的右括号做比较 Pop_LinkStack(top);//将栈顶左括号出栈 else return 0; break; case ‘]‘: if (!Empty_Stack(top) && (Top_LinkStack(top) == ‘}‘)) Pop_LinkStack(top); else return 0; break; case ‘)‘: if (!Empty_Stack(top) && (Top_LinkStack(top) == ‘}‘)) Pop_LinkStack(top); else return 0; break; }//end swithch
}//end while if (Empty_Stack(top)) return 3; else return 0; }//end BracketsCheck
//主调函数main
#define _CRT_SECURE_NO_WARNINGS//行首加vs2013版本以上解决宏禁止不安全函数
#include "definition.h"
#include "function.h"
int main(){
stack *s;
int x;
s = (stack *)malloc(sizeof(stack));
s = Init_LinkStack(s);
printf("初始化成功\n");
DataType a[80];
printf("请输入一个字符串:"); scanf("%s", a);
x=BracketsCheck(s,a);
if (x==1)
printf("匹配失败");
else if (x == 0)
printf("匹配成功");
else
printf("没有输入括号");
system("pause");
}
标签:
原文地址:http://www.cnblogs.com/1996h/p/5384916.html