<强>题目:强>
,一个数组(1 . . n)来实现两个栈,使得两个栈中的元素总和不到n时,两个都不会发生上溯。
思路(1):
,,创建一个数组,分别从两边开始,依次往中间走。
思路(2):
,创建一个数组,一个走奇数位,一个走偶数位。
//奇偶方式 # define _CRT_SECURE_NO_WARNINGS # includeusing namespace 性传播疾病; template class TwoStack { 公众: TwoStack (int 大小) :_top1 (1), _top2 (1), _len(大小) { 时间=_arr new T(大小); } ~ TwoStack () { if (_arr) { 删除[],_arr; } } void 推动(int 指数,int 数据); T 流行(int 指数); bool IsEmpty (int 指数); void Disp (); T 最高(int 指数); 私人: int _top1; int _top2; int _len; T *, _arr; }; template void TwoStack _len-2  | |, _top2>=_len-1)//{//cout & lt; & lt;,“溢出”,& lt; & lt;, endl;//返回;//}//}//其他//{//if (_top1 祝辞_len | |, _top2 祝辞=,_len 作用;2)//{//cout & lt; & lt;,“溢出”,& lt; & lt;, endl;//返回;//}//} int flag =, _len %, 2; if (index ==, 0) { if (flag ==, 0) { if (_top1 祝辞=,_len 作用;2) { cout & lt; & lt;,“Stack1 溢出”,& lt; & lt;, endl; 返回; } } if (flag ==, 1) { if (_top1 祝辞=,_len 作用;1) { cout & lt; & lt;,“溢出”,& lt; & lt;, endl; 返回; } }////////////////////////////if (_top1 ==, 1) { 时间=_top1 0; _arr [_top1],=,数据; } 其他的 { _top1 +=2; _arr [_top1],=,数据; } }/////////////////////////////////////////////////////////其他的 { if (flag ==, 0) { if (_top2 祝辞=,_len 作用;2) { cout & lt; & lt;,“Stack2 溢出”,& lt; & lt;, endl; 返回; } } if (flag ==, 1) { if (_top2 祝辞=,_len 作用;1) { cout & lt; & lt;,“溢出”,& lt; & lt;, endl; 返回; } }///////////////if (_top2 ==, 1) { 时间=_top2 1; _arr [_top2],=,数据; } 其他的 { _top2 +=2; _arr [_top2],=,数据; } } } template T TwoStack bool TwoStack void TwoStack T TwoStack 一个数组实现两个栈