|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个问题有大佬会的吗,谢谢了
知两个非递减序列线性顺序表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.
- #include <stdio.h>
- struct node
- {
- int data;
- struct node * next;
- }*h1,*h2,*t1,*t2,a[100001],b[100001];
- 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[i].data);
- if (!h1)h1=t1=&a[i];
- else
- {
- t1->next=&a[i];
- t1=&a[i];
- }
- }
- for (int i=1;i<=m;++i)
- {
- scanf("%d",&b[i].data);
- if (!h2)h2=t2=&b[i];
- else
- {
- t2->next=&b[i];
- t2=&b[i];
- }
- }
- struct node * res=merge(h1,h2);
- while (res)
- {
- printf("%d ",res->data);
- res=res->next;
- }
- }
复制代码
|
|