c - 这里有什么错误?删除节点、链表、

标签 c

我写了这段代码,我想知道我的问题是什么,如果你能帮助我修复我的代码而不是编写你自己的代码,这会对我有很大帮助...... 编辑:我已将其更改为此,现在它不会给我运行时错误,但是当我打印名称或检查节点是否存在时,它说它是...

void node_delete(friend *amit) // Deleting a node
{
 friend* temp;
 int flag = 0;
 while (head != NULL) // As long the node isnt the last one
 {
       if (0 == (strcmp(head -> next -> name, amit -> name))) // If the name that the user entered matchs a name in the linked list,
       { // It'll skip it
            temp = head -> next;
             head -> next = head -> next -> next; // Deletes a node from the linked list
             flag = 1;
       }
       if (flag == 1) break;
       head = head -> next; // Going to the next node
}
 free(temp); // Freeing the deleted node
 printf ("%s\n", amit -> name);
}

主要是:

amit.name = "amit"
amit.age = 16
amit.gender = 'm'
node_delete(&amit);

和结构定义:

typedef struct friend // The struct
 {
   char *name; 
   int age;
   char gender; 
   struct friend* next; // A pointer that points to the next node in the linked list
}friend;

非常感谢:)

最佳答案

以下是前几行的一些注释:

 head = amit;
 head -> next = amit;  // same as amit->next = amit;
 amit -> next = NULL;  // same as head->next = null;
 while (head -> next != NULL) // will never do anything since head->next is null

关于c - 这里有什么错误?删除节点、链表、,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10471578/

相关文章:

c - C 中的结构体数组

c - 停留在 while 循环中

c - 警告 : cast to pointer from integer of different size [-Wint-to-pointer-cast]

c - C中静态变量的初始化

c - Paho MQTT C 客户端库中的错误

c - 多维阵列打印给出了不同的东西

c - C 语言高级骰子游戏

c - 为什么这段使用 fork() 的代码可以工作?

c - 使用 memset 初始化浮点型数组

PS3 的 C++ 内存编写