题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2053
应用:创建单链表,合并单链表,打印单链表
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct node { int data; char name [200]; struct node *next; }LinkList; LinkList *creat(int n) { LinkList *head,*p,*tail; int i; head=(LinkList *)malloc(sizeof(LinkList)); head->next=NULL; tail=head; for(i=0;i<n;i++) { p=(LinkList *)malloc(sizeof(LinkList)); scanf("%s",&p->name); scanf("%d",&p->data); p->next=NULL; tail->next=p; tail=p; } return head; } void displayall(LinkList *L) { LinkList *p; p=L->next; while(p!=NULL) { if(p->next!=NULL) printf("%s ",p->name); else printf("%s\n",p->name); p=p->next; } } LinkList *merge(LinkList *L1,LinkList *L2) { LinkList *p,*q,*tail; p=L1->next; q=L2->next; tail=L1; while(p&&q) if(p->data<q->data) { tail->next=q; tail=q; q=q->next; } else if(p->data==q->data && strcmp(p->name,q->name)>0) { tail->next=q; tail=q; q=q->next; } else { tail->next=p; tail=p; p=p->next; } if(p) tail->next=p; else tail->next=q; return L1; } int main() { int n,m; scanf("%d",&m); LinkList *L1,*L2,*L; scanf("%d\n",&n); L1=creat(n); m--; while(m--) { scanf("%d\n",&n); L2=creat(n); L=merge(L1,L2); } displayall(L); return 0; }