一个C语言有关问题,比较复杂,麻烦哪位高人给看看
一个C语言问题,比较复杂,麻烦哪位高人给看看.
从标准输入上读入N(1 <= N <= 200000)个由空白符分隔的整数。对这N个数从小到大连续编号,相同的数应具有相同的编号。在标准输出上按这N个数输入时的顺序输出它们的编号序列。例如,设输入数据为 5 3 4 7 3 5 6,则输出数据为3 1 2 5 1 3 4。
我的思想是生成两个链表,一个按输入顺序排列,一个按从小到大排列,按第二个链表生成序号,再将第二个链表的序号复制到第一个链表,结果程序在DEBUG时运行正常,结果也正确,但一旦start without debug 就会出错.麻烦给看一下
#include <stdio.h>
#include <stdlib.h>
typedef struct list{
int num;
int serial;
struct list *next;
}LIST;
LIST *insert(LIST *input,int in);
LIST *compose(LIST *temp,int in);
LIST *sequence(LIST *input,LIST *temp);
int main()
{
LIST *input=NULL,*temp=NULL,*p,*q;
char c;
int in;
while((c=getchar())!= '\n ')
{
ungetc(c,stdin);
scanf( "%d ",&in);
input=insert(input,in);
temp=compose(temp,in);
}/*数据输入*/
input=sequence(input,temp);
p=input;
while(p!=NULL)
{
if(p-> next!=NULL)
printf( "%d ",p-> serial);
else
printf( "%d\n ",p-> serial);/*数据输出*/
q=p;
p=p-> next;
}
return 0;
}
LIST *insert(LIST *input,int in)/*生成未排序的链表*/
{
LIST *p,*q;
p=(LIST *)malloc(sizeof(LIST *));
p-> num=in;
p-> serial=0;
p-> next=NULL;
if(input==NULL)
{
return p;
}
else
{
q=input;
while(q-> next!=NULL)
{
q=q-> next;
}
q-> next=p;
return input;
}
}
LIST *compose(LIST *temp,int in)/*生成排序后的链表*/
{
LIST *p,*q,*r;
p=(LIST *)malloc(sizeof(LIST *));
p-> num=in;
p-> serial=0;
p-> next=NULL;
if(temp==NULL||in <temp-> num)
{
p-> next=temp;
temp=p;
return temp;
}
else
{
q=temp;
while(q!=NULL&&in> =q-> num)
{
r=q;
q=q-> next;
}
p-> next=q;
r-> next=p;
return temp;
}
}
LIST *sequence(LIST *input,LIST *temp)/*生成数据的序号*/
{
LIST *p,*q,*r;
int i=0;
p=temp;
q=NULL;
while (p!=NULL)
{
if(q==NULL||p-> num> q-> num)
p-> serial=++i;
q=p;
p=p-> next;
}
r=input;
while(r!=NULL)
{
p=temp;
while(p!=NULL)
{
if(r-> num==p-> num)
{
r-> serial=p-> serial;
break;
}
p=p-> next;
}
r=r-> next;
}
return input;
}
------解决方案--------------------
从标准输入上读入N(1 <= N <= 200000)个由空白符分隔的整数。对这N个数从小到大连续编号,相同的数应具有相同的编号。在标准输出上按这N个数输入时的顺序输出它们的编号序列。例如,设输入数据为 5 3 4 7 3 5 6,则输出数据为3 1 2 5 1 3 4。
我的思想是生成两个链表,一个按输入顺序排列,一个按从小到大排列,按第二个链表生成序号,再将第二个链表的序号复制到第一个链表,结果程序在DEBUG时运行正常,结果也正确,但一旦start without debug 就会出错.麻烦给看一下
#include <stdio.h>
#include <stdlib.h>
typedef struct list{
int num;
int serial;
struct list *next;
}LIST;
LIST *insert(LIST *input,int in);
LIST *compose(LIST *temp,int in);
LIST *sequence(LIST *input,LIST *temp);
int main()
{
LIST *input=NULL,*temp=NULL,*p,*q;
char c;
int in;
while((c=getchar())!= '\n ')
{
ungetc(c,stdin);
scanf( "%d ",&in);
input=insert(input,in);
temp=compose(temp,in);
}/*数据输入*/
input=sequence(input,temp);
p=input;
while(p!=NULL)
{
if(p-> next!=NULL)
printf( "%d ",p-> serial);
else
printf( "%d\n ",p-> serial);/*数据输出*/
q=p;
p=p-> next;
}
return 0;
}
LIST *insert(LIST *input,int in)/*生成未排序的链表*/
{
LIST *p,*q;
p=(LIST *)malloc(sizeof(LIST *));
p-> num=in;
p-> serial=0;
p-> next=NULL;
if(input==NULL)
{
return p;
}
else
{
q=input;
while(q-> next!=NULL)
{
q=q-> next;
}
q-> next=p;
return input;
}
}
LIST *compose(LIST *temp,int in)/*生成排序后的链表*/
{
LIST *p,*q,*r;
p=(LIST *)malloc(sizeof(LIST *));
p-> num=in;
p-> serial=0;
p-> next=NULL;
if(temp==NULL||in <temp-> num)
{
p-> next=temp;
temp=p;
return temp;
}
else
{
q=temp;
while(q!=NULL&&in> =q-> num)
{
r=q;
q=q-> next;
}
p-> next=q;
r-> next=p;
return temp;
}
}
LIST *sequence(LIST *input,LIST *temp)/*生成数据的序号*/
{
LIST *p,*q,*r;
int i=0;
p=temp;
q=NULL;
while (p!=NULL)
{
if(q==NULL||p-> num> q-> num)
p-> serial=++i;
q=p;
p=p-> next;
}
r=input;
while(r!=NULL)
{
p=temp;
while(p!=NULL)
{
if(r-> num==p-> num)
{
r-> serial=p-> serial;
break;
}
p=p-> next;
}
r=r-> next;
}
return input;
}
------解决方案--------------------