#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class stack
{
private:
int stackItem[1000];
int stackTop;
int link2NextMaxItem[1000];
int maxStackItemIndex;
public:
stack()
{
stackTop=-1;
maxStackItemIndex=-1;
}
void Push(int x)
{
stackTop++;
if(stackTop >= 1000)
{
;
}
else
{
stackItem[stackTop] = x;
if(x > Max())
{
link2NextMaxItem[stackTop]=maxStackItemIndex;
maxStackItemIndex=stackTop;
}
else
{
maxStackItemIndex=-1;
}
}
}
int Pop()
{
int ret;
if(stackTop < 0)
{
//ThrowException();
}
else
{
ret=stackItem[stackTop];
if(stackTop == maxStackItemIndex)
{
maxStackItemIndex=link2NextMaxItem[stackTop];
}
stackTop--;
return ret;
}
}
int Max()
{
if(maxStackItemIndex >=0)
{
return stackItem[maxStackItemIndex];
}
else
{
return -5622;
}
}
bool empty()
{
return stackTop == -1;
}
};
class Queue
{
private:
stack stackA;
stack stackB;
public:
void EnQueue(int x)
{
stackB.Push(x);
}
int DeQueue()
{
if(stackA.empty())
{
while(!stackB.empty())
{
stackA.Push(stackB.Pop());
}
}
return stackA.Pop();
}
int Queue :: Max()
{
return MaxValue(stackA.Max(),stackB.Max());
}
int MaxValue(int x,int y)
{
return x>y?x:y;
}
};
int main()
{
stack mxt;
mxt.Push(1);
mxt.Push(4);
mxt.Push(2);
mxt.Push(8);
for(int i=0;i<4;i++)
{
cout<<mxt.Pop();
cout<<"max:"<<mxt.Max();
cout<<endl;
}
Queue mxq;
mxq.EnQueue(8);
mxq.EnQueue(4);
mxq.EnQueue(2);
mxq.EnQueue(1);
for(int i=0;i<4;i++)
{
cout<<mxq.DeQueue();
cout<<"max:"<<mxq.Max();
cout<<endl;
}
system("pause");
return 0;
}
using namespace std;
#include<vector>
#include<algorithm>
class stack
{
private:
int stackItem[1000];
int stackTop;
int link2NextMaxItem[1000];
int maxStackItemIndex;
public:
stack()
{
stackTop=-1;
maxStackItemIndex=-1;
}
void Push(int x)
{
stackTop++;
if(stackTop >= 1000)
{
;
}
else
{
stackItem[stackTop] = x;
if(x > Max())
{
link2NextMaxItem[stackTop]=maxStackItemIndex;
maxStackItemIndex=stackTop;
}
else
{
maxStackItemIndex=-1;
}
}
}
int Pop()
{
int ret;
if(stackTop < 0)
{
//ThrowException();
}
else
{
ret=stackItem[stackTop];
if(stackTop == maxStackItemIndex)
{
maxStackItemIndex=link2NextMaxItem[stackTop];
}
stackTop--;
return ret;
}
}
int Max()
{
if(maxStackItemIndex >=0)
{
return stackItem[maxStackItemIndex];
}
else
{
return -5622;
}
}
bool empty()
{
return stackTop == -1;
}
};
class Queue
{
private:
stack stackA;
stack stackB;
public:
void EnQueue(int x)
{
stackB.Push(x);
}
int DeQueue()
{
if(stackA.empty())
{
while(!stackB.empty())
{
stackA.Push(stackB.Pop());
}
}
return stackA.Pop();
}
int Queue :: Max()
{
return MaxValue(stackA.Max(),stackB.Max());
}
int MaxValue(int x,int y)
{
return x>y?x:y;
}
};
int main()
{
stack mxt;
mxt.Push(1);
mxt.Push(4);
mxt.Push(2);
mxt.Push(8);
for(int i=0;i<4;i++)
{
cout<<mxt.Pop();
cout<<"max:"<<mxt.Max();
cout<<endl;
}
Queue mxq;
mxq.EnQueue(8);
mxq.EnQueue(4);
mxq.EnQueue(2);
mxq.EnQueue(1);
for(int i=0;i<4;i++)
{
cout<<mxq.DeQueue();
cout<<"max:"<<mxq.Max();
cout<<endl;
}
system("pause");
return 0;
}