标题:帮忙看一下 链表合并
只看楼主
jinxilee
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-6
 问题点数:0 回复次数: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 
2008-04-09 22:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-208100-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.349539 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved