delnode c语言代码
时间: 2023-07-30 10:06:11 浏览: 42
我不太清楚您的问题的具体意思,但如果您是想了解如何使用 C 语言编写删除节点(delnode)的代码,可以参考以下示例代码:
```
#include <stdio.h>
#include <stdlib.h>
// 定义一个单链表节点结构体
struct Node {
int data;
struct Node *next;
};
// 在链表尾部插入新节点
void insertAtEnd(struct Node **head, int data) {
struct Node *newNode = (struct Node *) malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node *lastNode = *head;
while (lastNode->next != NULL) {
lastNode = lastNode->next;
}
lastNode->next = newNode;
}
// 删除指定节点
void deleteNode(struct Node **head, int key) {
struct Node *prevNode = NULL;
struct Node *curNode = *head;
if (curNode != NULL && curNode->data == key) {
*head = curNode->next;
free(curNode);
return;
}
while (curNode != NULL && curNode->data != key) {
prevNode = curNode;
curNode = curNode->next;
}
if (curNode == NULL) {
return;
}
prevNode->next = curNode->next;
free(curNode);
}
// 打印链表节点值
void printList(struct Node *node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
struct Node *head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
printf("初始链表:\n");
printList(head);
deleteNode(&head, 3);
printf("删除节点 3 后的链表:\n");
printList(head);
return 0;
}
```
这段代码实现了一个简单的单链表,包括在链表尾部插入新节点、删除指定节点和打印链表节点值等功能。其中,`deleteNode` 函数实现了删除指定节点的功能,通过遍历链表找到要删除的节点并将其前一个节点的 `next` 指针指向要删除节点的下一个节点,最后释放要删除的节点的内存空间。