有序链表的归并

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

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

Output
1 14 21 23 26 28 45 50 66 99 100

一、

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    struct node *head,*tail,*p,*q,*head2;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    head2=(struct node *)malloc(sizeof(struct node));
    head2->next=NULL;
    int i,m,n;
    scanf("%d %d",&m,&n);
    tail=head;
    for(i=0;i<m;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    tail=head2;
    for(i=0;i<n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    p=head->next;
    q=head2->next;
    tail=head;
    while(p&&q)//比较
    {
        if(p->data<q->data)
        {
            tail->next=p;
            tail=p;
            p=p->next;
        }
        else
        {
            tail->next=q;
            tail=q;
            q=q->next;
        }
    }
    while(p)
    {
        tail->next=p;
        tail=p;
        p=p->next;
    }
    while(q)
    {
        tail->next=q;
        tail=q;
        q=q->next;
    }
    p=head->next;
    while(p->next)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("%d\n",p->data);
    return 0;
}

二、

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *creatseq(int n);
void display(struct node *head);
void listmerge(struct node *head1, struct node *head2);
int main()
{
    struct node *head1, *head2;
    int m, n;
    scanf("%d%d", &m, &n);
    head1 = creatseq(m); 
    head2 = creatseq(n);
    listmerge(head1, head2);
    display(head1);
    return 0;
}
struct node *creatseq(int n)
{
    struct node *head, *tail, *p;
    int i;
    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;
    tail = head;
    for(i = 0; i < n; i++)
    {
        p = (struct node *)malloc(sizeof(struct node));
        scanf("%d", &p->data);
        p->next = NULL;
        tail -> next = p;
        tail = p;
    }
    return head;
}
void display(struct node *head)
{
    struct node *p;
    p = head->next;
    while(p)
    {
        printf("%d%c", p->data, p->next ? ' ' : '\n');
        p = p->next;
    }
}
void listmerge(struct node *head1, struct node *head2)
{
    struct node *p1, *p2, *tail;
    //初始化
    p1 = head1->next;//p1指向head1第一个数据结点
    p2 = head2->next; //p2指向head2第一个数据结点
    head1->next = NULL;//新空链表借用head1
    tail = head1;//tail总是指向归并后链表尾结点
    free(head2);//释放head2
    while(p1 && p2)//比较
    {
        if(p1->data <= p2->data)
        {
            tail->next = p1;//数值较小的p1指向的结点插入tail之后
            tail = p1;  //tail指针后移
            p1 = p1->next;//p1指向下一结点
        }
        else
        {
            tail->next = p2;//数值较小的p2指向的结点插入tail之后
            tail = p2; //tail指针后移
            p2 = p2->next; //p2指向下一结点
        }
    }
    //收尾:p1或p2为空,某一个链表结点全部处理完成,将非空链表剩余结点全部插入新链表尾部。
    if(p1)
    {
        tail->next = p1;
    }
    else
    {
        tail->next = p2;
    }
}

发布了183 篇原创文章 · 获赞 5 · 访问量 8840

猜你喜欢

转载自blog.csdn.net/qq_45666654/article/details/104926687