队列中取最大值操作问题
来源:互联网 发布:淘宝开店代销货源 编辑:程序博客网 时间:2024/06/03 15:05
#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;
}
- 队列中取最大值操作问题
- 队列中取最大值操作问题
- 队列中取最大值操作问题
- 队列中取最大值操作问题
- 队列中取最大值操作问题
- 队列中取最大值操作问题
- 队列中取最大值操作问题
- 3.7 队列中取最大值操作问题
- 队列中取最大值操作问题
- 队列中取最大值操作问题
- 队列中取最大值操作问题
- 队列中取最大值操作
- 队列中取最大值操作
- 编程之美-队列中取最大值操作问题
- 编程之美3.7--队列中取最大值操作问题
- 3.7-编程之美-队列中取最大值操作问题
- 编程之美—队列中取最大值操作问题
- 编程之美--3.7 队列中取最大值操作问题
- 《c陷阱与缺陷》读书笔记
- Android 获取手机短信
- 解决mysql连接慢和开启skip-grant-tables后访问控制失控解决方法
- Android emulator 常用快捷键
- 几道简单ACM题的解答----1
- 队列中取最大值操作问题
- virtual与override--不断更新
- c++ 指向 类成员函数(thiscall)的 函数指针
- qwt移植到s3c6410
- PHP使用zlib扩展实现页面GZIP压缩输出
- oracle用户管理
- js 回到顶部
- 这是oracle数据库里的东西,表示什么意思啊?谁能帮忙解释一下?
- Thinking About Accelerometers and Gravity