資源簡介
設(shè)計一個算法采用順序棧判斷表達(dá)式中的括號是否正確配對。
代碼片段和文件信息
#include
using?namespace?std;
class?CharStack
{
private:
int?maxSize;
int?top;
char?*st;
public:
CharStack(int?size)
{
maxSize=size;
top=-1;
st=new?char[maxSize];
}
CharStack()
{
top=-1;
}
~CharStack()
{
delete[]st;
}
bool?Push(char?item)//入棧
{
if(top==maxSize-1)
{
char?*newSt=new?char[maxSize*2];
for(int?i=0;i {
newSt[i]=st[i];
}
delete[]st;
st=newSt;
maxSize*=2;
}
st[++top]=item;
return?true;
}
bool?Pop(char?item)//出棧
{
if(top==-1)
{
cout<<“棧為空,不能進(jìn)行刪除操作“< return?false;
}
else
{
item=st[top--];
return?true;
}
}
char?Top()//讀取棧頂元素
{
char?item;
if(top==-1)
{
cout<<“棧為空,不能讀取棧頂元素“< return?‘A
評論
共有 條評論