c语言如何删除链表中内容,C语言删除双向链表中数据项程序
创建一个双向链表,并删除此双向链表中的数据项的示例程序,将以下代码保存到一个源文件中:remove_data_from_doubly_linked_list.c, 如下所示 –
#include #include struct node { int data; struct node *prev; struct node *next; }; struct node *head = NULL; struct node *last = NULL; struct node *current = NULL; //display the list void printList() { struct node *ptr = head; printf("n[head] <=>"); //start from the beginning while (ptr != NULL) { printf(" %d <=>", ptr->data); ptr = ptr->next; } printf(" [last]n"); } void print_backward() { struct node *ptr = last; printf("n[last] <=>"); //start from the beginning while (ptr != NULL) { printf(" %d <=>", ptr->data); ptr = ptr->prev; } printf(" [head]n"); } //Create Linked List void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->prev = NULL; link->next = NULL; // If head is empty, create new list if (head == NULL) { head = link; return; } current = head; // move to the end of the list while (current->next != NULL) current = current->next; // Insert link at the end of the list current->next = link; last = link; link->prev = current; } void remove_data(int data) { int pos = 0; struct node *pre_node; if (head == NULL) { printf("Linked List not initialized"); return; } if (head->data == data) { if (head->next != NULL) { head->next->prev = NULL; head = head->next; return; } else { head = NULL; printf("List is empty now"); return; } } else if (head->data != data && head->next == NULL) { printf("%d not found in the listn", data); return; } current = head; while (current->next != NULL && current->data != data) { pre_node = current; current = current->next; } if (current->data == data) { pre_node->next = pre_node->next->next; if (pre_node->next != NULL) { // link back pre_node->next->prev = pre_node; } else last = pre_node; free(current); } else printf("%d not found in the list.", data); } int main() { insert(10); insert(20); insert(30); insert(40); insert(50); insert(60); printList(); remove_data(20); remove_data(50); printList(); print_backward(); return 0; }
执行上面程序,得到以下结果 –
[head] <=> 10 <=> 20 <=> 30 <=> 40 <=> 50 <=> 60 <=> [last] [head] <=> 10 <=> 30 <=> 40 <=> 60 <=> [last] [last] <=> 60 <=> 40 <=> 30 <=> 10 <=> [head]
¥ 我要打赏 纠错/补充 收藏
c语言如何删除链表中内容,C语言删除双向链表中数据项程序相关推荐
- 链表的c语言头文件,链表头文件C语言
因为眼前有个例程要使用链表,索性写了一个链表,纯洁的链表其实写得很少.写了一次,感觉又是受益匪浅. /* linked_list.h -- 链表头文件 */ /* 数据类型定义 */ typedef ...
- 1.删除链表的奇数节点 2.删除链表的偶数节点
1.删除链表的奇数节点 /** 删除奇数节点*/public static ListNode deleteOddNode(ListNode head) {if (head == null)return ...
- c语言用指针删除链表free,leetcode剑指 Offer 18(删除链表的节点)--C语言实现
求: 给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点. 返回删除后的链表的头节点. 注意:此题对比原题有改动 示例 1: 输入: head = [4,5,1,9], val = 5 ...
- Swift - 可编辑表格样例(可直接编辑单元格中内容、移动删除单元格)
(本文代码已升级至Swift3) 本文演示如何制作一个可以编辑单元格内容的表格(UITableView). 1,效果图 (1)默认状态下,表格不可编辑,当点击单元格的时候会弹出提示框显示选中的内容. ...
- python删除链表重复节点_LeetCode-python 82.删除排序链表中的重复元素 II
题目链接 难度: 中等 类型:链表 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例1 输入: 1->2->3->3-> ...
- python删除文件部分内容_Python实现删除文件中含“指定内容”的行示例
本文实例讲述了Python实现删除文件中含指定内容的行.分享给大家供大家参考,具体如下: #!/bin/env python import shutil, sys, os darray = [ &qu ...
- php删除大文件内容,详解在Linux中清空或删除大文件内容的5种方法
有时,在处理Linux终端中的文件时,您可能希望清除文件的内容,而无需使用任何Linux命令行编辑器打开它.怎么能实现这一目标?在本文中,我们将借助一些有用的命令,通过几种不同的方式清空文件内容. 警 ...
- linux删除dat文件内容,使用python删除.dat文件中的行和列
我想知道是否有一种简单的方法可以在python中删除特定的行和列.如果这是一个微不足道的问题,道歉.在 为了提供一些上下文,我目前正在编写一个脚本来自动执行一系列linux命令(特别是ciao-Cha ...
- python删除文件部分内容_如何在python中删除文件的一部分?
基本上你不能从文件的开头删除东西,所以你必须写入一个新文件. 我认为pythonic方式看起来像这样: # get a iterator over the lines in the file: wit ...
最新文章
- kill_mysql_sleep_thread
- 穿越传统藩篱,当统计学闯入人工智能“后花园”
- 京东产品负责人:数据如何高效驱动供应链?
- 如何在Win Server 2008R2环境下,把域帐户加到本地管理员组??
- 讲点码德!避免这些代码坏味道,努力做一名优秀的程序员
- webservice系统学习笔记8-简单的权限校验
- mysql存储图片特征向量_图像特征提取之(一)HOG特征
- 如何正确利用肢体语言表达自己
- 搭建一个属于自己的服务器,并实现内网穿透(外网访问本地服务器功能)
- 创业的成功率只有10%?错,有的人可以高达90%!为什么?
- 视频点播开发者实战:视频水印时间线,防模糊处理
- 【Ruby on Rails全栈课程】3.7 邮件发送(SendCloud、MailGun)
- 利用VUE生成二维码(两种方式)
- ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
- 全息投影是计算机技术吗,全息投影、VR技术、AR增强现实技术的区别
- 音频延时测试方法与实现
- java压缩文件耗时:30秒到1秒的优化过程
- 硬件探索——STM32F4控制74HC595数码管
- week3note函数
- PLATO IDO即将启动,元宇宙盈利效应明显