下一最大值

数据结构实验之栈五:下一较大值(一)
Time Limit: 1000MS Memory limit: 65536K
题目描述
对于包含n(1<=n<=1000)个整数的序列,对于序列中的每一元素,在序列中查找其位置之后第一个大于它的值,如果找到,输出所找到的值,否则,输出-1。

输入
输入有多组,第一行输入t(1<=t<=10),表示输入的组数;
以后是 t 组输入:每组先输入n,表示本组序列的元素个数,之后依次输入本组的n个元素。

输出
输出有多组,每组之间输出一个空行(最后一组之后没有);
每组输出按照本序列元素的顺序,依次逐行输出当前元素及其查找结果,两者之间以-->间隔。

示例输入
2
4 12 20 15 18
5 20 15 25 30 6
示例输出
12-->20
20-->-1
15-->18
18-->-1

20-->25
15-->25
25-->30
30-->-1

6-->-1

方法1栈思想.栈存放没找到下一最大值的元素.为元素编号

<pre name="code" class="cpp"># include <stdio.h>
# include <stdlib.h>
# define STACK_INIT_SIZE 100000
typedef struct node{
	int data;
	int next;
	int id;//为依次来的元素设定编号
} SelemType;
typedef struct{
	SelemType *base;
	SelemType *top;
	int stacksize;
} SqStack;

void InitStack(SqStack&S);
void Push(SqStack&S,SelemType e);
void Pop(SqStack&S,SelemType&e);
/*栈空返回ture*/
bool StackEmpty(SqStack&S);
void GetTop(SqStack&S,SelemType&e);
int main()
{
	int t,n,m,i;
	SelemType a[100000];
	SelemType etop,epop;
	SelemType*p;
	SqStack S;
	InitStack(S);
	scanf("%d",&t);
	while(t--)
	{
	    S.top = S.base;
	    scanf("%d",&m);
        for(i = 0;i < m;i++)
		{
		     scanf("%d",&a[i].data);
		     //cin>>a[i].data;
             a[i].id = i;
             a[i].next = -1;//默认每个元素找不到后一最大值,如果找到修改
		}
		for(i = 0;i < m;i++)
        {
			/*栈空直接进栈,因为还没来后面的元素*/
			if(StackEmpty(S))
			{
				Push(S,a[i]);
			}
			//  栈中的元素就是仍没找到后一最大值的元素,且栈顶元素最小!!!!
			else
            {
                while(!StackEmpty(S))
                {
                    GetTop(S,etop);
                    if(etop.data < a[i].data)
                    {
                        a[etop.id].next = a[i].data;
                        Pop(S,epop);
                    }
                    /*栈顶大于该元素*/
                    else
                    {
                         break;
                    }
                }
                /*无论怎样新来的就是来判定是否是栈中元素的后一最大值,自己没法判定自己。所以要入栈接受其后面元素的检验*/
                Push(S,a[i]);
            }
		}

		for(i = 0;i < m;i++)
	    {
		   printf("%d-->%d\n",a[i].data,a[i].next);
	    }
	    if(t)
        printf("\n");
	}
	return 0;
}

void InitStack(SqStack&S)
{
	S.base = (SelemType*)malloc(STACK_INIT_SIZE*sizeof(SelemType));
	if(!S.base)
	{
		exit(0);
	}
	S.top = S.base;
	S.stacksize = STACK_INIT_SIZE;
}

void Push(SqStack&S,SelemType e)
{

	/*放入添加的元素*/
	*(S.top) = e;
	/*栈顶S.top上移*/
	S.top++;

}

void Pop(SqStack&S,SelemType&e)
{
	/*判断栈是否为空,若为空直接返回*/
	if(S.top == S.base)
		return;
	else
	{
		//e = *--S.top;
		/*栈顶下移找到要Pop的元素*/
		S.top--;
		/*获取要出栈的元素*/
		e = *S.top;
	}
}
bool StackEmpty(SqStack&S)
{
	if(S.top == S.base)
		return true;
	else
		return false;
}
void GetTop(SqStack&S,SelemType&e)
{
	if(S.top == S.base)
		return;
	else
		e = *(S.top - 1);
}


 
<pre name="code" class="cpp"># include <stdio.h>
int main()
{
	int t,n,i,j,key;
	bool find = false;
	int a[1000];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		for(i=0;i<n;i++)
		{
			find = false;
			for(j=i+1;j<n;j++)
			{
				if(a[j]>a[i])
				{
					printf("%d-->%d\n",a[i],a[j]);
					find = true;
					break;
				}
			}
            if(!find)
				printf("%d-->-1\n",a[i]);
		}
		if(t)
		{
			printf("\n");
		}
	}
}


 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值