int net[]={1,2,2,1,3,3,4,4};
bool CheckBox(int net[], int n)
{
Stack<int> *s=new Stack<int>(n);
for(int i=0; i<n;i++)
{
if(!s->IsEmpty())
{
if((net[i]==net[s->Top()])
{
int x;
s->Delete(x);
}
else
s->Add(i);
}
else
s->Add(i);
}
// 是否有不可布线的网组
if(s->IsEmpty())
{
delete s;
cout<<"Switch box is routable"<<endl;
return true;
}
delete s;
cout <<"Switch box is not routable"<<endl;
return false;
}
Ref:<<数据结构,算法与应用>>P176~178
博客给出一段代码,定义了数组 net,实现了 CheckBox 函数。该函数利用栈数据结构,通过遍历数组元素,判断元素是否与栈顶元素相等来进行入栈或出栈操作,最终根据栈是否为空判断网组是否可布线,还给出了参考书籍。
999

被折叠的 条评论
为什么被折叠?



