| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 969 人关注过本帖
标题:求灭虫~~最后输出有BUG (算法:将两个顺序单链表合并成一个单链表)
只看楼主 加入收藏
guojiayu
Rank: 2
等 级:论坛游民
帖 子:17
专家分:20
注 册:2014-11-4
结帖率:100%
收藏
 问题点数:0 回复次数:0 
求灭虫~~最后输出有BUG (算法:将两个顺序单链表合并成一个单链表)
觉得hebing函数那里有问题,但是自己试着去改,但是怎么改都是不正确...链表没有学过,望大牛带飞
程序代码:
#include<stdio.h>
#include<stdlib.h> 
struct node{
    int data;
    struct node *next;
};
struct node *hebing(struct node *h1,struct node *h2)
{
    struct node *p,*s,*h3,*a,*b;
    h3=(struct node*)malloc(sizeof(struct node));
    a=(struct node*)malloc(sizeof(struct node));
    p=h1->next;
    s=h2->next;
    h3->next=a;
    while(p!=NULL && s!=NULL)
    {
        if(p->data>=s->data){
            a->data=p->data;
            p=p->next;
            b=(struct node*)malloc(sizeof(struct node));
            a->next=b;
            a=b;
        }
        else
            {
            a->data=s->data;;
            s=s->next;
            b=(struct node*)malloc(sizeof(struct node));
            a->next=b;
            a=b;
        }
    }
    if(p!=NULL)
        while(p!=NULL){
            a->data=p->data;
            p=p->next;
            b=(struct node*)malloc(sizeof(struct node));
            a->next=b;
            a=b;
        }
     if(s!=NULL)
         while(s!=NULL){
            a->data=s->data;
            s=s->next;
            b=(struct node*)malloc(sizeof(struct node));
            a->next=b;
            a=b;
         }
         a=NULL;
         return(h3);
}


void creatl(struct node *h,int a[5])
{
    struct node *p,*s;
    int i=0;
    p=(struct node*)malloc(sizeof(struct node));
    p=h;
    while(i<5)
    {
          s=(struct node*)malloc(sizeof(struct node));
          p->next=s;
          s->data=a[i];
          p=s;
          i++;
    }
    p->next=NULL;
}

void sz(int a[5]){
    int i=0;
    printf("请输入一串数据,由大到小排列:\n");
    while(i<5){
        printf("请输入一个数字:\n");
        scanf("%d",&a[i]);
        i++;
    }
    printf("结束,返回主函数!\n");
}
main ()
{
    struct node *h1,*h2,*h0,*p;
    int a[5],b[5];
    h1=(struct node*)malloc(sizeof(struct node));
    h2=(struct node*)malloc(sizeof(struct node));
    sz(a);
    sz(b);
    printf("开始建立链表La:\n");
    creatl(h1,a);
    printf("链表La建立完成,开始建立链表Lb:\n");
    creatl(h2,b);
    printf("链表Lb建立完成,开始排序合并!\n");
    h0=hebing(h1,h2);
    printf("排序合并完成,开始输出!\n");
    p=(struct node*)malloc(sizeof(struct node));
    p=h0->next;
    while(p!=NULL){
        printf("%d\t",p->data);
        p=p->next;
    }        
    
}

搜索更多相关主题的帖子: color 灭虫 
2016-04-15 20:44
快速回复:求灭虫~~最后输出有BUG (算法:将两个顺序单链表合并成一个单链表)
数据加载中...
 
   



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

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