程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 鏈表-請大神幫我調下我這程序bug

鏈表-請大神幫我調下我這程序bug

編輯:編程解疑
請大神幫我調下我這程序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");
}



最佳回答:


 #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");
}

圖片說明
不知道我的這個你滿意麼

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved