堆栈实现·括号匹配问题

堆栈的练习题,就是输入一堆括号,告诉你他们是否左右括号匹配了,如果不匹配则输出第一个不匹配的位置

思路非常简单,直接看代码

一直贯彻自文档化(self-documenting)的代码习惯 

// 括号匹配问题
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

// 栈元素存储的信息类型
// symbol 括号类型
// position 括号位置
struct cdataItem
{
	char symbol;
	int position;
};
typedef struct cdataItem dataItem;

// 栈元素的定义
struct cnode
{
	dataItem data;
	struct cnode *next;
};
typedef struct cnode node;

void stack_initial( node** topPtr );
void stack_push( node** topPtr, const dataItem symbol_new );
dataItem stack_pop( node** topPtr );
int stack_is_empty( const node* topPtr );
void stack_free( node** topPtr );
int parenthesis_matching( char a, char b );

// 栈的初始化
void stack_initial( node** topPtr)
{
	*topPtr = ( node* )malloc( sizeof( node ) );
	( *topPtr )->next = NULL;
	( *topPtr )->data.symbol = '\0';
}

// 栈的压入
void stack_push( node** topPtr, const dataItem data_new )
{
	node *tempPtr = ( node* )malloc( sizeof( node ) );
	tempPtr->next = *topPtr;
	tempPtr->data = data_new;
	*topPtr = tempPtr; 
}

// 栈的弹出
dataItem stack_pop( node** topPtr )
{
	dataItem temp_data = ( *topPtr )->data;
	if ( ( *topPtr )->next != NULL )
	{
		node *tempPtr = *topPtr;
		*topPtr = ( *topPtr )->next;
		free( tempPtr );
	}
	return temp_data;
}


// 判断栈是否为空
int stack_is_empty( const node* topPtr )
{
	if ( topPtr->next == NULL )
		return 1;
	else
		return 0;
}

// 释放栈的内存空间
void stack_free( node** topPtr )
{
	while ( *topPtr != NULL )
	{
		node *tempPtr = *topPtr;
		*topPtr = ( *topPtr )->next;
		free( tempPtr );
	}
}

// 括号匹配
int parenthesis_matching( char a, char b )
{
	if ( a > b )
	{
		char temp = a;
		a = b;
		b = temp;
	}
	if ( ((a=='[')&&(b==']')) || ((a=='{')&&(b=='}')) || ((a=='(')&&(b==')')) )
		return 1;
	else
		return 0;
}

int main()
{
	dataItem temp;
	int i, fail_check;
	const int len = 100;
	char now_char, now_string[ len ];
	node *topPtr = NULL;
	fputs( "Welcome to Will's program for parenthesis matching!\n", stdout );
	fputs( "Remember position starts at 0\n", stdout );
	fputs( "If you want to terminate the input, just input \"end\"\nInput:  ", stdout );
	fgets( now_string, len - 1, stdin );
	while ( strcmp( now_string, "end\n" )   )
	{
		stack_initial( &topPtr );
		fail_check = 0;
		for ( i = 0; i < strlen( now_string ) - 1; i++ )
		{
			now_char = now_string[ i ];
			switch ( now_char )
				{
					case '(':
					case '[':
					case '{':	
						temp.position = i;
						temp.symbol = now_char;
						stack_push( &topPtr, temp );
						break;
					case ')':
					case ']':
					case '}':
						if ( ! parenthesis_matching( now_char, stack_pop( &topPtr ).symbol ) )
						{
							printf( "Fail to match: position at %d\n", i );
							fail_check = 1;
						}
						break;
				}
			if ( fail_check )
				break;
		}
		if ( ! stack_is_empty( topPtr ) )
		{
			printf( "Fail to match: position at %d\n", stack_pop( &topPtr ).position );
			stack_free( &topPtr );
		}
		else
			if ( ! fail_check )
				fputs( "Successfully matched\n", stdout );
		fputs( "If you want to terminate the input, just input \"end\"\nInput:  ", stdout );
		fgets( now_string, len - 1, stdin );
	}
	fputs( "Good day!", stdout );
}

 

    原文作者:括号匹配问题
    原文地址: https://blog.csdn.net/Will0Huang/article/details/88704673
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞