數據結構07——表達式括號匹配

Description

假設一個算術表達式中可以包含三種括號:圓括號“( ”和“ )”、方括號“ [ ”和“ ] ”和花括號“{”和“}”,且這三種括號可按任意的次序嵌套使用(如:…[…{…}…[…]…]…[…]…(…)…)。編寫判別給定表達式中所含括號是否正確配對出現的程序(已知表達式已存入數據元素爲字符的順序表中)。

Input

輸入算術表達式,換行結束。

Output

若給定表達式中所含括號正確配對,則輸出yes,否則輸出no。

  • Sample Input 
    [5+(6-3)]-(2+3)]
  • Sample Output
    no

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct  stack{
	struct stack *top;
	struct stack *bottom;
	struct stack *next;
	char sign;
}stack;

void init(stack *list){
	list->bottom=(stack*)malloc(sizeof(stack));
	list->bottom->next==NULL;
	list->top=list->bottom;
}

void push(stack *list,char s){
	stack *p;
	p=(stack*)malloc(sizeof(stack));
	p->sign=s;
	p->next=list->top;
	list->top=p;
}

void pop(stack *list){
	stack *p;
	p=list->top;
	list->top=list->top->next;
	free(p);
}

int main(){
	char a[100];
	int i;
	stack *list;
	list=(stack*)malloc(sizeof(stack));
	init(list);
	scanf("%s",&a);
	for(i=0;i<strlen(a);i++){
		if(a[i]=='{'||a[i]=='('||a[i]=='['){
			push(list,a[i]);
		}
		else if(a[i]=='}'){
			if(list->top->sign=='{'){
				pop(list);
			}
			else{
				printf("no\n");
				return 0;
			}
		}
		else if(a[i]==')'){
			if(list->top->sign=='('){
				pop(list);
			}
			else{
				printf("no\n");
				return 0;
			}
		}
		else if(a[i]==']'){
			if(list->top->sign=='['){
				pop(list);
			}
			else{
				printf("no\n");
				return 0;
			}
		}
	}
	if(list->top == list->bottom){
		printf("yes\n");
		return 0;
	}
}


複習:

#include<stdio.h>
#include<stdlib.h>

typedef struct stack{
	struct stack *top;
	struct stack *bottom;
	struct stack *next;
	char data;
}stack;

void push(stack *list, char x){
	stack *p = (stack*)malloc(sizeof(stack));
	p->data = x;
	p->next = NULL;
	list->top->next = p;
	list->top = p;
}

char pop(stack *list){
	stack *p, *q;
	p = list->bottom->next;
	if(p != list->top){
		while(p->next != list->top){
		    p = p->next;
	    }
	}
	
	q = list->top;
	list->top = p;
	return(q->data);
	free(q);
}

bool isempty(stack *list){
	if(list->top == list->bottom){
		return true;
	}
	else{
		return false;
	}
}

bool lkuohao(char x){
	if(x == '{' || x == '[' || x == '('){
		return true;
	}
	else{
		return false;
	}
}

bool rkuohao(char x){
	if(x == '}' || x == ']' || x == ')'){
		return true;
	}
	else{
		return false;
	}
}

bool pipei(char x, char y){
	if((y=='{'&&x=='}') || (y=='['&&x==']') || (y=='('&&x==')'))
	    return true;
	else
	    return false;
}

void init(stack *list){
	stack *p = (stack*)malloc(sizeof(stack));
	p->data = '#';
	p->next = NULL;
	list->bottom = p;
	list->top = list->bottom;
}

bool creat(stack *list){
    char x, y;
	while(scanf("%c", &x)){
		if(x == '\n')
		    break;
		else if(!(lkuohao(x) || rkuohao(x)))
		    continue;
		else{
			if(lkuohao(x)){
				push(list, x);
			}
			else{
				if(isempty(list))
				     return false;
				else{
					y = pop(list);
					if(pipei(x, y))
					    continue;
					else
					    return false;
				}
			}
		}
	}
	return true;	
} 

int main(){
	stack *list = (stack*)malloc(sizeof(stack));
	init(list);
	if(creat(list)){
		printf("yes\n");
	}
	else{
		printf("no\n");
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章