字符鏈棧實現簡單的括號匹配檢驗

只能用來判斷   [   ]   (  )    四種括號

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
#define OK 1
#define ERROR 0
 
typedef int Status;
 
typedef struct snode{
	char sign; 
	struct snode* next;
}snode;
 
typedef struct linkstack{//字符棧 
	snode* top;
	snode* bottom;
	int length;
}linkstack;
 
Status creat(linkstack* S){//創建一個空棧 
	S->top=S->bottom=(snode*)malloc(sizeof(snode));
	if(!S->top){
		printf("申請空間失敗!\n");
		return ERROR;
	}
	S->top->next=NULL;
	S->bottom->next=NULL;
	S->length=0;
	return OK; 
}
 
bool empty(linkstack S){//判斷棧是否爲空  
	return !S.length;
}
 
int size(linkstack S){//返回棧的大小  
	return S.length;
}
 
Status push(linkstack* S,char e){//元素入棧  
	snode* newx=(snode*)malloc(sizeof(snode));
	if(!newx){
		printf("申請空間失敗!\n");
		return ERROR;
	}
	newx->sign=e;
	newx->next=S->top->next;
	S->top->next=newx;
	if(!S->length)
		S->bottom=S->bottom->next;
	S->length++;
	return OK;
}
 
Status pop(linkstack* S){//彈出棧頂元素  
	if(!S->length){
		printf("當前棧已經爲空!\n");
		return ERROR;
	}
	snode* del=S->top->next;
	S->top->next=del->next;
	free(del);
	S->length--;
	return OK;
}
 
Status gettop(linkstack S,char &e){//獲得棧頂元素 存到e中  
	if(!S.length){
		printf("當前棧爲空!\n");
		return ERROR;
	}
	e=S.top->next->sign;
	return OK;
}
 
Status show(linkstack S){//輸出當前棧的內容  
	if(!S.length){
		printf("空!\n");
		return ERROR;
	}
	snode* p=S.top->next;
	while(p){
		printf("%c ",p->sign);
		p=p->next;
	}
	printf("\n");
	return OK;
}
 
Status clear(linkstack *S){//清空棧  
	while(S->top->next)
		pop(S);
	return OK; 
}
 
Status destroy(linkstack* S){//銷燬當前棧  
	snode* del;
	while(S->top){
		del=S->top;
		S->top=S->top->next;
		free(del);
	}
	S->top=S->bottom=NULL;
	return OK;
}
 
int main()
{
	char str[99];
	printf("\t\t\t----------------檢測括號是否匹配程序----------------\n"); 
	printf("請輸入一個帶有括號的字符串:");
	while(scanf("%s",str)!=EOF){
		linkstack S;
		creat(&S);
		int flag=0;//表示狀態  
		int cnt=0;
		int i;
		for(i=0;i<strlen(str);i++){
			if(str[i]=='('||str[i]=='['||str[i]==')'||str[i]==']')
				cnt++;//判斷是否輸入括號 
			if(str[i]=='('||str[i]=='[')
				push(&S,str[i]);
			if(str[i]==')'||str[i]==']'){
				if(empty(S)&&str[i]==')'){
					flag=1;
					break;
				}
				else if(empty(S)&&str[i]==']'){
					flag=2;
					break;
				}
				
				char c;
				gettop(S,c);
				if(c=='('&&str[i]==')'||c=='['&&str[i]==']')
					pop(&S);
				else if(c=='('&&str[i]==']'){
					flag=3;
					break;
				}
				else if(c=='['&&str[i]==')'){
					flag=4;
					break;
				}	
			}	
		}
		
		if(!empty(S)&&i==strlen(str)){
			char c;
			gettop(S,c);
			if(c=='(')
				flag=5;
			else flag=6;
		}
				
		if(!cnt){
			printf("\n沒有括號!\n");
			printf("\n\t\t\t----------------檢測括號是否匹配程序----------------\n"); 
			printf("請輸入一個帶有括號的字符串:");
			continue; 
		}
		
		printf("\n");
		switch(flag){
			case 0:
				printf("括號匹配\n");
				break;
			case 1:
				printf(" ( 缺少,不匹配\n");
				break;
			case 2:
				printf(" [ 缺少,不匹配\n");
				break;
			case 3:
				printf("出現 (] 情況,不匹配\n");
				break;
			case 4:
				printf("出現 [) 情況,不匹配\n");
				break;
			case 5:
				printf(" ) 缺少,不匹配\n");
				break;	
			default:
				printf(" ] 缺少,不匹配\n");
		}
		printf("\n\t\t\t----------------檢測括號是否匹配程序----------------\n"); 
		printf("請輸入一個帶有括號的字符串:");
	}
	return 0;
} 

 

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