首页 > 解决方案 > prev->next 用于删除在 Visual Studio 中不起作用的节点?在调试器中未显示任何值

问题描述

我正在尝试为链接列表调试我的删除节点函数,但我在 Visual Studio 中收到此错误“-var-create:无法创建变量对象”。当我尝试将 prev->next = current->next 设置为 NULL 时发生错误。

node_t* delete_node(node_t* head) {
    // move through the head
    node_t* current = head;
    node_t* prev = NULL;
    while (current->next != NULL) {
        prev = current;
        current = current->next;
    }
    prev->next = current->next;
    prev = current;

    // To free malloc space
    return current;
}

这是我在 Visual Studio 调试器中看到的内容: 在此处输入图像描述

我错过了什么?谢谢你。

编辑:这是完整的链接列表代码。

//Build the LL
struct node {
    struct node* next;
    char value[];
};

// set existing type, node, to the alias, node_t
typedef struct node node_t;

node_t* tmp1;
// declaring head pointer
node_t* head1 = NULL;


node_t* create_new_node(const char* value)
{
    // create space for node with malloc
    node_t* result = malloc(sizeof(*result) + strlen(value) + 1);
    if (result)
    {
        strcpy(result->value, value);
        result->next = NULL;
    }
    return result;
}

node_t* insert_at_head(node_t** head, node_t* node_to_insert) {

    node_to_insert->next = *head;
    *head = node_to_insert;
    return node_to_insert;
}

node_t* delete_node(node_t* head) {
    // move through the head
    node_t* current = head;
    node_t* prev = NULL;
    while (current->next != NULL) {
        prev = current;
        current = current->next;
    }
    prev->next = current->next;
    prev = current;

    // To free malloc space
    return current;
}

//Get the Length of the Linked List storage
int get_length(node_t* head) {
    node_t* current = head;
    int count = 0;
    while (current != NULL) {
        current = current->next;
        count += 1;
    }
    return count;
}

//Prints linked list
void printlist(node_t* head) {
    node_t* temporary = head;
    int count = 1;
    //Call get length function, if true continue, else call delete node function
    while (get_length > 5) {
        //delete node as space is full
        delete_node(head1);
        // free node here
    }

    while (temporary != NULL) {
        //print out the value of the node that temporary points to

        Log_Debug("%d. %s\n", count, temporary->value);
        // to move along the list
        temporary = temporary->next;
        count++;
    }
    Log_Debug("\n");
    exit(0);
}

标签: cvisual-studiodata-structures

解决方案


谢谢楼上的评论。这是一个简单的错误。get_length() 函数从来没有值。

//Prints linked list
void printlist(node_t* head) {
    node_t* temporary = head;
    int count = 1;
    //Call get length function, if true continue, else call delete node function
    while (get_length(head1) > 5) {
        //delete node as space is full
        delete_node(head1);
        // free node here
    }

    while (temporary != NULL) {
        //print out the value of the node that temporary points to

        Log_Debug("%d. %s\n", count, temporary->value);
        // to move along the list
        temporary = temporary->next;
        count++;
    }
    Log_Debug("\n");
    exit(0);
}

推荐阅读