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&&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