STL实现
通过应用STL中的stack
实现:
.pop
弹出栈顶元素.top()
返回栈顶元素.empty()
询问栈是否为空.push()
向栈顶插入一个元素
#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的位置存入‘#’,代表栈空
如果为左括号,就入栈;
如果为右括号,就拿出栈顶元素与之比较,如果不能与之匹配,就说明不能整个公式或括号字符串不能完成括号匹配。
最后如果栈不为空,说明还有未被匹配的括号,即tempPopedChar != '#',就说明不能匹配
注意栈的特点,先入的被压在底部,先入后出,后入先出。
而括号匹配问题恰好能够运用这一特点。