链表结点的删除

文章展示了如何在C语言中删除链表节点,包括当头节点被删除和非头节点被删除的情况。提供了动态链表(使用malloc创建)和静态链表的节点删除函数,并通过示例代码解释了free函数如何释放已删除节点的内存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 注意:::free(p);free函数的使用范围必须是malloc()函数出来的内存空间才能够把这个垃圾内存给free掉。

链表结点的删除分为两种情况:头结点被删除导致头发生改变;其他结点被删除。

静态链表的结点删除:

  1 #include<stdio.h>
  2 
  3 struct test
  4 {
  5    int data;
  6    struct test *next;
  7 };
  8 
  9 void printLink(struct test *head)
 10 {
 11   struct test *point = head;
 12   while(point !=NULL ){
 13       printf("%d ",point->data);
 14       point = point->next;
 15   }
 16   putchar('\n');
 17 }
 18 struct test* deleteNode(struct test *head,int data)
 19 {
 20   struct test *point=head;
 21 
 22   if(head->data==data){
 23      head=head->next;
 24      return head;
 25   }
 26   while(point->next!=NULL){
 27      if(point->next->data==data){
 28         point->next=point->next->next;
 29         return head;
 30      }else{
 31         point=point->next;
 32      }
 33   }
 34   return head;
 35 }
 36 
 37 int main()
 38 {
 39   struct test *head=NULL;
 40   struct test t1 ={1,NULL};
 41   struct test t2 ={2,NULL};
 42   struct test t3 ={3,NULL};
 43   struct test t4 ={4,NULL};
 44   struct test t5 ={5,NULL};
 45 
 46   t1.next =&t2;
 47   t2.next =&t3;
 48   t3.next =&t4;
 49   t4.next =&t5;
 50 
 51   head=&t1;
 52   printLink(head);
 53   head=deleteNode(head,1);
 54   printLink(head);
 55 
 56   head=deleteNode(head,3);
 57   printLink(head);
 58   return 0;
 59 }
 60 

 动态链表结点删除

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 struct test
  5 {
  6    int data;
  7    struct test *next;
  8 };
  9 
 10 void printLink(struct test *head)
 11 {
 12   struct test *point = head;
 13   while(point !=NULL ){
 14       printf("%d ",point->data);
 15       point = point->next;
 16   }
 17   putchar('\n');
 18 }
 19 struct test* deleteNode(struct test *head,int data)
 20 {
 21   struct test *point=head;
 22 
 23   if(head->data==data){
 24      head=point->next;
 25      free(point);        //warning free()weizhi.
 26      return head;
 27   }
 28   while(point->next!=NULL){
 29      if(point->next->data==data){
 30         point->next=point->next->next;
 31         return head;
 32      }else{
 33         point=point->next;
 34      }
 35   }
 36   return head;
 37 }
 38 
 39 int main()
 40 {
 41   struct test *head=(struct test*)malloc(sizeof(struct test));
 42   head->data=1;
 43 
 44   struct test t2 ={2,NULL};
 45   struct test t3 ={3,NULL};
 46   struct test t4 ={4,NULL};
 47   struct test t5 ={5,NULL};
 48 
 49   head->next=&t2;
 50   t2.next =&t3;
 51   t3.next =&t4;
 52   t4.next =&t5;
 53 
 54   printLink(head);
 55   head=deleteNode(head,1);
 56   printLink(head);
 57 
 58   head=deleteNode(head,3);
 59   printLink(head);
 60   return 0;
 61 }
 62 

 

在C++中,我们可以使用结构体定义链表节点,并提供相应的函数来操作链表。以下是基本步骤: 1. **创建链表节点**: ```cpp struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; ``` 2. **创建无序链表**: ```cpp ListNode* createList(vector<int>& nums) { ListNode* head = nullptr; for (int num : nums) { head = insert(head, num); } return head; } // 插入节点函数 ListNode* insert(ListNode* head, int val) { ListNode* newNode = new ListNode(val); if (!head) { head = newNode; } else { ListNode* current = head; while (current->next) { current = current->next; } current->next = newNode; } return head; } ``` 3. **链表结点输出**: ```cpp void printList(ListNode* head) { ListNode* temp = head; while (temp != nullptr) { cout << temp->val << " "; temp = temp->next; } cout << endl; } ``` 4. **升序排序**: ```cpp ListNode* sortList(ListNode* head) { // 使用归并排序等算法链表进行排序 // 这里仅提供思路,实际代码需要实现链表比较和合并操作 } ``` 5. **链表结点插入**: 已经在`createList`函数中包含插入操作。 6. **链表逆序**: ```cpp ListNode* reverseList(ListNode* head) { ListNode* prev = nullptr; ListNode* curr = head; ListNode* nextTemp = nullptr; while (curr) { nextTemp = curr->next; curr->next = prev; prev = curr; curr = nextTemp; } return prev; } ``` 7. **链表拆分**: ```cpp void splitList(ListNode* head) { ListNode* evenHead = nullptr, *evenTail = nullptr; ListNode* oddHead = nullptr, *oddTail = nullptr; ListNode* current = head; while (current) { if (current->val % 2 == 0) { if (!evenHead) { evenHead = current; evenTail = evenHead; } else { evenTail->next = current; evenTail = evenTail->next; } } else { if (!oddHead) { oddHead = current; oddTail = oddHead; } else { oddTail->next = current; oddTail = oddTail->next; } } current = current->next; } if (evenTail) { evenTail->next = nullptr; } if (oddTail) { oddTail->next = nullptr; } } ``` 8. **释放链表**: ```cpp void freeList(ListNode* head) { while (head) { ListNode* temp = head; head = head->next; delete temp; } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值