鱼三省 2016-11-26 14:58 采纳率: 33.3%
浏览 1067
已采纳

请大神帮我调下我这程序bug

#include<stdio.h>
#include<stdlib.h>

typedef struct _List{
        int data;
        struct _List* next;
}*Node,List;

void SoloDelete(Node a);//删除链表
void Insert_1(Node a,int n);//在a后插入一个新节点,数据域为n
Node CreatList();//创建一个链表
void PrintList(Node a);//输出链表储存的值
void Combine(Node a,Node b);//将b中不属于a的节点插入a

int main(){
    List *ha = CreatList();
    List *hb = CreatList();
    printf("creat\n");
    Combine( ha, hb);
    PrintList(ha);

    SoloDelete(ha);
    SoloDelete(hb);
    int i,j;
    printf("你想创建多长的链表?请输入:\n");//创建
    scanf("%d",&j);
    for( i = 0; i <3 ;++i){
printf("请输入储存在此节点的数据\n");
    scanf("%d",&j);
    }
    printf("你想创建多长的链表?请输入:\n");//创建
    scanf("%d",&i);
    for( i = 0; i <3 ;++i){
printf("请输入储存在此节点的数据\n");
    scanf("%d",&j);
    }
    printf("合并后的ha为:1 6 5 4 2 3\n");
    return 0;
}

void Insert_1(Node a,int n){//在a后插入一个新节点,数据域为n
    Node c;
    c = (Node)malloc(sizeof(List));
    if(a->next){
        Node b;
        b = (Node)malloc(sizeof(List));
        b = a->next;
        a->next = c;
        c->next = b;
        b->next = NULL;
    }
    else {
        a->next = c;
        c->next = NULL;
        c->data = n;
    }
}


void SoloDelete(Node a){//删除一个节点
    while(a->next){
        Node c = a->next;
        free(a);
        a = c;
    }
}

Node CreatList(){
    int i = 0;
    int j = 0;
    int n = 0;
    List* ha;
    ha = (Node)malloc(sizeof(List));
    printf("你想创建多长的链表?请输入:\n");//创建
    scanf("%d",&i);
    for( j = 0 ; j<i ; ++j){
        printf("请输入储存在此节点的数据\n");
        scanf("%d",&n);
        Insert_1(ha,n);
    }
    return ha;
}

void PrintList(Node a){
        printf("print\n");
    List* b;
        b = a;
    while ( b ) {
        b = b->next;
        printf( "%d ", b->data);
    }
}

void Combine(Node a,Node b){
        //printf("combine\n");
    List* c;
    List* d;
    c = b->next;
        d = a->next;

        int status = 0;
    while(d->next){//将hb中不属于ha的节点插入ha
            //printf("while\n");
                status = 0;
                while(c->next){
                        printf("ehilr\n");
                        if(d->data==c->data){
                                status = 1;
                                break;
                        }
                        c = c->next;
                }
                if(status==0){
                        Insert_1(a,c->data);
                }
                d = d->next;
        }
        //printf("dfsfs\n");
}



  • 写回答

3条回答 默认 最新

  • YXTS122 2016-11-27 21:24
    关注
     #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct _List{
            int data;
            struct _List* next;
    }*Node,List;
    
    void SoloDelete(Node a);//删除链表
    void Insert_1(Node a,int n);//在a后插入一个新节点,数据域为n
    Node CreatList();//创建一个链表
    void PrintList(Node a);//输出链表储存的值
    void Combine(Node a,Node b);//将b中不属于a的节点插入a
    
    int main(){
        List *ha = CreatList();
        List *hb = CreatList();
        printf("creat\n");
        Combine( ha, hb);
        PrintList(ha);
    
        SoloDelete(ha);
        SoloDelete(hb);
        return 0;
    }
    
    void Insert_1(Node a,int n){//在a后插入一个新节点,数据域为n
        Node c;
        c = (Node)malloc(sizeof(List));
        if(a->next){
            c->data=n;
            c->next = a->next;
            a->next=c;
        }
        else {
            a->next = c;
            c->next = NULL;
            c->data = n;
        }
    }
    
    
    void SoloDelete(Node a){//删除一个节点
        while(a->next){
            Node c = a->next;
            free(a);
            a = c;
        }
    }
    
    Node CreatList(){
        int i = 0;
        int j = 0;
        int n = 0;
        List* ha;
        ha = (Node)malloc(sizeof(List));
        printf("你想创建多长的链表?请输入:\n");//创建
        scanf("%d",&i);
        for( j = 0 ; j<i ; ++j){
            printf("请输入储存在此节点的数据\n");
            scanf("%d",&n);
            Insert_1(ha,n);
        }
        return ha;
    }
    
    void PrintList(Node a){
            printf("print\n");
        List* b;
            b = a->next;
        while ( b) {
            printf( "%d ", b->data);
            b=b->next;
        }
    }
    
    void Combine(Node a,Node b){
            //printf("combine\n");
        List* c;
        List* d;
        int f[100],e[100];
        int k=0,m=0,t=0,j=0;
            c=b->next;
            while (c)
            {
                f[m]=c->data;
                m++;
                c=c->next;
            }
            for (k=0;k<m;k++)
            {
                d=a->next;
                while (d)
                {
                    if (f[k]==d->data)
                          break;
                    d=d->next;
                }
                if (d==NULL)
                {
                         e[j]=f[k];
                         j++;
                }
            }
            /*
            int status = 0;
        while(d){//将hb中不属于ha的节点插入ha
          c=b->next;
          printf("ehilr\n");
                    while(c){
                            status=0;
                           if(d->data==c->data){
                                    status = 1;
                            }
    
                    if(status==0){
                        for (k=0;k<m;k++)
                        {
                            if(f[k]==c->data)
                                break;
                        }
                        if(k==m)
                        {
                            f[k]=c->data;
                            m++;
                        }
                          for (k=0;k<m;k++)
          {
            if (f[k]==d->data)
            {
                for (t=k;t<m-1;t++)
                     f[t]=f[t+1];
                m--;
                k--;
              }
          }
                    }
                            c = c->next;
                    }
                    d = d->next;
            }*/
            for(k=0;k<j;k++)
                    printf("%d ",e[k]);
             printf("\n");
            for (k=0;k<j;k++)
                 Insert_1(a,e[k]);
            //printf("dfsfs\n");
    }
    

    图片说明
    不知道我的这个你满意么

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R