编程之美——队列中取最大值操作问题
来源:互联网 发布:始祖鸟淘宝谁家靠谱 编辑:程序博客网 时间:2024/05/20 22:37
编程之美——队列中取最大值操作问题
这是一个要在队列中记录最大值的问题,但每次进队或出队又不能通过遍历去检测最大值的变化。用两个堆栈去实现一个队列是比较常见的方法,书中巧妙的用到了此方法,这样问题就转化为堆栈中取最大值操作问题。由于堆栈的变化只在栈顶,借助反向推导的思想。
代码为:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 100
class stack
{
private:
int stackItem[MAXN];
int stackTop;
int link2NextMaxItem[MAXN];
int maxStackItemIndex;
public:
stack()
{
stackTop=-1;
maxStackItemIndex=-1;
}
void push(int x)
{
stackTop++;
if(stackTop>=MAXN)
return;
stackItem[stackTop]=x;
if(x>max())
{
link2NextMaxItem[stackTop]=maxStackItemIndex;
maxStackItemIndex=stackTop;
}
else
link2NextMaxItem[stackTop]=-1;
}
int pop()
{
if(stackTop>=0)
{
int ret=stackItem[stackTop];
if(stackTop==maxStackItemIndex)
{
maxStackItemIndex=link2NextMaxItem[stackTop];
}
stackTop--;
return ret;
}
}
int max()
{
if(maxStackItemIndex>=0)
return stackItem[maxStackItemIndex];
else return -10000;
}
bool empty()
{
return stackTop==-1;
}
};
class Queue
{
public:
stack s1;
stack s2;
public:
void enque(int x)
{
s1.push(x);
}
int deque()
{
if(s2.empty())
{
while(!s1.empty())
{
s2.push(s1.pop());
}
}
return s2.pop();
}
int max()
{
return Maxvalue(s1.max(),s2.max());
}
int Maxvalue(int x,int y)
{
return x>y?x:y;
}
};
int main()
{
stack stk;
stk.push(1);
stk.push(4);
stk.push(2);
stk.push(8);
for(int i=0;i<4;i++)
{
cout<<stk.pop()<<" ";
cout<<"max value:"<<stk.max()<<endl;
}
Queue queue;
queue.enque(8);
queue.enque(4);
queue.enque(2);
queue.enque(1);
for(int j=0;j<4;j++)
{
cout<<queue.deque()<<" ";
cout<<"max value:"<<queue.max()<<endl;
}
system("pause");
return 0;
}
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 100
class stack
{
private:
int stackItem[MAXN];
int stackTop;
int link2NextMaxItem[MAXN];
int maxStackItemIndex;
public:
stack()
{
stackTop=-1;
maxStackItemIndex=-1;
}
void push(int x)
{
stackTop++;
if(stackTop>=MAXN)
return;
stackItem[stackTop]=x;
if(x>max())
{
link2NextMaxItem[stackTop]=maxStackItemIndex;
maxStackItemIndex=stackTop;
}
else
link2NextMaxItem[stackTop]=-1;
}
int pop()
{
if(stackTop>=0)
{
int ret=stackItem[stackTop];
if(stackTop==maxStackItemIndex)
{
maxStackItemIndex=link2NextMaxItem[stackTop];
}
stackTop--;
return ret;
}
}
int max()
{
if(maxStackItemIndex>=0)
return stackItem[maxStackItemIndex];
else return -10000;
}
bool empty()
{
return stackTop==-1;
}
};
class Queue
{
public:
stack s1;
stack s2;
public:
void enque(int x)
{
s1.push(x);
}
int deque()
{
if(s2.empty())
{
while(!s1.empty())
{
s2.push(s1.pop());
}
}
return s2.pop();
}
int max()
{
return Maxvalue(s1.max(),s2.max());
}
int Maxvalue(int x,int y)
{
return x>y?x:y;
}
};
int main()
{
stack stk;
stk.push(1);
stk.push(4);
stk.push(2);
stk.push(8);
for(int i=0;i<4;i++)
{
cout<<stk.pop()<<" ";
cout<<"max value:"<<stk.max()<<endl;
}
Queue queue;
queue.enque(8);
queue.enque(4);
queue.enque(2);
queue.enque(1);
for(int j=0;j<4;j++)
{
cout<<queue.deque()<<" ";
cout<<"max value:"<<queue.max()<<endl;
}
system("pause");
return 0;
}
- 编程之美—队列中取最大值操作问题
- 编程之美-队列中取最大值操作问题
- 编程之美3.7--队列中取最大值操作问题
- 3.7-编程之美-队列中取最大值操作问题
- 编程之美--3.7 队列中取最大值操作问题
- 编程之美-队列中取最大值操作问题
- 编程之美读书笔记-队列中取最大值操作问题
- 编程之美3.7——队列中取最大值操作问题
- 编程之美——队列中取最大值操作问题
- 编程之美3.7——队列中取最大值操作问题
- 《编程之美》——队列中取最大值操作的问题
- 编程之美3.7——队列中取最大值操作问题
- 编程之美之队列中取最大值操作
- 编程之美3.7队列中取最大值操作
- 读书笔记之编程之美 - 3.7 队列中取最大值操作问题
- 编程之美:第三章 结构之法 3.7队列中取最大值操作问题
- 编程之美读书笔记3.7队列中取最大值操作的问题 解法2
- [编程之美] PSet3.7 队列中取最大值操作问题
- java笔记20121216
- Tortoises SVN 教程
- Platform设备驱动
- Cygwin,Nutch安装配置,检验是否正确(对网友守望者博客的修改---在此感谢守望者)2
- Linux-2.6.32.2内核在mini2440上的移植(五)---激活RTC驱动
- 编程之美——队列中取最大值操作问题
- 学生管理系统
- C语言中定义全局变量要注意的地方
- initializing java tooling(1%)
- x264中read_frame_yuv函数欣赏
- Linux-2.6.32.2内核在mini2440上的移植(六)---添加LCD背光驱动
- html中脚本的执行顺序
- s5pv210 uboot-2012-10移植(八) 之支持SD卡保存环境变量
- 第27节 命名空间的含义