【数据结构】如何使用栈来判定括号是否匹配

版权声明:无意呢。 https://blog.csdn.net/qq_41900081/article/details/86557457

题目:

先定义堆栈的几个基本操作,再设计一主函数利用堆栈的操作完成以下功能:假设一个算术表达式中可以包含三种括号:()[]{},且这三种括号可以按任意次序嵌套使用(如:…[…{…}…[…]…]…(…))。编写判别给定表达式中所含括号是否正确配对出现的算法。

算法:

a)创建一个栈
b)当(当前字符不等于输入的结束字符)

  • 如果当前字符不是括号符,则忽略它
  • 如果字符是一个开分隔符(如【,{,),那么将其入栈
  • 如果字符是一个闭分隔符(如】,}),且栈不为空,栈顶元素出栈,否则,匹配失败
  • 如果出栈的字符不是匹配的开分隔符,提示匹配错误

c)字符chulinix结束后,如果栈不为空,则提示匹配错误。

C++版本实现

#include<iostream>
using namespace std;
 
typedef struct Stack
{
	char  data;
	struct Stack *next;
}Stack, *Linkstack;
Linkstack Init_stack(Linkstack &S)
{
	S=NULL;
	return S;
 } 
Linkstack Push(Linkstack &S, char e) 
 {
 	Linkstack p= new Stack;
 	if(!p) exit(0);
 	p->data=e;
 	p->next=S;
 	S=p;
	return S;		
 }
bool Stack_empty(Linkstack &S)
{
	if(S==NULL) return true;
	else return false;
}
char Pop(Linkstack &S)
{   Linkstack p= new Stack;
	if(Stack_empty(S))	{cout<<"栈空!!!";return 0;} 
	else 
		{   
			int x=S->data;	
	        p=S;
	        S=S->next;
	        delete p;	
	        return x;
	  	}
}
char get_top(Linkstack &S)
{
	if(!Stack_empty(S)) 
		return S->data;
}

bool isMatch(Linkstack &S){
	char ch;
    cout<<"输入表达式:";
    ch=getchar();
	while(ch!='\n')
	{   
		switch(ch)
		{
			case '[':Push(S,ch);break;
			case '(':Push(S,ch);break;
			case '{':Push(S,ch);break;
					
			
			case ']':if(!Stack_empty(S)&&get_top(S)=='[')
			             Pop(S);
			         else 
					 {
			         	cout<<"匹配失败!"<<endl;
			         	return false;
					 }
			         break;
			case ')':if(!Stack_empty(S)&&get_top(S)=='(')
			             Pop(S);
			         else 
					 {
			         	cout<<"匹配失败!"<<endl;
			         	return false;
					 }
			         break;
			case '}':if(!Stack_empty(S)&&get_top(S)=='{')
			             Pop(S);
			         else 
					 {
			         	cout<<"匹配失败!"<<endl;
			         	return false;
					 }
			         break;
		
		}
		ch=getchar();
	 } 
	
	if(Stack_empty(S))   
		 cout<<"匹配成功"<<endl;
	else cout<<"匹配失败!"<<endl;
}
int  main()
{   
    Linkstack S;
	Init_stack(S);
	while(true){
		isMatch(S);
	}
	return 0;
 } 

Java版本实现


package dataStructure;

import java.util.Scanner;

public class IsMatch {
	static boolean isMatch(LLStack stack) {	
		System.out.println("请输入表达式:");
		Scanner scanner = new Scanner(System.in);
		String curString = scanner.next();
		//字符处理
		for(int i = 0; i < curString.length(); i++) {
			char ch = curString.charAt(i);
			switch(ch) {	
				case '(' :
					stack.push(ch);
					break;
				case '[' : 
					stack.push(ch);
					break;
				case '{' : 
					stack.push(ch);
					break;
				case ')' :
					if(stack.isEmpty()) {
						System.out.println("匹配失败");
						return false;
					}else if(stack.top() != '(' ) {					
							stack.pop();	
							System.out.println("匹配失败");
							return false;
						  }else {
								 stack.pop();
							  }	
					break;
				case ']' :
					if(stack.isEmpty()) {
						System.out.println("匹配失败");
						return false;
					}else if(stack.top() != '[' ) {					
							stack.pop();	
							System.out.println("匹配失败");
							return false;
						  }else {
							 stack.pop();
						  }	
					break;
				case '}' :
					if(stack.isEmpty()) {
						System.out.println("匹配失败");
						return false;
					}else if(stack.top() != '{' ) {					
							stack.pop();
							System.out.println("匹配失败");
							return false;
						  }else {
								 stack.pop();
							  }	
					break;
				}		
			}
		//字符处理后栈非空
		if(!stack.isEmpty()) {
			System.out.println("匹配失败");
			return false;
		}		
		System.out.println("匹配成功");
		return true;
	}
	public static void main(String[] args) {
		LLStack stack = new LLStack();
		while(isMatch(stack)) {
			
		}
		
	}
}

栈的java实现

class LLNode{
	private char data;
	private LLNode next = null;
	
	public LLNode(char d){
		data = d;
	}
	public char getData() {
		return data;
	}
	public void setData(char data) {
		this.data = data;
	}
	public LLNode getNext() {
		return next;
	}
	public void setNext(LLNode next) {
		this.next = next;
	}
}
public class LLStack {
	private LLNode  head;
	
	boolean isEmpty() {
		return head == null;
	}
	
	int size() {
		int count = 1;
		LLNode cur = head;
		if(isEmpty())  return 0;
		while(cur.getNext() !=null ) {
			cur = cur.getNext();
			count++;
		}
		return count;
	}
	
	char top() {
		if(isEmpty()) 
			return 0;
		return head.getData();
	}
	
	void push(char d) {
		if(isEmpty()) {
			head =new LLNode(d);		
		}else {
			LLNode node = new LLNode(d);
			node.setNext(head);
			head = node;
		}
	}
	
	char pop() {
		if(isEmpty())
			new Exception("栈空");
		LLNode del = head;
		head = head.getNext();
		return del.getData();
	}
	void display() {
		if(isEmpty()) {
			System.out.println("栈空");
		}else {
			LLNode cur = head;
			System.out.print("栈内元素:");
			while(cur != null) {
				System.out.print(cur.getData() + "->");
				cur = cur.getNext();
			}
			System.out.println();
		}
	}
	
	void deleteStack() {
		head  = null;
	}

猜你喜欢

转载自blog.csdn.net/qq_41900081/article/details/86557457