| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2144 人关注过本帖
标题:【求助】各位大神帮帮忙,关于链表的有序合并...
只看楼主 加入收藏
Aoba
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-12-21
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
【求助】各位大神帮帮忙,关于链表的有序合并...
已知有两个链表a和b,结点类型相同,均包括一个int类型的数据。编程把两个链表合并成一个,结点按升序排列。

【主要是让输入insert,merge,sort里面的内容,是评判系统上面的题目,其余是题目自带的,无法修改。】
【不知道哪里错了啊,用codeblock完全没问题,上评判系统没办法通过?我觉得是逻辑问题...用codepad看了下说是Segmentation fault...

程序代码:
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct DATA)

struct DATA
{
     long num;
     struct DATA *next;
};

struct DATA *create(int n)
{
     struct DATA *head=NULL,*p1=NULL,*p2=NULL;
     int i;
     for(i=1;i<=n;i++)
     {  p1=(struct DATA *)malloc(LEN);
        scanf("%ld",&p1->num);
        p1->next=NULL;
        if(i==1) head=p1;
        else p2->next=p1;
        p2=p1;
      }
      return(head);
}

struct DATA *merge(struct DATA *head, struct DATA *head2)
{
    struct DATA *p1;
    p1=head;
    while(p1->next!=NULL)p1=p1->next;
    p1->next=head2;
    return head;
}

struct DATA *insert(struct DATA *head, struct DATA *d)
{
    struct DATA *p0,*p1,*p2;
    p1=head;
    p0=d;
    if(head==NULL)head=p0;
    else
   {
       while(((p0->num)>(p1->num))&&(p1->next!=NULL))
       {
           p2=p1;
           p1=p1->next;
        }
     if((p0->num)<(p1->num))
      {
          if(head==p1)head=p0;
          else p2->next=p0;
          p0->next=p1;
      }
     else p1->next=p0;
    }
    return head;
}

struct DATA *sort(struct DATA *head)
{
    struct DATA *p1,*p2;
    p2=head;p1=head;
    p2=p2->next;
    p1->next=NULL;
    p1=p2;
    while(p2->next!=NULL)
    {
        p2=p2->next;
        p1->next=NULL;
        head=insert(head,p1);
        p1=p2;
    }
    head=insert(head,p1);
    return head;
}

void print(struct DATA *head)
{
    struct DATA *p;
    p=head;
    while(p!=NULL)
    {
        printf("%ld",p->num);
        p=p->next;
        printf("\n");
    }
}

main()
{
    struct DATA *head, *head2;
    int n;
    long del_num;
    scanf("%d",&n);
    head=create(n);
    scanf("%d",&n);
    head2=create(n);
    head = merge(head, head2);
    head = sort(head);
    print(head);
}




[此贴子已经被作者于2015-12-21 00:43编辑过]

搜索更多相关主题的帖子: insert color 手串 
2015-12-21 00:34
kehanping
Rank: 2
等 级:论坛游民
威 望:1
帖 子:25
专家分:88
注 册:2015-12-10
收藏
得分:14 
没问题啊,只有两个警告,一个是主函数的返回值问题,一个是主函数定义了一个变量没有使用,long del_num;。
2015-12-21 22:27
快速回复:【求助】各位大神帮帮忙,关于链表的有序合并...
数据加载中...
 
   



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

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