C 在自定义LinkedList上查找重复节点会通过递归引发分段错误
导言 我试图创建一个LinkedList,其中包含存储值和指向下一个值的指针的节点 我是怎么做的? 我使用这个函数C 在自定义LinkedList上查找重复节点会通过递归引发分段错误,c,linked-list,segmentation-fault,C,Linked List,Segmentation Fault,导言 我试图创建一个LinkedList,其中包含存储值和指向下一个值的指针的节点 我是怎么做的? 我使用这个函数 /* Finds if two Nodes contain the same value */ void findRepeated(Node *node) { Node *nodeList = node; //node List int currentValue; //stores the value of nodeList int nextValue; //stor
/*
Finds if two Nodes contain the same value
*/
void findRepeated(Node *node) {
Node *nodeList = node; //node List
int currentValue; //stores the value of nodeList
int nextValue; //stores value from nextNode
//Skip the current value
currentValue = nodeList->value; //Current value of the List
nodeList = nodeList->next; //Next node
//Keep going till the end of the list (NULL)
while(nodeList != NULL) {
nextValue = nodeList->value; //Get the next value from the Nodelist
printf("Testing %d with %d\n", currentValue, nextValue);
//Check if currentValue equals nextValue
if (currentValue == nextValue) {
printf("%d is repeated in the List\n", currentValue);
return; //Exit function
}
//Goto next node in List
nodeList = nodeList->next; //Next node
}
findRepeated(node->next); //Recursion
}
它使用递归的魔力来找到它,并且使用这个主代码做得很好
insertNode(&head, 0);
insertNode(&head, 1);
insertNode(&head, 2);
insertNode(&head, 3);
insertNode(&head, 4);
insertNode(&head, 0);
findRepeated(head);
//printList(head);
但是当我试图在do-while循环中“自动化”它时,一个分段错误发生了,我无法解释为什么
完整代码
//This is an element obj for the List
typedef struct Node {
int value; //Value of the node
struct Node *next; //Next element in List pointer
} Node;
/*
Insert a node into the List
*/
void insertNode(struct Node** head, int value) {
//Allocate memory to new Node
struct Node* new_node = (Node*)malloc(sizeof(Node));
//Add the data to the new node
new_node->value = value; //The value
new_node->next = *head; //The pointer to next Node
(*head) = new_node;
}
/*
Prints all the values stored in the List
*/
void printList(Node *node) {
//'till we don't find a NULL (end point) we keep going
while (node != NULL){
printf(" %d ", node->value); //Print the value of the node
node = node->next; //Move the pointer to the next, till you find a NULL
}
}
/*
Finds if two Nodes contain the same value
*/
void findRepeated(Node *node) {
Node *nodeList = node; //node List
int currentValue; //stores the value of nodeList
int nextValue; //stores value from nextNode
//Skip the current value
currentValue = nodeList->value; //Current value of the List
nodeList = nodeList->next; //Next node
//Keep going till the end of the list (NULL)
while(nodeList != NULL) {
nextValue = nodeList->value; //Get the next value from the Nodelist
printf("Testing %d with %d\n", currentValue, nextValue);
//Check if currentValue equals nextValue
if (currentValue == nextValue) {
printf("%d is repeated in the List\n", currentValue);
return; //Exit function
}
//Goto next node in List
nodeList = nodeList->next; //Next node
}
findRepeated(node->next); //Recursion
}
//Function definitions goes above their calls
int main()
{
int d;
//Create empty List
struct Node* head = NULL;
insertNode(&head, 0);
do {
printf("Input an Integer: ");
scanf(" %d", &d);
printf("Input was: %d\n", d);
insertNode(&head, d);
printf("Input done. Printing list...\n");
printList(head);
printf("Fetching repeated...\n");
findRepeated(head); //segmentation fault...
} while (1 == 1);
//Insert some nodes
/*
insertNode(&head, 0);
insertNode(&head, 1);
insertNode(&head, 2);
insertNode(&head, 3);
insertNode(&head, 4);
insertNode(&head, 5);
findRepeated(head);
//printList(head);
*/
}
我想知道它发生的原因以及如何修复它。
findRepeated
函数在调用递归的行上崩溃:
findRepeated(节点->下一步);
因为您不检查节点的值是否为NULL
。因此,当您到达最后一个节点时,可以使用node->next
调用函数findRepeated
,其中next
为空。
要解决此问题,您只需按如下方式进行检查:
if(节点->下一步!=NULL){
findRepeated(节点->下一步);
}
Btw,如果我想使用字符串而不是整数。我真的需要改变太多吗?