| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付买域名,送MP3、MP4
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY买空间,免费送域名(厦门中资源)
共有 185 人关注过本帖
标题:帮忙看一下 链表合并
收藏  订阅  推荐  打印 
jinxilee
Rank: 2
等级:注册会员
帖子:41
积分:600
注册:2008-4-6
帮忙看一下 链表合并

我刚学不久,不知道 问题在哪里  
#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-4-9 22:33
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.070507 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved