栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构 C 代码6:栈以及括号匹配

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构 C 代码6:栈以及括号匹配

一.栈的定义

栈 是一个线性表,插入和删除位置限制在线性表的尾部进行要插入X,只能插入在最后一个元素的后面,也只能删除最后一个元素

栈是插入和删除受到限制的线性表

通常将允许删除,插入操作的一端称为栈顶,另一端称为栈底

不含任何一个元素的栈称为空栈

栈是一种先进后出(Last IN First Out)的线性表,简称LIFO
栈的插入操作被形象的称为进栈,或入栈(压栈)
删除操作被称为出栈,或退栈(弹出)

二.代码实现

1.栈的结构定义

typedef struct CharStack {
    int top;

    int data[STACK_MAX_SIZE];
} *CharStackPtr; 

2.输出数据

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

3.打印

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

4.入栈

void push(CharStackPtr paraStackPtr, int paraValue){
	if(paraStackPtr->top>=STACK_MAX_SIZE-1){
		printf("can't push , Stack is full.nr");
		return;
	}
	paraStackPtr->top++;
	paraStackPtr->data[paraStackPtr->top]=paraValue;
}

5.出栈

char pop(CharStackPtr paraStackPtr){
	if(paraStackPtr->top<0){
		printf("can't pop , Stack is empty.rn");
		return '';
	}
	paraStackPtr->top--;
	return paraStackPtr->data[paraStackPtr->top+1];
}

6.测试

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

7.测试结果

---- pushPopTest begins. ----
After initialization, the stack is:
Pushing a.
a
Pushing b.
a b
Pushing c.
a b c
Pushing d.
a b c d
Pushing e.
a b c d e
Pushing f.
a b c d e f
Pushing g.
a b c d e f g
Pushing h.
a b c d e f g h
Pushing i.
a b c d e f g h i
Pushing j.
a b c d e f g h i j
Pushing k.
Cannot push element: stack full.
a b c d e f g h i j
Pushing l.
Cannot push element: stack full.
a b c d e f g h i j
Pop j.
a b c d e f g h i
Pop i.
a b c d e f g h
Pop h.
a b c d e f g
---- pushPopTest ends. ----
Press any key to continue
三.括号匹配

栈结构具有后进先出的特点。括号匹配问题描述:若表达式中包含三种括号:圆括号、方括号和花括号,它们可以相互嵌套。

算法思想:检验算法中可设置一个栈,每读入一个括号,若是左括号,则直接进栈,等待相匹配的同类右括号;若读入的是右括号,且与当前栈顶左括号同类型,则二者匹配,将栈顶的左括号弹出,否则属于不合法情况。另外。如果输入序列已经读完,而栈中仍有等待匹配的左括号,或者读入一个右括号,而栈中已无等待匹配的同类型左括号,均属于不合法情况。当输入序列和栈同时变空的时候,说明所有括号完全匹配。

四.代码演示

1.栈的应用

bool bracketMatching(char* paraString, int paraLength){
	char tempChar, tempPopedChar;
	CharStackPtr tempStack=charStackInit();
	push (tempStack,'#');
	for(int i=0;i 

2.代码测试

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


	tempExpression = "( )  )";
	tempMatch = bracketMatching(tempExpression, 6);
	printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch);

	tempExpression = "()()(())";
	tempMatch = bracketMatching(tempExpression, 8);
	printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch);

	tempExpression = "({}[])";
	tempMatch = bracketMatching(tempExpression, 6);
	printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch);


	tempExpression = ")(";
	tempMatch = bracketMatching(tempExpression, 2);
	printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch);
}

3.测试结果

Is the expression '[2 + (1 - 3)] * 4' bracket matching? 1
Is the expression '( )  )' bracket matching? 0
Is the expression '()()(())' bracket matching? 1
Is the expression '({}[])' bracket matching? 1
Is the expression ')(' bracket matching? 0
Press any key to continue
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/873063.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号