在自定义 LinkedList 上查找重复节点通过递归引发分段错误

Find repeated Node on a custom LinkedList provokes Segmentation fault by recursion

简介

我正在尝试创建一个 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; //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(node->next);

因为你没有检查 node 的值是否为 NULL。因此,当您到达最后一个节点时,您使用 node->next 调用函数 findRepeated,其中 next 为空。 要解决此问题,您只需像这样检查它:

if (node->next != NULL) {
    findRepeated(node->next);
}