0

2 つのリンク リストの交差部分にリンク リストを作成することになっています。私が書いたコードは、余分な要素を示しています->交差リストの最後に「0」。

struct ll{
    int num;
    struct ll *y;
    };
typedef struct ll node;

void create(node *list){
        char c;
        printf("input: ");
        scanf("%d", &list -> num);
        printf("continue?(y/n)\t");
        getchar();  c = getchar();
        if(c == 'y'){
            list -> y = (node *)malloc(sizeof(node));
            create(list -> y);  }
        else    list -> y = NULL;
        }

void print(node *list){
    if(list -> y != NULL){
        printf("%d ->", list -> num);
        if(list -> y -> y == NULL)  printf("%d", list -> y -> num);
        print(list -> y);
            }   
    return; 
    }

int pres(node *list, int key){
    if(list -> num == key)  return 1;
    else{
        if(list -> y == NULL)   return 0;
        else    pres(list -> y, key);
        }   
    }

int count(node *list){
    if(list -> y == NULL)   return 1;
    else    return(1+count(list -> y));
    }

gin(node *head1, node *head2, node *inter){
    node *x = head2, *z = inter;
    int n2, i;
    n2 = count(head2);
    for(i = 0; i<n2; i++){
        if(pres(head1, head2 -> num)){
            (inter -> num) = (head2 -> num);
            inter -> y = (node *)malloc(sizeof(node));
            inter = inter -> y; }
        inter -> y = NULL;
        head2 = head2 -> y;
        }
    head2 = x;  inter = z;
}

main(){
    node *head1, *head2, *inter;
    head1 = (node *)malloc(sizeof(node));
    head2 = (node *)malloc(sizeof(node));
    inter = (node *)malloc(sizeof(node));
        printf("enter list 1 elements:\n");
    create(head1);
    printf("\nenter list 2 elements:\n");
    create(head2);
    printf("\nlist1:\t");
    print(head1);
    printf("\nlist2:\t");
    print(head2);
    printf("\nintersection:\t");
    gin(head1, head2, inter);
    print(inter);
    printf("\nno. of items in intersection list = %d\n", count(inter));
}

入力

リスト 1 要素を入力します。

入力: 20

続けますか?(y/n) y

入力: 30

続けますか?(y/n) y

入力: 40

続けますか?(y/n) y

入力: 60

続けますか?(はい/いいえ) n

リスト 2 要素を入力します。

入力: 10

続けますか?(y/n) y

入力: 30

続けますか?(y/n) y

入力: 50

続けますか?(y/n) y

入力: 60

続けますか?(はい/いいえ) n

出力

list1: 20 ->30 ->40 ->60

list2: 10 ->30 ->50 ->60

交点: 30 ->60 ->0

この時点での問題

番号。交差リストの項目数 = 3

4

2 に答える 2

0

本当に答えではなく、ここにメモしてください。

あなたのコードは、どこにでもある NULL ポインタに対していくつかの深刻なチェックを必要とします。また、物事を過度に複雑にしています。たとえば、print()関数を次のように書き換えることができます。

void print_list(struct ll *list)
{
    struct ll *current = list;

    while (current != NULL) {
        printf("%d\n", current->num);
        current = current->y;
    }
}
于 2014-03-20T18:21:55.730 に答える
0

結合リストに空のノードを保持しているようです。交差点を取得したら、空のノードにデータを入力し、新しい空のノードを作成します。

   if(pres(head1, head2 -> num)){
        (inter -> num) = (head2 -> num);  // You are populating the 'empty' node here
        inter -> y = (node *)malloc(sizeof(node)); // Here is where you create the new 'empty' node
        inter = inter -> y; }
    inter -> y = NULL;

これは次のようになります。

   if(pres(head1, head2 -> num)){
        (inter -> y = (node *)malloc(sizeof(node));  // Create new node
        inter = inter -> y; }  // Set new node as current
        (inter -> num) = (head2 -> num);  // Populate new (current) node
        inter -> y = NULL; }// Terminate node
于 2014-03-20T18:14:37.360 に答える