赞
踩
#include <iostream> #include <stdlib.h> #include <string.h> #include <math.h> #define MAXSIZE 100 struct node { struct node *next; int data; }; int main(){ //定义链表 struct node *p,*h1,*h2,*q; int n1,n2; h1=(struct node *)malloc(sizeof(struct node)); h2=(struct node *)malloc(sizeof(struct node)); scanf("%d ",&n1); q=h1; for(int i=0;i<n1;i++){ p=(struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); q->next=p; q=p; } q->next=NULL; scanf("%d ",&n2); q=h2; for(int i=0;i<n2;i++){ p=(struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); q->next=p; q=p; } q->next=NULL; //合并链表为一个有序链表 struct node *h3,*z; h3=(struct node *)malloc(sizeof(struct node)); p=h1->next; q=h2->next; z=h3; while(p&&q){ if(p->data<q->data){ z->next=p; z=p; p=p->next; z->next=NULL; } else{ z->next=q; z=q; q=q->next; z->next=NULL; } } if(q==NULL){ z->next=p; } if(p==NULL){ z->next=q; } //输出h3 printf("\n"); q=h3->next; while(q){ printf("%d ",q->data); q=q->next; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。