資源簡介
設計一個算法采用順序棧判斷表達式中的括號是否正確配對。
代碼片段和文件信息
#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<<“棧為空,不能進行刪除操作“< return?false;
}
else
{
item=st[top--];
return?true;
}
}
char?Top()//讀取棧頂元素
{
char?item;
if(top==-1)
{
cout<<“棧為空,不能讀取棧頂元素“< return?‘A
- 上一篇:MFC員工信息管理系統
- 下一篇:設A和B是兩個單鏈表,其表中元素遞增有序
評論
共有 條評論