标题:【链表问题】我找了很久都找不到哪里错,求大神指教
取消只看楼主
奶牛君
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-12-20
结帖率:100%
已结贴  问题点数:20 回复次数:0 
【链表问题】我找了很久都找不到哪里错,求大神指教
【题目】已知有两个链表a和b,结点类型相同,均包括一个int类型的数据。编程把两个链表合并成一个,结点按升序排列。

#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 *p;
    p=head;
    while(p->next!=NULL)
    {
        p=p->next;
    }
    p->next=head2;
      return head;
}

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

struct DATA *sort(struct DATA *head)
{
    struct DATA *p,*q;
    p=head;
    q=head;
    q=q->next;
    p->next=NULL;
    p=q;
    while(q->next!=NULL)
    {
        q=q->next;
        p->next=NULL;
        head=insert(head,p);
        p=q;
    }
    head=insert(head,p);
    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);
}
搜索更多相关主题的帖子: include create 
2015-12-20 10:41



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




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

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