C++数据结构之栈的实现(顺序栈)
首先我们定义了一个栈模板类,它有几个纯虚函数,分别是栈类最常用的几个函数
- Push() 将元素放入栈顶
- Top() 返回栈顶元素值
- Pop() 弹出栈顶元素
- Clear() 清空栈(析构函数会用到)
同样我们定义了判断栈是否为空函数IsEmpty(),以及保护成员—栈的高度(长度)height;
模板类定义代码如下
template<class type>
class abstack
{
public:
bool IsEmpty()
{
return((height == 0) ? true : false);
}
virtual void Push(type&) = 0;
virtual type Top() = 0;
virtual bool Pop(type&) = 0;
virtual void Clear() = 0;
protected:
unsigned height;
};
数组栈、链表栈可由该模板派生而来。下面介绍一下数组栈(也叫顺序栈),因为数组栈比较好写 。
栈的实现用到了判断函数assert,头文件要将它引入。
代码如下:
//顺序栈实现
template<class type>
class SeqStack :public abstack<type> //由抽象模板类派生
{
public:
SeqStack(int i); //构造函数
SeqStack(SeqStack&s) //拷贝构造函数
{
Copy(s);
}
~SeqStack() //析构函数
{
Clear();
}
void Push(type& x); //元素入栈
bool Pop(type& x); //弹出栈顶元素(以引用形式返回)
type Top(); //返回栈顶元素值
void Clear() { delete elements; } //清栈
SeqStack& Copy(SeqStack& s); //这里新增了一个拷贝函数
SeqStack& operator=(SeqStack& s) //重载运算符
{
delete elements;
Copy(s);
return this;
}
bool IsFull() //判断栈是否已满,防止上溢
{
return (top == (maxsize - 1) ? true : false);
}
protected:
int top; //top代表栈顶元素在数组的下标
type* elements;//一维数组指针存放栈中元素
int maxsize; //栈的最大长度
};
template<class type>
type SeqStack<type>::Top()
{
return elements[top];
}
template<class type>
SeqStack<type>::SeqStack(int i)
{
SeqStack::height = 0;
top = -1;
maxsize = i > 10 ? i : 10;
elements = new type[maxsize];
assert(elements);
}
template<class type>
void SeqStack<type>::Push(type& x)
{
assert(!IsFull());
elements[++top] = x;
SeqStack::height++;
}
template<class type>
bool SeqStack<type>::Pop(type& x)
{
if (SeqStack::IsEmpty())
return false;
else
{
x = elements[top];
top--;
SeqStack::height--;
}
return true;
}
template<class type>
SeqStack<type>& SeqStack<type>::Copy(SeqStack& s)
{
maxsize = s.maxsize;
elements = new type[maxsize];
assert(elements);
int len = s.height;
for (int i = 0;i < len;i++)
elements[i] = *(s.elements + i);
top = s.top;
SeqStack::height = s.height;
return *this;
}
这样一个顺序栈就写好了,为了测试一下,用一道比较经典的括号匹配问题试一下。
问题描述
假设一个算术表达式可以包含三种括号:“(”和“)”,方括号“[”和“]”,及花括号“ { ”和“ } ”,且这三种括号可嵌套使用。试设计算法判断给定表达式中所含括号是否配对出现。
测试用例
{3*[4*(5+6)+7]}
{3+4*(7+8)+5*(9+9}
期待输出
括号配对
括号不配对
代码实现
#include<iostream>
#include<assert.h>
using namespace std;
//栈模板
template<class type>
class abstack
{
public:
bool IsEmpty()
{
return((height == 0) ? true : false);
}
virtual void Push(type&) = 0;
virtual type Top() = 0;
virtual bool Pop(type&) = 0;
virtual void Clear() = 0;
protected:
unsigned height;
};
//顺序栈实现
template<class type>
class SeqStack :public abstack<type>
{
public:
SeqStack(int i);
SeqStack(SeqStack&s)//拷贝构造函数
{
Copy(s);
}
~SeqStack()
{
Clear();
}
void Push(type& x);
bool Pop(type& x);
type Top();
void Clear() { delete elements; }
SeqStack& Copy(SeqStack& s);
SeqStack& operator=(SeqStack& s)
{
delete elements;
Copy(s);
return this;
}
bool IsFull()
{
return (top == (maxsize - 1) ? true : false);
}
protected:
int top;
type* elements;//一维数组指针存放栈中元素
int maxsize;
};
template<class type>
type SeqStack<type>::Top()
{
return elements[top];
}
template<class type>
SeqStack<type>::SeqStack(int i)
{
SeqStack::height = 0;
top = -1;
maxsize = i > 10 ? i : 10;
elements = new type[maxsize];
assert(elements);
}
template<class type>
void SeqStack<type>::Push(type& x)
{
assert(!IsFull());
elements[++top] = x;
SeqStack::height++;
}
template<class type>
bool SeqStack<type>::Pop(type& x)
{
if (SeqStack::IsEmpty())
return false;
else
{
x = elements[top];
top--;
SeqStack::height--;
}
return true;
}
template<class type>
SeqStack<type>& SeqStack<type>::Copy(SeqStack& s)
{
maxsize = s.maxsize;
elements = new type[maxsize];
assert(elements);
int len = s.height;
for (int i = 0;i < len;i++)
elements[i] = *(s.elements + i);
top = s.top;
SeqStack::height = s.height;
return *this;
}
int main()
{
SeqStack<char> s(60000);
char c[60000], ch;
cin >> c;
int i = 0;
int flag = 0;
while (c[i] != '\0')
{
ch = c[i];
i++;
if (ch == '[' || ch == '{' || ch == '(')
s.Push(ch);
else if (ch == ']')
{
if (s.IsEmpty())
{
flag = 1;
break;
}
else if (s.Top() != '[')
{
flag = 1;
break;
}
else
{
char a;
s.Pop(a);
}
}
else if (ch == '}')
{
if (s.IsEmpty())
{
flag = 1;
break;
}
else if (s.Top() != '{')
{
flag = 1;
break;
}
else
{
char a;
s.Pop(a);
}
}
else if (ch == ')')
{
if (s.IsEmpty())
{
flag = 1;
break;
}
else if (s.Top() != '(')
{
flag = 1;
break;
}
else
{
char a;
s.Pop(a);
}
}
}
if (!s.IsEmpty())
flag = 1;
if (flag)
cout << "括号不配对" << endl;
else
cout << "括号配对" << endl;
return 0;
}
结果不给你们看了,肯定是对的。