我应该创建两个链接列表并将数据添加到该列表并显示两个列表的交集。(两个列表中的通用数据)。
我不知道为什么当我尝试创建第一个列表时工作正常,而当我尝试创建第二个列表时崩溃
在这里下面的函数做以下事情

  • 创建列表-创建两个列表。
  • addnode-将节点添加到两个列表中。
  • 相交列表-显示两个列表的相交。
  • 已释放-释放所有节点。
  • 显示的
  • -显示两个列表。

  • 和代码
    #include<stdio.h>
    #include<conio.h>
    struct node
    {
        int data;
        struct node *next;
    };
    struct node *list1 = NULL, *list2 = NULL;
    void create_list()
    {
        int ch;
        struct node *tempnode;
        printf("Enter one to create list one or two to create list two\n");
    marker:
        scanf("%d",&ch);
        if(ch != 1 && ch != 2)
        {
            printf("wrong input\n");
            printf("Please enter again\n");
            goto marker;
        }
        if(ch == 1)
        {
            tempnode = (struct node *)malloc(sizeof(struct node));
            printf("sucesss");
            if(tempnode == NULL)
            {
                printf("Memory allocation unsuccessful\n");
                getch();
                exit(1);
            }
            printf("Enter the data to be inserted\n");
            scanf("%d",tempnode->data);
            if(list1 == NULL)
            {
                tempnode->next = NULL;
                list1 = tempnode;
            }
            else
            {
                printf("List one already created\n");
                free(tempnode);
            }
        }
    
        if(ch == 2)
        {
            tempnode = (struct node *)malloc(sizeof(struct node));
            if(tempnode == NULL)
            {
                printf("Memory allocation unsuccessful\n");
                getch();
                exit(1);
            }
            printf("Enter the data to be inserted\n");
            scanf("%d",tempnode->data);
            if(list2 == NULL)
            {
                tempnode->next = NULL;
                list2 = tempnode;
            }
            else
            {
                printf("List two already created\n");
                free(tempnode);
            }
        }
    }
    
    void addnode()
    {
        int ch;
        struct node *tempnode;
        printf("Enter one to add node to list one or two to add node to list two\n");
    marker:
        scanf("%d",&ch);
        if(ch != 1 && ch != 2)
        {
            printf("wrong input\n");
            printf("Please enter again\n");
            goto marker;
        }
        if(ch == 1)
        {
            tempnode = (struct node *)malloc(sizeof(struct node));
            if(tempnode == NULL)
            {
                printf("Memory allocation unsuccessful\n");
                getch();
                exit(1);
            }
            printf("Enter the data to be inserted\n");
            scanf("%d",tempnode->data);
            if(list1 != NULL)
            {
                tempnode->next = list1;
                list1 = tempnode;
            }
            else
            {
                printf("List not created yet please create list\n");
                getch();
                free(tempnode);
            }
        }
    
        if(ch == 2)
        {
            tempnode = (struct node *)malloc(sizeof(struct node));
            if(tempnode == NULL)
            {
                printf("Memory allocation unsuccessful\n");
                getch();
                exit(1);
            }
            printf("Enter the data to be inserted\n");
            scanf("%d",tempnode->data);
            if(list2 != NULL)
            {
                tempnode->next = list2;
                list2 = tempnode;
            }
            else
            {
                printf("List not created yet please create list\n");
                getch();
                free(tempnode);
            }
        }
    
    }
    
    void intersection_list()
    {
        int flag = 0;
        struct node *tempnode1, *tempnode2;
        if((list1 == NULL) || (list2 == NULL))
        {
            printf("One of both the list is empty\n");
        }
        for(tempnode1 = list1; tempnode1 != NULL; tempnode1 = tempnode1->next)
        {
            for(tempnode2 = list2; tempnode2 != NULL; tempnode2 = tempnode2->next)
            {
                if(tempnode1->data == tempnode2->data)
                {
                    if(flag == 0)
                    {
                        printf("The union of list one and list two is\n");
                    }
                    printf("\t%d",tempnode2->data);
                    flag++;
                }
            }
        }
        if(flag == 0)
        {
            printf("There is no same data in both the list\n");
        }
    
    }
    
    void freed()
    {
        int count = 0;
        struct node *tempnode;
        while(list1 != NULL)
        {
            tempnode = list1;
            list1 = list1->next;
            free(tempnode);
            count++;
        }
        printf("%d nodes freed from list 1\n",count);
    
        count = 0;
        while(list2 != NULL)
        {
            tempnode = list2;
            list2 = list2->next;
            free(tempnode);
            count++;
        }
        printf("%d nodes freed from list 2\n",count);
    }
    
    void displayed()
    {
        int ch;
        struct node *tempnode;
        printf("Enter one to display list one and enter two to display list two\n");
    marker:
        scanf("%d",&ch);
        if(ch != 1 && ch != 2)
        {
            printf("wrong input\n");
            printf("Please enter again\n");
            goto marker;
        }
        if(ch == 1)
        {
            if(list1 == NULL)
            {
                printf("Empty list\n");
            }
            else
            {
                printf("The data in list one\n");
                for(tempnode = list1; tempnode != NULL; tempnode = tempnode->next)
                {
                    printf("\t%d\n",tempnode->data);
                }
            }
    
        }
    
        if(ch == 2)
        {
            if(list2 == NULL)
            {
                printf("Empty list\n");
            }
            else
            {
                printf("The data in list two\n");
                for(tempnode = list2; tempnode != NULL; tempnode = tempnode->next)
                {
                    printf("\t%d\n",tempnode->data);
                }
            }
        }
    }
    
    int main()
    {
        int ch;
        do
        {
            printf("Enter the option number for the execution\n1. Create list\n2. Add node\n3. Intersection of list\n4. Display list\n5. Exit\n");
            scanf("%d",&ch);
            if(ch == 1)
            {
                create_list();
            }
            else
            {
                if(ch == 2)
                {
                    addnode();
                }
                else
                {
                    if(ch == 3)
                    {
                        intersection_list();
                    }
                    else
                    {
                        if(ch == 4)
                        {
                            displayed();
                        }
                    }
                }
            }
        } while(ch < 5 && ch > 0);
        freed();
    }
    

    最佳答案

    希望这会有所帮助

    void inter(Node* a, Node* b, struct Linkedlist* res)
    {
        if(a!=NULL&&b!=NULL)
        {
            if(a->data == b->data)
            {
                 nodePushBack(res, a->data);
                 inter(a->next, b->next, res);
            }
            else if(a->data < b->data)
            {
                 inter(a->next, b, res);
            }
            else
            {
                inter(a, b->next, res);
            }
        }
    }
    

    10-04 21:58
    查看更多