2个链表的交集

时间:2014-03-20 18:03:17

标签: c linked-list set-intersection

我应该为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));
}

输入

  

输入list 1元素:

     

输入:20

     

继续?(y / n)y

     

输入:30

     

继续?(y / n)y

     

输入:40

     

继续?(y / n)y

     

输入:60

     

继续?(y / n)n

     

输入list 2元素:

     

输入:10

     

继续?(y / n)y

     

输入:30

     

继续?(y / n)y

     

输入:50

     

继续?(y / n)y

     

输入:60

     

继续?(y / n)n

输出

  

list1:20 - &gt; 30 - &gt; 40 - &gt; 60

     

list2:10 - &gt; 30 - &gt; 50 - &gt; 60

     

交叉口:30 - > 60 - > 0

此时的问题

  

没有。交叉点列表中的项目数= 3

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

不是真正的anwer,只是在这里注意。

您的代码几乎无处不在,需要对NULL指针进行一些严格的检查。而且,你太复杂了。例如,您的print()函数可以重写为以下内容:

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

    while (current != NULL) {
        printf("%d\n", current->num);
        current = current->y;
    }
}