【发布时间】:2013-10-30 18:23:57
【问题描述】:
我应该创建两个链表并将数据添加到该列表并显示两个列表的交集。(两个列表中的公共数据。)
我不知道为什么当我尝试创建第一个列表时它工作正常,而当我尝试创建第二个列表时它崩溃
这里下面的函数做下面的事情
- 创建列表 - 创建两个列表。
- addnode - 将节点添加到两个列表中。
- intersectionlist - 这显示了两个列表的交集。
- 已释放 - 释放所有节点。
- 已显示 - 显示两个列表。
还有代码
#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();
}
【问题讨论】:
-
我看到的第一件事:
goto marker; -
scanf("%d", &tempnode->data) -
fvalcin 有什么问题我没听明白请解释一下 agbinfo tempnode 是一个指针,所以 & 不是必需的,对吗?我只是编程新手。
-
@SaranSankaran 使用 goto 实现此功能是零理由的,在您依赖
scanf()之类的功能之前,您需要了解它们的工作原理。这段代码中的错误列表很重要,但有些事情绝对是核心。例如:您省略了检查您正在调用的 api 的结果。例如:scanf()return 是什么,在这个程序中如何在功能上使用它?最后,要求某人调试 270 行代码墙是相当假定的。 你有没有努力调试这个?如果有,你发现了什么?如果不是,为什么不呢? -
... 继续。顺便说一句,这并不全是坏事。你是指针工作,特别是对于初学者来说,一点也不差。在一些地方粗略,但仍然比许多刚开始学习指针的地方好。这是一个陡峭的学习障碍,你似乎做得很好。
标签: c crash linked-list intersection