链表实现运行时错误
Runtime error in implementation of Linked list
我的老师给了我一道作业题,其中:-
1) User will input the size of linked list
2) User will input the data which is to be inputted in the linked
list.
3) User will input a "specific data value" which we have to find in
the original Linked list created above.
4) User will input a "new data value" which is to be inserted before
the "specific data value".
示例 1:
Input values:
4 //size of linked list
9 77 12 6 //values of linked list
12 //specific value which we have to find
8 //new value to be inserted before.
Expected Output:
Linked List : ->9->77->8->12->6
示例 2:
Input values:
4 //size of linked list
9 77 12 6 //values of linked list
10 //specific value which we have to find
8 //new value to be inserted before.
Expected Output:
Node not found!
Linked List : ->9->77->12->6
这是我针对上述问题编写的以下代码。
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <stdbool.h>
struct node //Linked list structure.
{
int data;
struct node *next;
};
int main()
{
int number; //Variable to take in number of linked list.
scanf("%d", &number);
struct node *head; //creating first linked list manually.
head = malloc(sizeof(struct node));
scanf("%d", &head -> data);
head -> next = NULL;
struct node *temp; //"temp" will help in traversing linked list.
temp = head;
int i; //counter variable for loop.
for(i = 1; i < number; i++)
{
struct node *fnnode;
fnnode = malloc(sizeof(struct node));
scanf("%d", &fnnode -> data); //taking in rest of the values.
fnnode -> next = NULL;
temp -> next = fnnode;
temp = temp -> next;
}
int specific;
scanf("%d", &specific); // inputting the specific value we have to traverse to in linked list and insert the value.
struct node *temp2; //"temp2" will help in traversing linked list.
temp2 = head;
temp = head;
while(temp -> data != specific)
{
temp2 = temp;
temp = temp -> next;
if(temp == NULL) //if "temp" reaches the end of the linked list without finding the value then:
{
printf("Node not found!\n");
temp = head;
printf("Linked List : "); //printing the original linked list.
while(temp != NULL)
{
printf("->%d", temp -> data); //printing the original linked list.
temp = temp -> next;
}
return 0; // TERMINATING the program here by returning value 0.
}
}
//If data is found then code below will execute.
struct node *fnnode;
fnnode = malloc(sizeof(struct node));
scanf("%d", &fnnode -> data); //Taking in the data which needs to be inserted.
temp2 -> next = fnnode;
fnnode -> next = temp;
temp = head;
printf("Linked List : ");//printing the new linked list.
while(temp != NULL)
{
printf("->%d", temp -> data);
temp = temp -> next;
}
temp = NULL;
free(temp);
temp2 = NULL;
free(temp2);
return 0;
}
无论问题是我收到此错误:
Runtime error.
我的程序有什么错误?
看来问题出在这里了
而不是
temp2 = head;
temp = head;
你应该写
temp2 = NULL;
temp = head;
然后
fnnode -> next = temp;
temp2 == NULL ? ( head = fnnode ) : ( temp2 -> next = fnnode );
还要考虑到这些陈述
temp = NULL;
free(temp);
temp2 = NULL;
free(temp2);
没有意义,即使你将它们重写得更有意义,比如
free(temp);
temp = NULL;
free(temp2);
temp2 = NULL;
您需要删除所有已分配的节点。
例如
while ( head != NULL )
{
struct node *tmp = head;
head = head->next;
free( tmp );
}
我的老师给了我一道作业题,其中:-
1) User will input the size of linked list
2) User will input the data which is to be inputted in the linked list.
3) User will input a "specific data value" which we have to find in the original Linked list created above.
4) User will input a "new data value" which is to be inserted before the "specific data value".
示例 1:
Input values:
4 //size of linked list
9 77 12 6 //values of linked list
12 //specific value which we have to find
8 //new value to be inserted before.
Expected Output:
Linked List : ->9->77->8->12->6
示例 2:
Input values:
4 //size of linked list
9 77 12 6 //values of linked list
10 //specific value which we have to find
8 //new value to be inserted before.
Expected Output:
Node not found!
Linked List : ->9->77->12->6
这是我针对上述问题编写的以下代码。
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <stdbool.h>
struct node //Linked list structure.
{
int data;
struct node *next;
};
int main()
{
int number; //Variable to take in number of linked list.
scanf("%d", &number);
struct node *head; //creating first linked list manually.
head = malloc(sizeof(struct node));
scanf("%d", &head -> data);
head -> next = NULL;
struct node *temp; //"temp" will help in traversing linked list.
temp = head;
int i; //counter variable for loop.
for(i = 1; i < number; i++)
{
struct node *fnnode;
fnnode = malloc(sizeof(struct node));
scanf("%d", &fnnode -> data); //taking in rest of the values.
fnnode -> next = NULL;
temp -> next = fnnode;
temp = temp -> next;
}
int specific;
scanf("%d", &specific); // inputting the specific value we have to traverse to in linked list and insert the value.
struct node *temp2; //"temp2" will help in traversing linked list.
temp2 = head;
temp = head;
while(temp -> data != specific)
{
temp2 = temp;
temp = temp -> next;
if(temp == NULL) //if "temp" reaches the end of the linked list without finding the value then:
{
printf("Node not found!\n");
temp = head;
printf("Linked List : "); //printing the original linked list.
while(temp != NULL)
{
printf("->%d", temp -> data); //printing the original linked list.
temp = temp -> next;
}
return 0; // TERMINATING the program here by returning value 0.
}
}
//If data is found then code below will execute.
struct node *fnnode;
fnnode = malloc(sizeof(struct node));
scanf("%d", &fnnode -> data); //Taking in the data which needs to be inserted.
temp2 -> next = fnnode;
fnnode -> next = temp;
temp = head;
printf("Linked List : ");//printing the new linked list.
while(temp != NULL)
{
printf("->%d", temp -> data);
temp = temp -> next;
}
temp = NULL;
free(temp);
temp2 = NULL;
free(temp2);
return 0;
}
无论问题是我收到此错误:
Runtime error.
我的程序有什么错误?
看来问题出在这里了
而不是
temp2 = head;
temp = head;
你应该写
temp2 = NULL;
temp = head;
然后
fnnode -> next = temp;
temp2 == NULL ? ( head = fnnode ) : ( temp2 -> next = fnnode );
还要考虑到这些陈述
temp = NULL;
free(temp);
temp2 = NULL;
free(temp2);
没有意义,即使你将它们重写得更有意义,比如
free(temp);
temp = NULL;
free(temp2);
temp2 = NULL;
您需要删除所有已分配的节点。
例如
while ( head != NULL )
{
struct node *tmp = head;
head = head->next;
free( tmp );
}