您的位置:首页 > 理论基础 > 数据结构算法

SDUT3333&&SDUT3332数据结构实验之栈五:下一较大值(一)

2016-07-27 09:28 337 查看
#include<bits/stdc++.h>
using namespace std;
struct node
{
int num,next,id;
};
void nextmax(int n)
{
stack<struct node>s;
struct node a[100050],t;
for(int i=1; i<=n; i++)
{

scanf("%d",&a[i].num);
a[i].next=-1;
a[i].id=i;
if(s.empty())
s.push(a[i]);
else
{
while(!s.empty())
{
t=s.top();
if(a[i].num>t.num)
{
a[t.id].next=a[i].num;
s.pop();
}
else
break;
}
s.push(a[i]);
}
}
for(int i=1; i<=n; i++)
printf("%d-->%d\n",a[i].num,a[i].next);

}
int main()
{
int n,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
nextmax(n);
if(T)printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: