有序链表的归并(数据结构)

链表的归并有好多种做法,链表的难度并不是在思想上,而是在代码的实现上,指针的来回转换很容易将人弄混乱,一会不知道指针到哪了,一个好好的指针可能在不知不觉中就变成了一个野指针,没有对象也没有内存。。。

以SDUT的一个题目为例来解释一下链表的归并:

数据结构实验之链表四:有序链表的归并

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

分别输入两个有序的整数序列(分别包含M和N个数据),建立两个有序的单链表,将这两个有序单链表合并成为一个大的有序单链表,并依次输出合并后的单链表数据。

Input

第一行输入M与N的值; 
第二行依次输入M个有序的整数;
第三行依次输入N个有序的整数。

Output

输出合并后的单链表所包含的M+N个有序的整数。

Sample Input

6 5
1 23 26 45 66 99
14 21 28 50 100

Sample Output

1 14 21 23 26 28 45 50 66 99 100

Hint

不得使用数组!

这个题的思想很容易就可以想到,要么就建立第三个链表来进行对前两个链表的输出,或者比较复杂的就是用第三个链表把前两个链表中的每一个结点都“扣”下来连接到第三个链表上,然后输出第三个链表。还有一种方法是对第一种方法进行了优化,思想一样,但是代码效率较高

先附上第一种:(用c++写的,还没学c++的童鞋也可以看的,c和c++没有什么区别的,代码的主体是一样的)

#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
	int data;
	struct node*next;
}tree;
int main()
{
	ios::sync_with_stdio(false);
	struct node*head1,*head2,*p,*q,*s,*tail1,*tail2,*head3,*tail;
	head1=new tree;
	head2=new tree;
	head1->next=NULL;
	head2->next=NULL;
	tail1=head1;
	tail2=head2;
	int m,n,i;
	cin>>m>>n;
	for(i=0;i<=m-1;i++)
	{
		p=new tree;
		cin>>p->data;
		p->next=NULL;
		tail1->next=p;
		tail1=p;
	}
	for(i=0;i<=n-1;i++)
	{
		q=new tree;
		cin>>q->data;
		q->next=NULL;
		tail2->next=q;
		tail2=q;
	}
	head3=new tree;
	head3->next=NULL;
	tail=head3;
	p=head1->next;
	q=head2->next;
	int c1=0,c2=0;
	while(p&&q)
	{
		if(p->data<q->data)
		{
			s=new tree;
			s->data=p->data;
			p=p->next;
			tail->next=s;
			tail=s;
			c1++;
		}
		else
		{
			s=new tree;
			s->data=q->data;
			q=q->next;
			tail->next=s;
			tail=s;
			c2++;
		}
	}
	for(i=c1;i<=m-1;i++)
	{
		s=new tree;
		s->data=p->data;
		p=p->next;
		tail->next=s;
		tail=s;
	}
	for(i=c2;i<=n-1;i++)
	{
		s=new tree;
		s->data=q->data;
		q=q->next;
		tail->next=s;
		tail=s;
	}
	p=head3->next;
	while(p)
	{
		if(p->next==NULL)
		{
			cout<<p->data<<endl;
		}
		else
		{
			cout<<p->data<<" ";
		}
		p=p->next;
	}
	return 0;
}

接下来是第二种代码,想挑战一下自己的童鞋可以好好理解一下哦:

(C的代码)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct linshi
{
    int data;
    struct linshi *next;//创建链表所用的结构体
};

 struct linshi *head1,*head2,*p,*q,*r,*s;//定义全局变量,可以省略函数间传递指针这个麻烦的过/                                         //程

 void set(int);               //函数声明
 void chuli(void);

void set(int k)//创建链表的过程就不多解释了..
{
    int a;
    while(k--)
    {
        scanf("%d",&a);
        q=(struct linshi *)malloc(sizeof(struct linshi)*1);
        q->data=a;
        p->next=q;
        q->next=NULL;
        p=p->next;
    }
    return ;
}

void chuli(void)
{
    r=head2->next;
    while(r!=NULL)
    {
        s=(struct linshi *)malloc(sizeof(struct linshi)*1);
        s->data=r->data;
        s->next=NULL;
        p=head1->next;
        q=head1;
        while(p!=NULL)
        {
            if(s->data<p->data)
            {
                q->next=s;
                s->next=p;
                break;
            }
            q=p;
            p=p->next;
        }
        if(p==NULL)
        {
            s->next=NULL;
            q->next=s;
        }
        r=r->next;
    }
    p=head1->next;
    while(p!=NULL)
    {
        printf("%d",p->data);
        if(p->next!=NULL)
        {
            printf(" ");
        }
        p=p->next;
    }
    printf("\n");
}

int main()
{
    int m=0,n=0;
    head1=(struct linshi *)malloc(sizeof(struct linshi)*1);
    head1->next=NULL;
    head2=(struct linshi *)malloc(sizeof(struct linshi)*1);
    head2->next=NULL;
    p=(struct linshi *)malloc(sizeof(struct linshi)*1);
    q=(struct linshi *)malloc(sizeof(struct linshi)*1);
    r=(struct linshi *)malloc(sizeof(struct linshi)*1);
    scanf("%d%d",&m,&n);
    p=head1;
    set(m);
    p=head2;
    set(n);
    chuli();
    return 0;
}

 第三种代码(依旧是C++):

#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
	int data;
	struct node*next;
}tree;
int main()
{
	ios::sync_with_stdio(false);//加速输入输出
	int m,n;
	cin>>m>>n;
	struct node *head1,*head2,*p,*q,*tail1,*tail2;
	head1=new tree;
	head2=new tree;
	head1->next=NULL;
	head2->next=NULL;
	tail1=head1;
	tail2=head2;
	for(int i=0;i<=m-1;i++)//建立链表
	{
		p=new tree;
		cin>>p->data;
		p->next=NULL;
		tail1->next=p;
		tail1=p;
	}
	for(int i=0;i<=n-1;i++)//建立链表
	{
		q=new tree;
		cin>>q->data;
		q->next=NULL;
		tail2->next=q;
		tail2=q;
	}
	p=head1->next;
	q=head2->next;
	struct node * head3,* tail;
	head3=new tree;
	head3->next=NULL;
	tail=head3;
	 while (p && q)
    {
        if (p->data > q->data)
        {
            tail->next = q;
            tail = q;
            q = q->next;
        }
        else
        {
            tail->next = p;
            tail = p;
            p = p->next;
        }
    }
    if (p)//连接未输出的链表的结点
        tail->next = p;
    if (q)
        tail->next = q;
	p=head3->next;
	while(p)//输出链表三
	{
		if(p->next==NULL)
		{
			cout<<p->data<<endl;
		}
		else
		{
			cout<<p->data<<" ";
		}
		p=p->next;
	}
	return 0;
}

还请大佬们多多指点!

猜你喜欢

转载自blog.csdn.net/weixin_44015865/article/details/85010039