3.1.2顺序栈的应用:括号匹配问题

1.代码

#include <stdio.h>
#include <iostream>
using namespace std;
#define MAXSIZE 10
#define OK 1
#define ERROR 0

typedef int Status;
typedef char selemtype;

typedef struct stack
{
	selemtype* base;
	selemtype* top;
	int stacksize;
}Sqstack;

Status InitStack(Sqstack& s)//初始化
{
	s.base = new selemtype[MAXSIZE];
	if (!s.base)
		exit(OVERFLOW);
	s.top = s.base;
	s.stacksize = MAXSIZE;
	return OK;
}

Status Push(Sqstack& s, selemtype e)//入栈
{
	if (s.top - s.stacksize == s.base)//***栈满***

		return ERROR;
	*s.top++ = e;//先获取e的值,在进行移位
	return OK;
}

selemtype Pop(Sqstack& s)//出栈
{
	if (s.top == s.base)
		return ERROR;
	selemtype e;
	e = *--s.top;
	return e;
}

selemtype GetTop(Sqstack s)//获取栈顶元素
{
	if (s.top != s.base)
	{
		return *(s.top - 1);
	}
}

void output(Sqstack s)//输出
{
	while (s.base != s.top)
	{
		cout << *s.base;
		s.base++;
	}
}
void test()
{
	Sqstack tempstack;
	InitStack(tempstack);
	cout << "After initialization,the stack is: " << endl;
	output(tempstack);
	for (char ch = 'a'; ch < 'm'; ch++)
	{
		cout << "\nPushing " << ch << endl;
		Push(tempstack, ch);
		output(tempstack);
	}
	char ch;
	for (int i = 0; i < 3; i++)
	{
		ch = Pop(tempstack);
		cout << "\nPop " << ch << endl;
		output(tempstack);
	}
	cout << "\nThe test is end.";
}
bool matching(const char* parastring, int paralength)//匹配
{
	Sqstack tempstack;
	InitStack(tempstack);
	Push(tempstack, '#');
	char tempchar, popchar;
	for (int i = 0; i < paralength; i++)
	{
		tempchar = parastring[i];
		switch (tempchar)
		{
		case '(':
		case '[':
		case '{':
			Push(tempstack, tempchar);
			break;

		case ')':
			popchar = Pop(tempstack);
			if (popchar != '(')
				return false;
			break;

		case ']':
			popchar = Pop(tempstack);
			if (popchar != '[')
				return false;
			break;

		case '}':
			popchar = Pop(tempstack);
			if (popchar != '{')
				return false;
			break;
		default:
			break;
		}
	}
	popchar = Pop(tempstack);
	if (popchar != '#')
	{
		return false;
	}
	return true;
}
void matchingtest()
{
	const char* tempexpression;//相当于定义的tempexpression是一个常量,不可对当前地址的值进行二次修改;
	tempexpression = "[2 + (1 - 3)]*4";
	bool tempmatch = matching(tempexpression, 15);
	cout << "Is the expression " << tempexpression << " bracket matching?" << tempmatch << endl;

	tempexpression = "())";
	tempmatch = matching(tempexpression, 3);
	cout << "Is the expression " << tempexpression << " bracket matching?" << tempmatch << endl;

	tempexpression = "()()(())";
	tempmatch = matching(tempexpression, 8);
	cout << "Is the expression " << tempexpression << " bracket matching?" << tempmatch << endl;

	tempexpression = "({}[])";
	tempmatch = matching(tempexpression, 6);
	cout << "Is the expression " << tempexpression << " bracket matching?" << tempmatch << endl;

	tempexpression = ")(";
	tempmatch = matching(tempexpression, 2);
	cout << "Is the expression " << tempexpression << " bracket matching?" << tempmatch << endl;
}

int main()
{
	test();
	matchingtest();
	return 0;
}

1.const如果放在*的左边,修饰的是指针指向的内容,保证指针指向的内容不能通过指针来改变。但是指针变量本身的内容可变。

2.const如果放在*的右边,修饰的是指针变量本身,保证了指针变量的内容不能修改,但是指针指向的内容,可以通过指针改变。{【C语言 关键字const的作用】https://mbd.baidu.com/ma/s/keYzu4cE}

1.指针本身是一个变量,存放的值是32位的无符号整数,这个整数是一个32位的地址。
2.通过这个32位的整数地址值,可以找到另外一个数据,就是这个指针指向的数据了,可以是任何了类型的数据。也就是指针指向的值了。(http://zhidao.baidu.com/question/108212046/answer/346636108)

这里的tempexpression是指针本身,*tempexpression是指针所指的值。

2.运行结果

在这里插入图片描述

3.关于顺序栈

1.栈具有后进先出(last in first out即LIFO),但需注意的是,最先进栈的元素不代表最后出栈。栈对线性表的插入删除位置做了限制,但并没有对出栈和入栈的时间做限制。也就是说,在不是所有元素都入栈的情况下,事先入栈的元素也可以在任意时间出栈,只要保证每次出栈的元素都是栈顶元素就可以。(http://t.csdn.cn/bDLyl)
2.顺序栈需要指定栈的存储单元大小(动态可变),对于push和pop等操作,要注意考虑栈满栈空的情况。
3.输出时也可使用int n装s.top的值,判断其是否为-1(先进行初始化)。
4.清空栈可直接用覆盖法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值