c - 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;
}
我错过了什么?谢谢你。
编辑:这是完整的链接列表代码。
//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);
}
解决方案
谢谢楼上的评论。这是一个简单的错误。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);
}
推荐阅读
- java - 为什么 Object 类有方法 wait() 和 notify() 而 Class 类没有?
- visual-studio - Visual Studio 2015 IIS Express 不加载 CSS、JS、图像等资产
- javascript - Gulp:调用一个异步函数,该函数在转换函数中提供自己的回调
- kubernetes - 服务(NodePort)间歇性超时
- java - Websphere 9:Workmanager 中的 Jax-WS 调用第二次失败
- python - Python字符串正则表达式联合返回一堆空字符串
- django - 单击 django 博客卡中的阅读更多按钮后,尝试在新的 html 页面上显示完整的博客
- azure-cli - 使用 Azure CLI 和 JMESPath 进行筛选
- mongodb - mongodb的安装破坏了我的包管理器
- mysql - Mysql嵌套并声明为数组