数据结构栈的应用

(1)建立空栈(2)初始化(3)打印(4)进栈(功能表达)(5)出栈(功能表达)(6)进出栈(功能实现)(7)检验括号是否匹配(功能实现)(8)检验括号是否匹配(功能表达)

1建立空栈

typedef struct CharStack {
    int top;
 
    int data[STACK_MAX_SIZE];
} 

2初始化

CharStackPtr charStackInit() {
	CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(struct CharStack));
	resultPtr->top = -1;
 
	return resultPtr;
}

3打印

void outputStack(CharStackPtr paraStack) {
    for (int i = 0; i <= paraStack->top; i ++) {
        printf("%c ", paraStack->data[i]);
    }
    printf("\r\n");
}

4进栈(功能表达)

void push(CharStackPtr paraStackPtr, int paraValue) {
 
    if (paraStackPtr->top >= STACK_MAX_SIZE - 1) {
        printf("Cannot push element: stack full.\r\n");
        return;
    }
 
	paraStackPtr->top ++;
 
    paraStackPtr->data[paraStackPtr->top] = paraValue;
}

5出栈(功能表达)

char pop(CharStackPtr paraStackPtr) {
 
    if (paraStackPtr->top < 0) {
        printf("Cannot pop element: stack empty.\r\n");
        return '\0';
    }
 
	paraStackPtr->top --;
 
    return paraStackPtr->data[paraStackPtr->top + 1];
}

6进出栈(功能实现)

void pushPopTest() {
    printf("---- pushPopTest begins. ----\r\n");
 
    CharStackPtr tempStack = charStackInit();
    printf("After initialization, the stack is: ");
	outputStack(tempStack);
 
	for (char ch = 'a'; ch < 'm'; ch ++) {
		printf("Pushing %c.\r\n", ch);
		push(tempStack, ch);
		outputStack(tempStack);
	}
 
	for (int i = 0; i < 3; i ++) {
		ch = pop(tempStack);
		printf("Pop %c.\r\n", ch);
		outputStack(tempStack);
	}
 
    printf("---- pushPopTest ends. ----\r\n");
}

7检验括号是否匹配(功能实现)

bool bracketMatching(char* paraString, int paraLength) {
 
    CharStackPtr tempStack = charStackInit();
	push(tempStack, '#');
	char tempChar, tempPopedChar;
 
	for (int i = 0; i < paraLength; i++) {
		tempChar = paraString[i];
 
		switch (tempChar) {
		case '(':
		case '[':
		case '{':
			push(tempStack, tempChar);
			break;
		case ')':
			tempPopedChar = pop(tempStack);
			if (tempPopedChar != '(') {
				return false;
			}
			break;
		case ']':
			tempPopedChar = pop(tempStack);
			if (tempPopedChar != '[') {
				return false;
			}
			break;
		case '}':
			tempPopedChar = pop(tempStack);
			if (tempPopedChar != '{') {
				return false;
			}
			break;
		default:
			break;
		}
	}
 
	tempPopedChar = pop(tempStack);
	if (tempPopedChar != '#') {
		return true;
	}
 
	return true;
}

8检验括号是否匹配(功能表达)

void bracketMatchingTest() {
	char* tempExpression = "[2 + (1 - 3)] * 4";
	bool tempMatch = bracketMatching(tempExpression, 17);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
 
 
	tempExpression = "( )  )";
	tempMatch = bracketMatching(tempExpression, 6);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
 
	tempExpression = "()()(())";
	tempMatch = bracketMatching(tempExpression, 8);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
 
	tempExpression = "({}[])";
	tempMatch = bracketMatching(tempExpression, 6);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
 
 
	tempExpression = ")(";
	tempMatch = bracketMatching(tempExpression, 2);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
}

利用进出栈与括号的前后相结合从而将括号内的内容进行计算,计算时结果栈中没有元素就代表为合法序列

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值