当前位置: 代码迷 >> C语言 >> 帮忙看一下 链表合并
  详细解决方案

帮忙看一下 链表合并

热度:240   发布时间:2008-04-09 22:33:22.0
帮忙看一下 链表合并
我刚学不久,不知道 问题在哪里  
#include<stdio.h>
#include<malloc.h>
#define null 0
#define len sizeof(struct node)
struct node
{int num;
struct node *next;
};
int number=0;

struct node *create(void)
{struct node *head,*p1,*p2;
int n=0;
p1=p2=(struct node *)malloc(len);
scanf("%d",&p1->num);
head=null;
  while(p1->num!=0)
  {n=n+1;
   if(n==1) head=p1;
   else p2->next=p1;
   p2=p1;
   p1=(struct node *)malloc(len);
   scanf("%d",&p1->num);
  }
   p2->next=null;
   head->num=n;
   return(head);
}

struct node *merge(struct node *head_x,struct node *head_y)
{struct node *p1,*p2,*z,*r;
  z=head_x;
  p1=head_x->next;
  p2=head_y->next;
if(head_x=null&&head_y=null)
   printf("z=null\n");
   z->next=null;
   r=z;
   free(head_y);
else
  {while((p1->next!=null)&&(p1->next!=null))
     p1->next=p2;
     p2->next=p1;
     p1=p2->next;
     p2=p1->next;
     r=z;
  }
     if(head_x->num>=head_y->num)
     {if(head_y->num%2==0)
       r->next=p2;
      else if(head_y->num%2==1)
       r->next=p1;
      }
      else
     {if(head_x->num%2==0)
      r->next=p1;
      else
       r->next=p2;
     }
     return(z);
}
void print(struct node *head)
{struct node *p;
  printf("\nnow,these %dnum are :\n",number);
  p=head;
  if(head!=null)
  do
   {printf("%d",p->num);
    p=p->next;
   }while(p!=null);
}

void main()
{struct node *head_x,*head_y,*head;
printf("input the link_x:\n");
head_x=create();
printf("input the link_y:\n");
head_y=create();
number=head_x->num+head_y->num;
head=merge(head_x,head_y);
print(head);
}
搜索更多相关的解决方案: 链表  node  struct  num  int  

----------------解决方案--------------------------------------------------------
  相关解决方案