最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c語言通過棧判斷括號匹配是否配對

 更新時間:2023年09月22日 09:16:25   作者:gnip  
前面實(shí)現(xiàn)了棧的基本數(shù)據(jù)結(jié)構(gòu),這里來做一個聯(lián)系,用棧來解決一道比較常見的算法題,就是括號配對是否滿足規(guī)則,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下

實(shí)現(xiàn)

描述

給定一組括號,判斷是否滿足配對。

代碼

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#define bool char 
#define true 1
#define MAX_LEN 10
#define false 0
typedef  char ElementType;
typedef struct Stack {
	int top;
	ElementType stackList[MAX_LEN];
} Stack;
bool fn(char arr[], int len, Stack* S);
bool push(Stack* S, ElementType data);
bool pop(Stack* S, ElementType* x);
bool intStack(Stack* S);
int main() {
	Stack S;
	intStack(&S);
	char brackList[] = {'(', '{','(',')','}','{','}','}' };
	printf("%d", fn(brackList, sizeof(brackList) / sizeof(brackList[0]), &S));
}
bool fn(char arr[], int len, Stack* S) {
	for (int i = 0; i < len; i++) {
		if (arr[i] == '{' || arr[i] == '(') {
			push(S,arr[i]);
			printf("%c", arr[i]);
		}
		else {
			if (S->top == 0) {
				return false;
			}
			ElementType topData;
			pop(S, &topData);
			if (topData == '(' && arr[i] != ')') {
				return false;
			}
			else if (topData == '{' && arr[i] != '}') {
				return false;
			}
		}
	}
	if (S->top == 0) {
		return true;
	}
	return false;
}
//初始化
bool intStack(Stack* S) {
	for (int i = 0; i < MAX_LEN; i++) {
		S->stackList[i] = 0;
	}
	S->top = 0;
	return true;
}
//入棧
bool push(Stack* S,ElementType data) {
	S->top++;
	S->stackList[S->top] = data;
	return true;
}
//出棧
bool pop(Stack* S, ElementType *x) {
	*x = S->stackList[S->top];
	S->stackList[S->top] = 0;
	S->top--;
	return true;
}

到此這篇關(guān)于c語言通過棧判斷括號匹配是否配對的文章就介紹到這了,更多相關(guān)c語言判斷是否配對內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

容城县| 曲阜市| 壶关县| 阜宁县| SHOW| 金坛市| 工布江达县| 蒲江县| 通许县| 阿拉尔市| 阜新| 原平市| 张家港市| 通城县| 南丹县| 如皋市| 乌兰浩特市| 兰考县| 盈江县| 土默特左旗| 义马市| 通渭县| 柘荣县| 濉溪县| 定州市| 高台县| 镇江市| 黄龙县| 登封市| 灵山县| 柳林县| 栾川县| 宝鸡市| 桓台县| 新竹县| 崇仁县| 黔东| 勃利县| 伊宁市| 普宁市| 平阳县|