算法复习--------------利用堆栈实现括号匹配
2015-04-30 15:34
519 查看
要匹配形如(((.....))这样的括号不匹配的,就可以将(入栈,然后当匹配到正确的)时候就出栈
具体代码如下:
template<class T>
class Stack{
public:
Stack(int MaxStackSize = 10);
~Stack(){ delete[]stack; }
bool IsEmpty() const{
return top == -1;
}
bool IsFull()const{
return top == MaxTop;
}
T Top()const;
Stack<T> &Add(const T& x);
Stack<T> & Delete(T& x);
private:
int top;
int MaxTop;
T* stack;
};
template<class T>
Stack<T>::Stack(int MaxStackSize /* = 10 */){
MaxTop = MaxStackSize - 1;
stack = new T[MaxStackSize];
top = -1;
}
template<class T>
T Stack<T>::Top()const{
if (IsEmpty())throw;
else
return stack[top];
}
template<class T>
Stack<T>& Stack<T>::Add(const T& x)
{
if (IsFull())throw;
stack[++top] = x;
return *this;
}
template<class T>
Stack<T>& Stack<T>::Delete(T& x)
{
if (IsEmpty())throw;
x = stack[top--];
return *this;
}
// ClassStackTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "ClassStack.h"
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
const int MaxLength = 100;
void PrintMatchedPairs(char* expr){
Stack<int> s(MaxLength);
int j, length = strlen(expr);
for (int i = 0; i <= length; i++)
{
if (expr[i - 1] == '(')
s.Add(i);
else if (expr[i - 1] == ')')
{
try{
s.Delete(j);
cout <<"the matched is" <<j << ' ' << i << endl;
}
catch (...){
cout << "No match for right parenthesis at" << i << endl;
}
}
}
while (!s.IsEmpty())
{
s.Delete(j);
cout << "no match for left parentsthesis at " << j << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char expr[MaxLength];
cout << " type an expression of length at most " << MaxLength << endl;
cin.getline(expr, MaxLength);
cout << "the [pairs of matching parenthess in " << endl;
puts(expr);
cout << "are" << endl;
PrintMatchedPairs(expr);
getchar();
getchar();
return 0;
}
以上代码就完成了对括号的匹配,但是程序有个问题,就是当右边括号多余左边括号的时候就会出现内存泄露了
具体代码如下:
template<class T>
class Stack{
public:
Stack(int MaxStackSize = 10);
~Stack(){ delete[]stack; }
bool IsEmpty() const{
return top == -1;
}
bool IsFull()const{
return top == MaxTop;
}
T Top()const;
Stack<T> &Add(const T& x);
Stack<T> & Delete(T& x);
private:
int top;
int MaxTop;
T* stack;
};
template<class T>
Stack<T>::Stack(int MaxStackSize /* = 10 */){
MaxTop = MaxStackSize - 1;
stack = new T[MaxStackSize];
top = -1;
}
template<class T>
T Stack<T>::Top()const{
if (IsEmpty())throw;
else
return stack[top];
}
template<class T>
Stack<T>& Stack<T>::Add(const T& x)
{
if (IsFull())throw;
stack[++top] = x;
return *this;
}
template<class T>
Stack<T>& Stack<T>::Delete(T& x)
{
if (IsEmpty())throw;
x = stack[top--];
return *this;
}
// ClassStackTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "ClassStack.h"
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
const int MaxLength = 100;
void PrintMatchedPairs(char* expr){
Stack<int> s(MaxLength);
int j, length = strlen(expr);
for (int i = 0; i <= length; i++)
{
if (expr[i - 1] == '(')
s.Add(i);
else if (expr[i - 1] == ')')
{
try{
s.Delete(j);
cout <<"the matched is" <<j << ' ' << i << endl;
}
catch (...){
cout << "No match for right parenthesis at" << i << endl;
}
}
}
while (!s.IsEmpty())
{
s.Delete(j);
cout << "no match for left parentsthesis at " << j << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char expr[MaxLength];
cout << " type an expression of length at most " << MaxLength << endl;
cin.getline(expr, MaxLength);
cout << "the [pairs of matching parenthess in " << endl;
puts(expr);
cout << "are" << endl;
PrintMatchedPairs(expr);
getchar();
getchar();
return 0;
}
以上代码就完成了对括号的匹配,但是程序有个问题,就是当右边括号多余左边括号的时候就会出现内存泄露了
相关文章推荐
- 利用栈实现括号匹配算法
- 利用栈实现括号匹配算法!
- Java堆栈的应用1----------堆栈的自定义实现以及括号匹配算法的Java实现
- 利用STL栈实现括号匹配算法
- 考研数据结构与算法之堆栈的使用(三)利用堆栈匹配括号
- Java-利用栈实现括号匹配
- C++,利用链式栈实现括号匹配,界面友好,操作方便,运行流畅
- 用堆栈实现括号匹配——ADT实现
- 利用C语言中后进先出的栈的概念实现括号匹配
- 15数组实现顺序堆栈的括号匹配问题
- 数据结构13: 括号匹配算法及C语言实现
- 简单算法——使用栈实现括号匹配检验
- 堆栈数据结构应用之括号匹配检查(算法3.2.2)
- 基于PHP实现栈数据结构和括号匹配算法示例
- 不用堆栈实现的括号匹配
- 算法——括号匹配问题(堆栈应用)
- C语言实现括号匹配,中缀表达式转后缀表达式并计算的算法
- 使用堆栈来实现括号匹配的检查
- 算法复习(5)链表实现的堆栈
- 练习:利用颜色直方图匹配算法实现图像中目标的识别