C语言 删除链表末尾,删除循环单链表末尾元素

在循环单链表中删除末尾节点有三种情况。

情况1(链表为空)

如果链表为空,则条件head == NULL将变为true,在这种情况下,只需要在屏幕上打印下溢并退出。

if(head == NULL) { printf("UNDERFLOWn"); return; }

情况2(链表包含单个节点)

如果链表包含单个节点,则条件head->next == head将变为true。 在这种情况下,需要删除整个链表并使头指针空闲。 这将通过使用以下语句来完成。

if(head->next == head) { head = NULL; free(head); }

情况3(链表包含多个元素)

如果链表中有多个节点,那么要删除最后一个节点,需要到达最后一个节点。 还需要跟踪链表的倒数第二个节点。 为此,需要定义两个指针ptr和preptr。 以下代码序列用于此目的。

ptr = head; while(ptr ->next != head) { preptr=ptr; ptr = ptr->next; }

现在,需要再做一次指针调整。需要将指针指向pretr的next指向ptr的next(即head),然后使指针ptr空闲(释放)。

preptr->next = ptr -> next; free(ptr);

算法

第1步:IF HEAD = NULL 提示 “内存溢出” 转到第8步 [IF结束] 第2步:设置PTR = HEAD 第3步:重复第4步和第5步,同时PTR - > NEXT!= HEAD 第4步:SET PREPTR = PTR 第5步:SET PTR = PTR - > NEXT [循环结束] 第6步:设置PREPTR - > NEXT = HEAD 第7步:释放PTR 第8步:退出

示意图

c93b78e2f9f9e204b016f45fb416b45f.png

C语言实现示例代码 –

#include #include void create(int); void last_delete(); struct node { int data; struct node *next; }; struct node *head; void main() { int choice, item; do { printf("1.Append Listn2.Delete Node from endn3.Exitn4.Enter your choice?"); scanf("%d", &choice); switch (choice) { case 1: printf("Enter the itemn"); scanf("%d", &item); create(item); break; case 2: last_delete(); break; case 3: exit(0); break; default: printf("Please Enter valid choicen"); } } while (choice != 3); } void create(int item) { struct node *ptr = (struct node *)malloc(sizeof(struct node)); struct node *temp; if (ptr == NULL) { printf("OVERFLOWn"); } else { ptr->data = item; if (head == NULL) { head = ptr; ptr->next = head; } else { temp = head; while (temp->next != head) temp = temp->next; ptr->next = head; temp->next = ptr; head = ptr; } printf("Node Insertedn"); } } void last_delete() { struct node *ptr, *preptr; if (head == NULL) { printf("UNDERFLOWn"); } else if (head->next == head) { head = NULL; free(head); printf("Node Deletedn"); } else { ptr = head; while (ptr->next != head) { preptr = ptr; ptr = ptr->next; } preptr->next = ptr->next; free(ptr); printf("Node Deletedn"); } }

执行上面示例代码,得到以下结果 –

1.Append List 2.Delete Node from end 3.Exit 4.Enter your choice?1 Enter the item 90 Node Inserted 1.Append List 2.Delete Node from end 3.Exit 4.Enter your choice?2 Node Deleted

¥ 我要打赏   纠错/补充 收藏

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,可以通过以下步骤在链表末尾插入数据: 1. 定义一个结构体来表示链表节点,包含数据和指向下一个节点的指针。 2. 创建一个新的节点,并为其分配内存。 3. 将要插入的数据存储在新节点的数据字段中。 4. 将新节点的指针设置为NULL,表示它是链表的最后一个节点。 5. 如果链表为空,则将新节点设置为链表的头节点。 6. 否则,遍历链表直到找到最后一个节点。 7. 将最后一个节点的指针指向新节点。 8. 插入完成。 以下是一个示例代码,演示了如何在链表末尾插入数据: ```c #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* current = *head; while (current->next != NULL) { current = current->next; } // 将最后一个节点的指针指向新节点 current->next = newNode;} int main() { // 创建一个空链表 struct Node* head = NULL; // 在链表末尾插入数据 insertAtEnd(&head, 10); insertAtEnd(&head, 20); insertAtEnd(&head, 30); // 遍历链表并打印数据 struct Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } return 0; } ``` 输出结果为:10 20 30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值