gpt4 book ai didi

c - 应用程序崩溃(两个链表的交集)

转载 作者:太空宇宙 更新时间:2023-11-04 03:52:27 25 4
gpt4 key购买 nike

我应该创建两个链表并将数据添加到该列表并显示两个列表的交集。(两个列表中的公共(public)数据。)

我不知道为什么当我尝试创建第一个列表时它工作正常而当我尝试创建第二个列表时它崩溃了

这里下面的函数做以下事情

  • 创建列表 - 创建两个列表。

  • addnode - 将节点添加到两个列表。

  • intersectionlist - 这显示了两个列表的交集。

  • freed - 释放所有节点。

  • displayed - 显示列表。

代码

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

关于c - 应用程序崩溃(两个链表的交集),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19690932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com