C语言用链栈完成括号匹配的检验

/*作业4:括号匹配的检验

假设表达式中允许包含两种括号:圆括号和方括号,其嵌入的顺序随意。设计一个算法采用顺序栈判断表达式中的括号是否正确配对,并编程实现。*/

/*思路:利用栈的先进后出,当识别'['&'('时压入栈,']'&')'时与栈顶元素比较,如果栈顶元素为'['&')'时弹出栈顶元素直至识别定义的结束符'#',结束进出栈的操作,检测栈内元素是否为空,为空则匹配正确(因为都消掉了),否则失败*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
	char data;
	struct node*next;
}node,*LinkList;
//初始化
LinkList creat(){
	LinkList p;
	p=(node*)malloc(sizeof(node));
	if(p!=NULL){
	p->next=NULL;
	}
	return p;
}
//判断是否为空
int StackEmpty(LinkList p){
    if(p->next==NULL)
		return 1;
	else 
		return 0;
}
//压入栈
void Push(LinkList p,char x){
	LinkList s;
	s=(node*)malloc(sizeof(node));
    s->data=x;
	s->next=p->next;
	p->next=s;
}
//出栈
void Pop(LinkList p,char *x){
	LinkList s;
	*x=p->data;
	s=p->next;
	p->next=s->next;
	free(s);
}
//取栈顶元素
char GetTop(LinkList p){
	if(p->next)
		return p->next->data;
	return 0;
}
int main(){
	LinkList top;
	char ch,t;
	int flag=1;
	top=creat();
	printf("please enter the number(end of''#'):\n");
	ch=getchar();
	while(ch!='#'&&flag){
		switch(ch){
		case'[':
		case'(':
			Push(top,ch);
			break;
		case']':
			if(!StackEmpty(top)&&GetTop(top)=='[')
				Pop(top,&t);
		    else
				flag=0;
		    break;
		case')':
			if(!StackEmpty(top)&&GetTop(top)==')')
				Pop(top,&t);
			else
				flag=0;
			break;
	}
	ch=getchar();
	}
if(StackEmpty(top)&&flag)
		printf("yes\n");
	else  printf("no\n");
return 0;
}
 
 

结果如下:

 

 

好的,以下是使用链栈实现括号匹配C语言代码,希望能帮到您: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> // 定义链栈节点结构体 typedef struct Node { char data; struct Node *next; } Node; // 定义链栈结构体 typedef struct Stack { Node *top; // 栈顶指针 } Stack; // 初始化链栈 Stack* initStack() { Stack *s = (Stack*)malloc(sizeof(Stack)); s->top = NULL; return s; } // 判断链栈是否为空 bool isEmpty(Stack *s) { return s->top == NULL; } // 进栈 void push(Stack *s, char data) { Node *node = (Node*)malloc(sizeof(Node)); node->data = data; node->next = s->top; s->top = node; } // 出栈 char pop(Stack *s) { if (isEmpty(s)) { printf("Stack is empty.\n"); return '\0'; } Node *node = s->top; char data = node->data; s->top = node->next; free(node); return data; } // 判断括号是否匹配 bool isMatch(char left, char right) { if (left == '(' && right == ')') { return true; } else if (left == '[' && right == ']') { return true; } else if (left == '{' && right == '}') { return true; } else { return false; } } // 判断括号序列是否匹配 bool isBracketMatch(char *str) { Stack *s = initStack(); int i = 0; while (str[i] != '\0') { if (str[i] == '(' || str[i] == '[' || str[i] == '{') { push(s, str[i]); } else if (str[i] == ')' || str[i] == ']' || str[i] == '}') { if (isEmpty(s) || !isMatch(pop(s), str[i])) { return false; } } i++; } return isEmpty(s); } int main() { // 测试括号匹配 char str1[] = "(((())))"; char str2[] = "([{}])"; char str3[] = "([]})"; printf("%s\n", isBracketMatch(str1) ? "Match" : "Not match"); printf("%s\n", isBracketMatch(str2) ? "Match" : "Not match"); printf("%s\n", isBracketMatch(str3) ? "Match" : "Not match"); return 0; } ``` 以上是一个简单的括号匹配示例,可以在其中添加更多的操作,如获取栈顶元素等,以满足您的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值