Chain list of data structure experiment: Merging of ordered chain list

Source: Internet
Author: User

Chain list of data structure experiment: Merging of ordered chain list Time limit:1000ms Memory limit:65536k The title description enters two sequential integer sequences (containing M and n data respectively), establishes two ordered single-linked lists, merges the two ordered single-linked lists into a large ordered single-linked list, and sequentially outputs the merged single-linked list data. Enter the values of M and N in the first line;
The second line enters m ordered integers sequentially;
The third line, in turn, enters N ordered integers. The output of the merged single-linked list contains an ordered integer m+n. Sample input
6 51 23 26 45 66 9914 21 28 50 100
Sample output
1 14 21 23 26 28 45 50 66 99 100
Tip do not use arrays! Source

Sample Program

#include <stdio.h> #include <stdlib.h>struct node {int data; struct node *next;};/    /build Order list struct node *creat (int n) {struct node *head, *tail, *p;    int i;    head= (struct node *) malloc (sizeof (struct node));    head->next=null;    Tail=head;        for (i=1;i<=n;i++) {p= (struct node *) malloc (sizeof (struct node));        scanf ("%d", &p->data);        p->next=null;        tail->next=p;    Tail=p; } return head;    struct node * Merge (struct node *head1, struct node *head2) {struct node *p1, *P2, *tail;    p1=head1->next;    p2=head2->next;    Tail=head1;    Free (head2);            while (P1&AMP;&AMP;P2) {if (P1->data < P2->data) {tail->next=p1;            TAIL=P1;        p1=p1->next;            } else {tail->next=p2;            TAIL=P2;        p2=p2->next;    }} if (p1) tail->next=p1;    else tail->next=p2; return (HEAD1);} int main ({struct node * head1, *head2, *p;    int n, m;    scanf ("%d%d", &n,&m);    Head1=creat (n);    Head2=creat (m);    P=merge (HEAD1,HEAD2);        while (P->next->next!=null) {printf ("%d", p->next->data);    p=p->next;    } printf ("%d\n", p->next->data); return 0;}


Chain list of data structure experiment: Merging of ordered chain list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.