合并有序单链表

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

Time Limit: 1000 ms Memory Limit: 65536 KiB

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
不得使用数组!
Source

#include"stdio.h"
#include"stdlib.h"
typedef struct node{
    int data;
    struct node *next;
}*Node; 
void creatNode(Node &x,int arry[],int n){
    Node temp,r;
    x=(Node)malloc(sizeof(struct node));
    r=x;
    for(int i=0;i<n;i++){
        temp=(Node)malloc(sizeof(struct node));
        temp->data=arry[i];
        r->next=temp;
        r=temp;
    }
    r->next=NULL;
}
Node Merage(Node x,Node y){ //合并两个有序单链表
if(!x) return y;
else if(!y) return x;
    Node p1=x->next,p2=y->next,p3,z;//p1 p2 来存放x y头指针便于遍历链表 p3做z的尾指针便于数据插入
    z=(Node)malloc(sizeof(struct node));
    z->next=NULL;
    p3=z;
    while(p1&&p2){                        
            p3->next=p1;
            p1=p1->next; 
        }
        else{
            p3->next=p2;
            p2=p2->next;
        }
        p3=p3->next;
    }
    p3->next=p1?p1:p2;
    return z;
}
int main(){
    int m,n;
    Node x,y,z;
    scanf("%d%d",&m,&n);
    int arry1[m],arry2[n];
    for(int i=0;i<m;i++){
        scanf("%d",&arry1[i]);
    }
      for(int i=0;i<n;i++){
        scanf("%d",&arry2[i]);
    }
    creatNode(x,arry1,m);
    creatNode(y,arry2,n);
     z=Merage(x,y);
     z=z->next;
     while(z){
        printf("%d ",z->data);
        z=z->next;
    }
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个合并两个有序单链表(L1 和 L2)并新增结点的 C 语言代码: ```c #include <stdio.h> #include <stdlib.h> //单链表结点结构体 typedef struct node { int data; struct node* next; } Node; //创建带头结点的单链表 Node* createList() { Node* head = (Node*)malloc(sizeof(Node)); head->next = NULL; return head; } //在单链表尾部插入结点 void appendNode(Node* head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; Node* p = head; while (p->next != NULL) { p = p->next; } p->next = newNode; } //合并有序单链表并新增结点 void mergeList(Node* L1, Node* L2, int data) { Node* p1 = L1->next; Node* p2 = L2->next; Node* head = createList(); Node* tail = head; while (p1 != NULL && p2 != NULL) { if (p1->data < p2->data) { tail->next = p1; p1 = p1->next; } else { tail->next = p2; p2 = p2->next; } tail = tail->next; } if (p1 == NULL) { tail->next = p2; } else { tail->next = p1; } //在合并后的单链表尾部插入结点 Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; tail = head; while (tail->next != NULL) { tail = tail->next; } tail->next = newNode; //输出合并后的单链表 printf("合并后的单链表:"); Node* p = head->next; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); //释放内存 p = head; while (p != NULL) { Node* temp = p; p = p->next; free(temp); } } int main() { //创建 L1 单链表 Node* L1 = createList(); appendNode(L1, 1); appendNode(L1, 3); appendNode(L1, 5); printf("L1 单链表:"); Node* p = L1->next; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); //创建 L2 单链表 Node* L2 = createList(); appendNode(L2, 2); appendNode(L2, 4); appendNode(L2, 6); printf("L2 单链表:"); p = L2->next; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); //合并有序单链表并新增结点 mergeList(L1, L2, 7); return 0; } ``` 输出结果: ``` L1 单链表:1 3 5 L2 单链表:2 4 6 合并后的单链表:1 2 3 4 5 6 7 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值