C语言单链表合并
这个问题有大佬会的吗,谢谢了{:10_303:}知两个非递减序列线性顺序表LA和LB,LA: {1,3,9,9,12,13,18,21} LB: {2,4,6,10,15,19,22,23,25}
设计顺序线性表合并算法,合并LA和LB为LC,要求仍然为非递减序列,最后输出合并后的LC. 去搜搜 归并排序 , 有惊喜
这跟链表有个鬼的关系 本帖最后由 jackz007 于 2022-10-2 19:33 编辑
楼主要的是这个吗?
#include <stdio.h>
int main(void)
{
int LA[] ={1,3,9,9,12,13,18,21} , LB[] = {2,4,6,10,15,19,22,23,25} , LC , i , j , k;
for(i = j = k = 0 ; k < 17 ; k ++) LC = (LA < LB && i < 8) ? LA : LB ;
printf("%d" , LC) ;
for(i = 1 ; i < 17 ; i ++) printf("\t%d" , LC) ;
printf("\n") ;
}
编译、运行实况:
D:\\C>g++ -o x x.c
D:\\C>x
1 2 3 4 6 9 9 10 12 13
15 18 19 21 22 23 25
D:\\C> #include <stdio.h>
struct node
{
int data;
struct node * next;
}*h1,*h2,*t1,*t2,a,b;
struct node * merge(struct node* h1,struct node* h2)
{
struct node *tail=NULL,*head=NULL;
while (h1&&h2)
{
if (h1->data < h2->data)
{
if (!tail)
tail=head=h1;
else
{
tail->next=h1;
tail=h1;
}
h1=h1->next;
}
else
{
if (!tail)
tail=head=h2;
else
{
tail->next=h2;
tail=h2;
}
h2=h2->next;
}
}
while (h1)
{
tail->next=h1;
tail=h1;
h1=h1->next;
}
while (h2)
{
tail->next=h2;
tail=h2;
h2=h2->next;
}
return head;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i)
{
scanf("%d",&a.data);
if (!h1)h1=t1=&a;
else
{
t1->next=&a;
t1=&a;
}
}
for (int i=1;i<=m;++i)
{
scanf("%d",&b.data);
if (!h2)h2=t2=&b;
else
{
t2->next=&b;
t2=&b;
}
}
struct node * res=merge(h1,h2);
while (res)
{
printf("%d ",res->data);
res=res->next;
}
}
页:
[1]