0

i'm supposed to create a linked list for the intersection of 2 linked lists. the code i wrote for that shows an extra element -> '0' at the end of the intersection list.

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

input

enter list 1 elements:

input: 20

continue?(y/n) y

input: 30

continue?(y/n) y

input: 40

continue?(y/n) y

input: 60

continue?(y/n) n

enter list 2 elements:

input: 10

continue?(y/n) y

input: 30

continue?(y/n) y

input: 50

continue?(y/n) y

input: 60

continue?(y/n) n

output

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

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

intersection: 30 ->60 ->0

problem at this point

no. of items in intersection list = 3

Community
  • 1
  • 1
user3248186
  • 1,518
  • 4
  • 21
  • 34
  • Please read this, http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and then come back and ask a *specific* question about your code. – Eric Lippert Mar 20 '14 at 21:39

2 Answers2

0

It looks like you are keeping an empty node in your combined list. When you get an intersection, you populate the empty node, and create a new empty node.

   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;

What this should be is:

   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
Velox
  • 254
  • 1
  • 5
0

Not really an anwer, just a note here.

Your code needs some serious checks for NULL pointers pretty much everywhere. Also, you're overcomplicating things. For instance, your print() function could be rewritten to something like this:

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

    while (current != NULL) {
        printf("%d\n", current->num);
        current = current->y;
    }
}
Asblarf
  • 483
  • 1
  • 4
  • 14