I have a linked list where each node stores a word and a number. I can add nodes at the top of the list (push
), at the center of the list (insertAfter
) and at the end of the list (append
). I now added a function to delete nodes, where it will take a char, it will search that char through the list and delete the node that stores that char.
我有一个链表,每个节点存储一个单词和一个数字。我可以在列表顶部(推送),列表中心(insertAfter)和列表末尾(追加)添加节点。我现在添加了一个删除节点的函数,它将获取一个char,它将通过列表搜索该char并删除存储该char的节点。
The problem is that deleteNode
will work with a normal node added at the top of the list, but when i append a node at the end or add it at the middle of the list it won't work.
问题是deleteNode将使用添加在列表顶部的普通节点,但是当我在末尾添加节点或将其添加到列表的中间时,它将无法工作。
Tl;dr deleteNode
works with nodes created with push
but not with nodes created with append
or insertAfter
.
Tl; dr deleteNode与使用push创建的节点一起使用,但不与使用append或insertAfter创建的节点一起使用。
The error i get is segmentation fault
, so i don't have a specific error from the compiler. I'm trying to debug it by running different parts of the code but i still can't find the problem.
我得到的错误是分段错误,所以我没有编译器的特定错误。我试图通过运行代码的不同部分来调试它,但我仍然找不到问题。
struct Node
{
int data;
char *word;
struct Node *next;
};
void push(struct Node** head_ref, int new_data, char *new_word)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->word= malloc(strlen(new_word));
strcpy(new_node->word, new_word);
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
/* Given a node prev_node, insert a new node after the given
prev_node */
void insertAfter(struct Node* prev_node, int new_data, char *new_word)
{
if (prev_node == NULL)
{
printf("the given previous node cannot be NULL");
return;
}
struct Node* new_node =(struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->word= malloc(strlen(new_word));
strcpy(new_node->word, new_word);
new_node->next = prev_node->next;
prev_node->next = new_node;
}
void append(struct Node** head_ref, int new_data, char *new_word)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
struct Node *last = *head_ref;
new_node->data = new_data;
new_node->word= malloc(strlen(new_word));
strcpy(new_node->word, new_word);
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return;
}
void deleteNode(struct Node **head_ref, char *word)
{
struct Node* temp = *head_ref, *prev;
if (strcmp(word, (*head_ref)->word)==0)
{
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
while (strcmp(word, (*head_ref)->word)!=0)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp); // Free memory
}
2 个解决方案
#1
2
This part looks strange:
这部分看起来很奇怪:
while (strcmp(word, (*head_ref)->word)!=0)
{
prev = temp;
temp = temp->next;
}
In the strcmp
you use head_ref
but in the body you update temp
to move to the next element.
在strcmp中你使用head_ref,但在正文中你更新temp以移动到下一个元素。
Did you intend to do:
你有意这样做:
while (strcmp(word, temp->word)!=0)
{
prev = temp;
temp = temp->next;
}
Further there should probably some check for temp
being NULL. Like:
此外,应该检查temp为NULL。喜欢:
while (temp && strcmp(word, temp->word)!=0)
#2
1
Apart from what was stated by @4386427, you are not allocating enough space for your strings there:
除了@ 4386427所说的,你没有为你的字符串分配足够的空间:
new_node->word= malloc(strlen(new_word));
Notice that the C library function size_t strlen(const char *str)
computes the length of the string str up to, but not including the terminating null character. So i would rather suggest:
请注意,C库函数size_t strlen(const char * str)计算字符串str的长度,但不包括终止空字符。所以我宁愿建议:
new_node->word= malloc(strlen(new_word) + 1);
new_node->word[strlen(new_word)] = '\0';
This can cause you some problems with the memory. ;)
这可能会导致内存出现问题。 ;)
Or event better, use calloc
, so the second line will be unnecessary:
或事件更好,使用calloc,所以第二行将是不必要的:
new_node->word= calloc(strlen(new_word) + 1, sizeof(char));
#1
2
This part looks strange:
这部分看起来很奇怪:
while (strcmp(word, (*head_ref)->word)!=0)
{
prev = temp;
temp = temp->next;
}
In the strcmp
you use head_ref
but in the body you update temp
to move to the next element.
在strcmp中你使用head_ref,但在正文中你更新temp以移动到下一个元素。
Did you intend to do:
你有意这样做:
while (strcmp(word, temp->word)!=0)
{
prev = temp;
temp = temp->next;
}
Further there should probably some check for temp
being NULL. Like:
此外,应该检查temp为NULL。喜欢:
while (temp && strcmp(word, temp->word)!=0)
#2
1
Apart from what was stated by @4386427, you are not allocating enough space for your strings there:
除了@ 4386427所说的,你没有为你的字符串分配足够的空间:
new_node->word= malloc(strlen(new_word));
Notice that the C library function size_t strlen(const char *str)
computes the length of the string str up to, but not including the terminating null character. So i would rather suggest:
请注意,C库函数size_t strlen(const char * str)计算字符串str的长度,但不包括终止空字符。所以我宁愿建议:
new_node->word= malloc(strlen(new_word) + 1);
new_node->word[strlen(new_word)] = '\0';
This can cause you some problems with the memory. ;)
这可能会导致内存出现问题。 ;)
Or event better, use calloc
, so the second line will be unnecessary:
或事件更好,使用calloc,所以第二行将是不必要的:
new_node->word= calloc(strlen(new_word) + 1, sizeof(char));