@李长宁的笔记
#include
using namespace std;
const int StackSize=10;
template
class SeqStack
{
public:
SeqStack(){top=-1;}
~SeqStack(){}
void Push(DataType x);
DataType Pop();
DataType GetTop(){if(top!=-1) return data[top];}
int Empty(){if(top==-1)return 1;else return 0;}
private:
DataType data[StackSize];
int top;
};
int main()
{
SeqStackStack;
cout<<“判断初始栈是否为空:”;
cout<<Stack.Empty()<<endl;
cout<<“将147依次插入栈:”<<endl;
Stack.Push(1);Stack.Push(4);Stack.Push(7);
cout<<“判断当前栈是否为空:”;
cout<<Stack.Empty()<<endl;
cout<<“当前头元素为:”;
cout<<Stack.GetTop()<<endl;
cout<<“将头元素出队:”;
cout<<Stack.Pop()<<endl;
cout<<“当前头元素为:”;
cout<<Stack.GetTop()<<endl;
//括号匹配
cout<<“括号匹配,请输入十个以内个括号:”<<endl;
bool matching(char exp[]);
char k[10];
cin>>k;
cout<<matching(k);
}
//括号匹配
bool matching(char exp[])
{
int state=1,i=0;
char ch,e;
ch=exp[i++];
SeqStackS;
while(ch!=’\0’&&state)
{
if(ch==’(’||ch==’[’)S.Push(ch);
else if(ch==’)’)
if(!S.Empty()&&S.GetTop()’(’)e=S.Pop();else state=0;
else if(ch’]’)
if(!S.Empty()&&S.GetTop()’[’)e=S.Pop();else state=0;
ch=exp[i++];
}
if(state&&S.Empty())return 1;
else return 0;
}
template
void SeqStack::Push(DataType x)
{
if(topStackSize-1)throw"上溢";
data[++top]=x;
}
template
DataType SeqStack::Pop()
{
DataType x;
if(top==-1)throw"下溢";
x=data[top–];
return x;
}