C -两个链表的交集



我应该为两个链表的交集创建一个链表。我为此编写的代码显示了一个额外的元素->交点列表末尾的'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 = %dn", 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 ->30 ->40 ->60

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

交集:30 ->60 ->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

不是真正的答案,只是这里的一个提示。

你的代码需要对空指针进行严格的检查。而且,你把事情复杂化了。例如,您的print()函数可以重写为如下内容:

void print_list(struct ll *list)
{
    struct ll *current = list;
    while (current != NULL) {
        printf("%dn", current->num);
        current = current->y;
    }
}

相关内容

  • 没有找到相关文章

最新更新