3332-数据结构实验之栈与队列五:下一较大值(二)

来源:互联网 发布:ai软件 编辑:程序博客网 时间:2024/05/16 11:23
#include <bits/stdc++.h>using namespace std;typedef int ElemType;struct Node{    ElemType data;    ElemType num;    ElemType next;}p[100100];int main(){    int T;    struct Node t;    cin >> T;    while(T--)    {        stack<struct Node>Q;        int k;        cin >> k;        for(int i = 0; i < k; i++)        {            cin >> p[i].data;            p[i].num = i;            p[i].next = -1;        }        for(int i = 0; i < k; i++)        {            if(Q.empty())            {                Q.push(p[i]);            }            else            {                t = Q.top();                while(!Q.empty() && p[i].data > t.data)                {                    int x = t.num;                    p[x].next = p[i].data;                    Q.pop();                    if(Q.empty())                        break;                    t = Q.top();                }                Q.push(p[i]);            }        }        for(int i = 0; i < k; i++)        {            printf("%d-->%d\n",p[i].data,p[i].next);        }        if(T)        {            cout << endl;        }    }    return 0;}
阅读全文
0 0