码迷,mamicode.com
首页 > 其他好文 > 详细

两条带头结点的升序重复合并成一个无重复的升序链表

时间:2018-08-05 01:11:58      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:lse   重复   stdio.h   color   col   nod   std   相同   ext   

#include<stdio.h>

#include<stdlib.h>

typedef struct node{

   int  data;

   struct node * next;

}ElemSN;

ElemSN  * Createlink(int a[],int n){ //建立带表头结点的链表

int i;

ElemSN * h, * p;

h=p=(ElemSN *)malloc(sizeof(ElemSN));

        h->next=NULL;

        for( i=0;i<n;i++){

              p=p->next=(ElemSN *)malloc(sizeof(ElemSN));

      p->data =a[i];

      p->next=NULL;

}

return h;

   }

void Fun(ElemSN*head1,ElemSN*head2){

      ElemSN*h=NULL,*p,*t,*s;

      while(head1->next&&head2->next) {        //任意一条链表为空,循环结束。判断剩余链表的头结点与生成新链表的尾结点的值是否相同,

                                                                           //相同则free(p),头结点后移

            if(head1->next->data<head2->next->data){

    p=head1->next;

            head1->next=p->next; 

    }

  else{

p=head2->next;

head2->next=p->next;

  }

 p->next=NULL;

         if(!h)

h=t=p;

else{

      if(t->data==p->data)

          free(p);

      else

  t=t->next=p;

 }

      }

      if(head1->next)

  s=head1->next;

      else

  s=head2->next;

      while(s){

            p=s;

            s=s->next;

            p->next=NULL;

            if(p->data==t->data)

       free(p);

           else

       t=t->next=p;

      }

      head1->next=h;

}

   void Printlink(ElemSN * h){

   ElemSN * p;

   for(p=h;p->next;p=p->next)

   printf("%2d\n",p->next->data);

   }

    int main(void){   

int a[9]={1,2,4,4,5,7,7,8,9};

int b[8]={2,3,3,4,10,12,12,13}; 

ElemSN * head1,*head2;

        head1=Createlink(a,9);

head2=Createlink(b,8);

Fun(head1,head2);

free(head2);

Printlink(head1);

}



两条带头结点的升序重复合并成一个无重复的升序链表

标签:lse   重复   stdio.h   color   col   nod   std   相同   ext   

原文地址:http://blog.51cto.com/13645380/2154814

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!