c 语言单向链表的删除

按照序号删除单链表中的结点

void deletenode(struct listnode* head, int n) {

int i = 1;

struct listnode* fast = head->next;

struct listnode* slow = head;

while (fast != NULL && i < n) {

slow = slow->next;

fast = fast->next;

i++;

}

if (fast == NULL) {

printf("妹找着");

return;

}

slow->next = fast->nezt;

free(fast);

}







删除单链表中的第一个key值

node* fast = head->next;

node* slow = head;

while (fast != NULL && fast->val != key) {

slow = slow->next;

fast = fast->next;

}

if (fast == NULL) {

printf("没找到值为%d的点",key);

return;

}

slow->nest = fast->next;

free(fast);





删除单链表中值为key的所有结点

node* fast = head->next;

node* slow = head;

while (fast != NULL) {

if (fast->val == key) {

slow->next = fast->next;

free(fast);

fast = slow->next;

}

else {

slow = slow->next;

fast = fast->next;

}

}





删除单链表中所有重复的节点

node* p = head->next;

node* fast,*slow;

while (p != NULL) {

slow = p;

fast = p->next;

while (fast!= NULL) {

if (p->val == fast->val) {

slow->next = fast->next;

free(fast);

fast = slow->next;

}

else {

slow = slow->next;

fast = fast->next;

}

}

p = p->next;

}

在 C 语言中,单向链表是一种常见的数据结构,尾部删除操作涉及查找倒数第二个节点,并将其 `next` 指针置为 `NULL`,随后释放最后一个节点的内存。这一过程需要对链表进行遍历,以找到目标节点。 以下是一个完整的实现示例: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct Node { int data; struct Node* next; } Node; // 删除链表尾节点的函数 Node* delete_at_list_tail(Node* head) { if (head == NULL) { return NULL; } if (head->next == NULL) { free(head); return NULL; } Node* tmp_p = head; while (tmp_p->next->next != NULL) { tmp_p = tmp_p->next; } free(tmp_p->next); tmp_p->next = NULL; return head; } ``` 上述代码中,函数 `delete_at_list_tail` 首先检查链表是否为空或仅包含一个节点。如果链表只有一个节点,则直接释放该节点并返回 `NULL`。否则,通过遍历找到倒数第二个节点,并将它的 `next` 设置为 `NULL`,同时释放最后一个节点的内存[^1]。 为了验证功能的正确性,可以在主函数中创建一个简单的链表并调用该函数: ```c int main() { // 创建链表头节点 Node* head = (Node*)malloc(sizeof(Node)); head->data = 1; head->next = NULL; // 添加一些节点 Node* current = head; for (int i = 2; i <= 5; i++) { current->next = (Node*)malloc(sizeof(Node)); current = current->next; current->data = i; current->next = NULL; } // 打印原始链表 printf("Original list:\n"); current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); // 删除尾节点 head = delete_at_list_tail(head); // 打印修改后的链表 printf("Modified list:\n"); current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); // 释放剩余节点 current = head; while (current != NULL) { Node* temp = current; current = current->next; free(temp); } return 0; } ``` 在上述测试程序中,首先创建了一个包含五个节点的链表,然后调用 `delete_at_list_tail` 函数删除尾节点,并打印出修改后的链表内容。最后,程序会释放所有剩余节点的内存。 ### 相关问题 1. 如何在 C 语言中实现单向链表的头部删除操作? 2. 单向链表中如何高效地查找某个特定节点? 3. 在 C 语言中,如何处理单向链表中的内存泄漏问题? 4. 单向链表的尾部插入操作如何实现? 5. 如何判断一个单向链表是否为空?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值