代码清单3-9
class stack
{
public:
stack()
{
stackTop = -1;
maxStackItemIndex = -1;
}
void Push(Type x)
{
stackTop++;
if(stackTop >= MAXN)
;
else
{
stackItem[stackTop] = x;
if(x > Max())
{
link2NextMaxItem[stackTop] = maxStackItemIndex;
maxStackItemIndex = stackTop;
}
else
link2NextMaxItem[stackTop] = -1;
}
}
Type Pop()
{
Type ret;
if(stackTop < 0)
ThrowException();
else
{
ret = stackItem[stackTop];
if(stackTop == maxStackItemIndex)
{
maxStackItemIndex = link2NextMaxItem[stackTop];
}
stackTop--;
}
return ret;
}
Type Max()
{
if(maxStackItemIndex >= 0)
return stackItem[maxStackItemIndex];
else
return –INF;
}
private:
Type stackItem[MAXN];
int stackTop;
int link2NextMaxItem[MAXN];
int maxStackItemIndex;
}