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;

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的单向链表的 C 语言实现: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结构体 struct Node { int data; // 存储数据 struct Node* next; // 指向下一个节点的指针 }; // 创建新节点 struct Node* create_node(int data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = data; new_node->next = NULL; return new_node; } // 在链表尾部插入节点 void append_node(struct Node** head, int data) { struct Node* new_node = create_node(data); if (*head == NULL) { *head = new_node; return; } struct Node* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = new_node; } // 在链表中间插入节点 void insert_node(struct Node** head, int data, int position) { struct Node* new_node = create_node(data); if (position == 1) { new_node->next = *head; *head = new_node; return; } struct Node* temp = *head; for (int i = 1; i < position - 1; i++) { if (temp == NULL) { printf("Invalid position\n"); return; } temp = temp->next; } new_node->next = temp->next; temp->next = new_node; } // 删除节点 void delete_node(struct Node** head, int position) { if (*head == NULL) { printf("List is empty\n"); return; } struct Node* temp = *head; if (position == 1) { *head = temp->next; free(temp); return; } for (int i = 1; i < position - 1; i++) { if (temp == NULL) { printf("Invalid position\n"); return; } temp = temp->next; } if (temp == NULL || temp->next == NULL) { printf("Invalid position\n"); return; } struct Node* next_node = temp->next->next; free(temp->next); temp->next = next_node; } // 打印链表 void print_list(struct Node* head) { struct Node* temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main() { struct Node* head = NULL; append_node(&head, 1); append_node(&head, 2); append_node(&head, 3); append_node(&head, 4); print_list(head); insert_node(&head, 5, 3); print_list(head); delete_node(&head, 2); print_list(head); delete_node(&head, 5); print_list(head); delete_node(&head, 3); print_list(head); return 0; } ``` 这个实现包含了创建节点、在链表尾部插入节点、在链表中间插入节点、删除节点以及打印链表等基本操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值