当前位置: 代码迷 >> 综合 >> Math Dash的两个随机数链表合并程序
  详细解决方案

Math Dash的两个随机数链表合并程序

热度:94   发布时间:2023-12-02 09:37:51.0

代码如下: 

#include <stdio.h>
#include <stdlib.h>
struct LNode
{int data;struct LNode *next;
};
struct LNode *a0()
{struct LNode *a;a=(struct LNode*)malloc(sizeof(struct LNode));a->next=NULL;return a;
}
struct LNode *a1(int i)
{int b,*c;c=(int*)malloc(i*sizeof(int));for(b=0;b<i;b++){c[b]=rand()%10;}int d,e;d=i;for(;;){for(e=0;e<d-1;e++){if(c[e]>c[e+1]){int f;f=c[e];c[e]=c[e+1];c[e+1]=f;}if(c[e]==c[e+1]){int f;for(f=e;f<d;f++){c[f]=c[f+1];}d--;}}for(e=0;e<d-1;e++){if(c[e]>=c[e+1]){break;}}if(e==d-1){break;}}struct LNode *a,*b0;a=a0();b0=a;for(b=0;b<d;b++){struct LNode *f;f=(struct LNode*)malloc(sizeof(struct LNode));f->data=c[b];f->next=NULL;b0->next=f;b0=f;}return a;
}
struct LNode *a2(struct LNode *a,struct LNode *b)
{struct LNode *c,*d,*e,*f;f=a;c=f;d=a->next;e=b->next;while(d&&e){if(d->data>e->data){c->next=e;c=e;e=e->next;}else{c->next=d;c=d;d=d->next;}}if(c->next==e){c->next=d;}else{c->next=e;}struct LNode *g,*h;g=f;h=f->next;while(h){if(!(h->next&&(h->data==h->next->data))){g->next=h;g=h;}h=h->next;}return f;
}
void jie_mian_system()
{char h[]="Math Dash的两个随机数链表合并程序";char i[strlen(h)+6];sprintf(i,"title %s",h);system(i);puts(h);srand((unsigned)time(NULL));struct LNode *a,*c,*e;a=a1(10);//*创建一个单链表e=a;c=a->next;while(c){printf("%d\n",c->data);c=c->next;}printf("\n");struct LNode *b,*d,*f;b=a1(5);//*创建另一个单链表f=b;d=b->next;while(d){printf("%d\n",d->data);d=d->next;}printf("\n");struct LNode *g;g=a2(&(*e),&(*f))->next;//*合并两个单链表while(g){printf("%d\n",g->data);g=g->next;}system("pause");
}
main()
{jie_mian_system();return 0;
}

程序运行结果如下:

Math Dash的两个随机数链表合并程序
0
1
2
3
5
6
8

0
3
5
6
8

0
1
2
3
5
6
8
请按任意键继续. . .

  相关解决方案