bool push(DualStack &DS, T x, int d) {
if (DS.t[0] + 1 == DS.t[1]) return false; // 棧滿
if (d == 0) DS.t[0]++;
else DS.t[1]--;
DS.Vector[DS.t[d]] = x;
return true;
}
DualStack &DS 是雙棧結構體的引用**,包含:
Vector:底層數組,存放兩棧數據。t[0]:0 號棧棧頂索引(從 -1 向上增長)。t[1]:1 號棧棧頂索引(從 MAXSIZE 向下增長)。t[0]/t[1] 及數組內容 Vector。DualStack DS,修改僅影響副本,調用者棧狀態不變。template <typename T, int MAXSIZE>
struct DualStack {
T Vector[MAXSIZE];
int t[2]; // t[0] = -1, t[1] = MAXSIZE 初始化
DualStack() : t{-1, MAXSIZE} {}
};