删除两个双向链表中值相同的结点--带空白头结点
有两个双向链表,空白头结点为:ListA和ListB,要求删除这两个链表中关键字相同的结点, C语言实现,结点结构如下:
- struct node // 双向链表结点
- {
- int key;
- struct node *front, *next;
- };
完整代码:
/*功能:删除两个双向链表(都带空白头结点)中值(key)相同的结点,规定header.front始终指向尾结点名词定义:空白头结点:该结点不作为真正保存值的结点;其front成员规定指向尾结点, next指向第一个非空白结点;当该链表无结点时,规定其front和next都为NULL.头结点: 链表的第一个非空白头结点(第一个元素)尾结点: 链表的最后一个非空白结点
*/#include <stdio.h>
#include <stdlib.h>
#include <string.h>struct node // 双向链表结点
{int key;struct node *front, *next;
};/*功能: 创建双向链表(尾插法:新创建的结点放在链表尾部)返回值:1-创建成功,0-创建失败header: 创建的双向链表的头指针n: 待创建的结点个数
*/
int createLinklist(struct node *header, int n)
{int v;struct node *p = NULL;printf("请输入%d个整数:\n", n);while(n-- > 0){scanf("%d", &v);p = malloc(sizeof(struct node));if(p){if(header->next == NULL)header->next = p; // 设置链表头指针else{header->front->next = p;p->front = header->front;}p->key = v;p->next = header->next; // 新结点的next指向第一个结点header->next->front = p; // 结点的front指向新结点(作为最后一个结点)header->front = p; // header->front始终指向最后一个结点}elsereturn 0; // 创建链表失败}return 1; // 创建链表成功
}// 输出双向链表中的值
void displayLinklist(struct node *header)
{struct node *p = header->next;printf("header.front = 0x%X, header.next = 0x%X\n", header->front, header->next);if(NULL != p){do{printf("[p = 0x%X]\tdata = %d, front = 0x%X, next = 0x%X\n", p, p->key, p->front, p->next);p = p->next;}while(p != header->next);printf("\n");}
}// 删除双向链表中所有结点并释放空间(头删法)
void FreeLinklist(struct node *header)
{struct node *p;while(header->next){p = header->next; // p指向待删结点if(p == header->front) // 待删除的是尾结点{header->front = header->next = NULL;}else{header->front->next = p->next;p->next->front = header->front;header->next = p->next;}free(p);}
}/*功能: 删除双向链表(头指针pHeader)中值与key相同的结点,从结点*pStart开始向后搜索返回值: 如果从双向链表中删除了值为指定key的结点,返回1,否则返回0pHeader: 双向链表空白头指针pStart: 从*pStart开始向后搜索,删除值与key相同的结点,直到遇到pHeader->nextkey: 待删结点关键字注意: 调用此函数时,输入参数pHeader==*pStart,程序将会出错
*/
int removeNode(struct node *pHeader, struct node **pStart, int key)
{struct node *p, *temp;int del = 0;p = *pStart;*pStart = NULL;while(p){if(p->key == key){del = 1;temp = p; // temp指向待删结点if(pHeader->next == p) // 删除头结点{if(pHeader->front == pHeader->next) // 待删结点是链表的唯一结点pHeader->front = pHeader->next = NULL;else{ pHeader->front->next = p->next;p->next->front = pHeader->front; // 尾结点的next指向新的头结点pHeader->next = p->next;}p = pHeader->next; // p指向新的头结点}else{p->front->next = p->next;p->next->front = p->front;if(p == pHeader->front) // 待删除的是尾结点pHeader->front = p->front; // pHeader->front指向新的尾结点p = p->next; // p指向被删除结点的后继结点}free(temp);}else{if(*pStart == NULL)*pStart = p;p = p->next;}if(*pStart && p == pHeader->next)break;}return del;
}// 删除两个链表中值相同的结点
void removeEqualNodes(struct node *pHeadA, struct node *pHeadB)
{struct node *p1, *p2;int del = 0;p1 = pHeadA->next;while(p1){p2 = pHeadB->next;if((del = removeNode(pHeadB, &p2, p1->key)) == 1){removeNode(pHeadA, &p1, p1->key);}elsep1 = p1->next;if(pHeadB->next == NULL || pHeadA->next == NULL || (del == 0 && p1 == pHeadA->next))break;}
}int main(int argc, char *argv[])
{struct node listA, listB; // 定义两个双向链表的空白头结点int n1, n2; // 保存待创建的链表结点个数if(argc < 3){printf("Usage: %s <n1> <n2>\n", argv[0]);return 1;}n1 = atoi(argv[1]);n2 = atoi(argv[2]);listA.front = listA.next = NULL;listB.front = listB.next = NULL;createLinklist(&listA, n1); // 创建双向链表createLinklist(&listB, n2);printf("Before remove:\n");displayLinklist(&listA); // 显示双向链表内容displayLinklist(&listB);removeEqualNodes(&listA, &listB); // 删除两个链表中值相同的结点printf("\nAfter remove:\n");displayLinklist(&listA);displayLinklist(&listB);FreeLinklist(&listA); // 释放空间FreeLinklist(&listB);return 0;
}
运行结果:
E:\Program\VC\del\Debug>del.exe 2 4
请输入2个整数:
14 25
请输入4个整数:
14 25
63 66
Before remove:
header.front = 0x3807E0, header.next = 0x3807A8
[p = 0x3807A8] data = 14, front = 0x3807E0, next = 0x3807E0
[p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x3808C0, header.next = 0x380818
[p = 0x380818] data = 14, front = 0x3808C0, next = 0x380850
[p = 0x380850] data = 25, front = 0x380818, next = 0x380888
[p = 0x380888] data = 63, front = 0x380850, next = 0x3808C0
[p = 0x3808C0] data = 66, front = 0x380888, next = 0x380818After remove:
header.front = 0x0, header.next = 0x0
header.front = 0x3808C0, header.next = 0x380888
[p = 0x380888] data = 63, front = 0x3808C0, next = 0x3808C0
[p = 0x3808C0] data = 66, front = 0x380888, next = 0x380888E:\Program\VC\del\Debug>del.exe 2 4
请输入2个整数:
14 25
请输入4个整数:
12 23 21 54
Before remove:
header.front = 0x3807E0, header.next = 0x3807A8
[p = 0x3807A8] data = 14, front = 0x3807E0, next = 0x3807E0
[p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x3808C0, header.next = 0x380818
[p = 0x380818] data = 12, front = 0x3808C0, next = 0x380850
[p = 0x380850] data = 23, front = 0x380818, next = 0x380888
[p = 0x380888] data = 21, front = 0x380850, next = 0x3808C0
[p = 0x3808C0] data = 54, front = 0x380888, next = 0x380818After remove:
header.front = 0x3807E0, header.next = 0x3807A8
[p = 0x3807A8] data = 14, front = 0x3807E0, next = 0x3807E0
[p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x3808C0, header.next = 0x380818
[p = 0x380818] data = 12, front = 0x3808C0, next = 0x380850
[p = 0x380850] data = 23, front = 0x380818, next = 0x380888
[p = 0x380888] data = 21, front = 0x380850, next = 0x3808C0
[p = 0x3808C0] data = 54, front = 0x380888, next = 0x380818E:\Program\VC\del\Debug>del.exe 0 0
请输入0个整数:
请输入0个整数:
Before remove:
header.front = 0x0, header.next = 0x0
header.front = 0x0, header.next = 0x0After remove:
header.front = 0x0, header.next = 0x0
header.front = 0x0, header.next = 0x0E:\Program\VC\del\Debug>del.exe 0 2
请输入0个整数:
请输入2个整数:
12 54
Before remove:
header.front = 0x0, header.next = 0x0
header.front = 0x3807E0, header.next = 0x3807A8
[p = 0x3807A8] data = 12, front = 0x3807E0, next = 0x3807E0
[p = 0x3807E0] data = 54, front = 0x3807A8, next = 0x3807A8After remove:
header.front = 0x0, header.next = 0x0
header.front = 0x3807E0, header.next = 0x3807A8
[p = 0x3807A8] data = 12, front = 0x3807E0, next = 0x3807E0
[p = 0x3807E0] data = 54, front = 0x3807A8, next = 0x3807A8E:\Program\VC\del\Debug>del.exe 2 0
请输入2个整数:
14 25
请输入0个整数:
Before remove:
header.front = 0x3807E0, header.next = 0x3807A8
[p = 0x3807A8] data = 14, front = 0x3807E0, next = 0x3807E0
[p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x0, header.next = 0x0After remove:
header.front = 0x3807E0, header.next = 0x3807A8
[p = 0x3807A8] data = 14, front = 0x3807E0, next = 0x3807E0
[p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x0, header.next = 0x0E:\Program\VC\del\Debug>del.exe 1 1
请输入1个整数:
25 25
请输入1个整数:
Before remove:
header.front = 0x3807A8, header.next = 0x3807A8
[p = 0x3807A8] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x3807E0, header.next = 0x3807E0
[p = 0x3807E0] data = 25, front = 0x3807E0, next = 0x3807E0After remove:
header.front = 0x0, header.next = 0x0
header.front = 0x0, header.next = 0x0E:\Program\VC\del\Debug>del.exe 5 6
请输入5个整数:
14 25 63 47 58 14
请输入6个整数:
25 36 47 55 25
Before remove:
header.front = 0x380888, header.next = 0x3807A8
[p = 0x3807A8] data = 14, front = 0x380888, next = 0x3807E0
[p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x380818
[p = 0x380818] data = 63, front = 0x3807E0, next = 0x380850
[p = 0x380850] data = 47, front = 0x380818, next = 0x380888
[p = 0x380888] data = 58, front = 0x380850, next = 0x3807A8header.front = 0x3809D8, header.next = 0x3808C0
[p = 0x3808C0] data = 14, front = 0x3809D8, next = 0x3808F8
[p = 0x3808F8] data = 25, front = 0x3808C0, next = 0x380930
[p = 0x380930] data = 36, front = 0x3808F8, next = 0x380968
[p = 0x380968] data = 47, front = 0x380930, next = 0x3809A0
[p = 0x3809A0] data = 55, front = 0x380968, next = 0x3809D8
[p = 0x3809D8] data = 25, front = 0x3809A0, next = 0x3808C0After remove:
header.front = 0x380888, header.next = 0x380818
[p = 0x380818] data = 63, front = 0x380888, next = 0x380888
[p = 0x380888] data = 58, front = 0x380818, next = 0x380818header.front = 0x3809A0, header.next = 0x380930
[p = 0x380930] data = 36, front = 0x3809A0, next = 0x3809A0
[p = 0x3809A0] data = 55, front = 0x380930, next = 0x380930E:\Program\VC\del\Debug>
删除两个双向链表中值相同的结点--带空白头结点相关推荐
- 删除两个双向链表中值相同的结点--无空白头结点
有两个双向链表,头指针为:pListA和pListB,要求删除这两个链表中值相同的结点, C语言实现,结点结构如下: struct node // 双向链表结点 {int key;struct nod ...
- php两个数组去掉相同的,php如何删除两个数组中相同的数据
php如何删除两个数组中相同的数据 发布时间:2021-03-12 10:27:42 来源:亿速云 阅读:77 作者:小新 这篇文章给大家分享的是有关php如何删除两个数组中相同的数据的内容.小编觉得 ...
- js删除两个集合中共同元素_多个集合中的共同和独特元素
js删除两个集合中共同元素 本周,我们将暂时中断较高级别的问题和技术文章,以解决我们中许多人可能面临的一些代码问题. 没什么花哨的或太辛苦的,但是有一天它可能会节省您15分钟的时间,偶尔回到基础上也很 ...
- JS删除两个数组中相同的某个对象值
var arr1=[{id:1},{id:2},{id:3},{id:4},{id:5}] var arr2=[{id:1},{id:2},{id:3}] var set=arr2.map(item= ...
- 删除无序单链表中值重复出现的节点
题目: 给定一个无序单链表的头节点head,删除其中值重复出现的节点. 请按以下要求实现两种方法. 方法一.时间复杂度O(N) 方法二.空间复杂度O(1) 基本思路 方法一.利用哈希表,依次遍历每 ...
- 链表问题13——删除无序单链表中值重复出现的节点
题目 给定一个无序单链表的头节点head,删除其中值重复出现的节点. 原链表 删除后 1->2->3->3->4->2->4->1 1->2->3 ...
- vue 删除两个集合中相同的数据_vue.js如何删除数组里面的数据
vue.js如何删除数组里面的数据 发布时间:2020-12-10 12:04:44 来源:亿速云 阅读:107 作者:小新 这篇文章主要介绍了vue.js如何删除数组里面的数据,具有一定借鉴价值,需 ...
- mysql 删除另一个表中的_mysql – 在一个查询SQL中删除两个表中的记录
我有两张桌子 EMPGROUP_TBL SEQID | MASTERID | BUSINESS_UNIT | DIVISION | SUB_DIVISION | CLASSIFICATION | SU ...
- 删除两个字符串中的特定字符使其相等,统计删除的字符的ASCII和
为什么80%的码农都做不了架构师?>>> Minimum ASCII Delete Sum for Two Strings 问题: Given two strings s1, ...
最新文章
- 36.intellij idea 如何一键清除所有断点
- Keras图像分割实战:数据整理分割、自定义数据生成器、模型训练
- css 定位连线_CSS Position(定位)
- 阿里云安全送您六道平安符,恭贺新春!
- 棋牌游戏服务器架构: 详细设计(三) 数据库设计
- 新鲜高频笔面试题分享,Redis、MongoDB、ElasticSearch...
- jdk 9和jdk8_JDK 9已完成功能!
- 字典按照值或键进行排序
- android eclipse自动更新,Android Eclipse 升级ADT到24.0.2完美解决方案
- 小学生c语言入门教程,啊哈C语言(小学生坐在马桶上都能看懂C语言入门教程).pdf...
- C语言编程之学习putpixel画点
- 【ANSYS命令流】模型的建立相关命令(一)
- 微信支付 发起商家转账API 2022年v3 transfer batches
- Excel 对象模型
- 打印机出现黄色感叹号!无法查看属性和设置,开机查看打印机,打印自动变灰色问题无法使用!
- 《微机原理第五版》期末知识总结(第八章---第十章)
- pb11.5的使用体会
- 解析VCARD文件(vcf格式)导入QQ通讯录功能
- Qt编程之Xml文件的读取
- mac下载的java是zip_【7-Zip和java For Mac哪个好用】7-Zip和java For Mac对比-ZOL下载
热门文章
- 大数据数据科学家常用面试题_进行数据科学工作面试
- 2048. 下一个更大的数值平衡数
- leetcode392. 判断子序列(动态规划)
- lambda 使用_如何使用Lambda和API网关构建API
- yeoman_使用yeoman轻松创建Yeoman生成器
- seaborn分布数据可视化:直方图|密度图|散点图
- 前端面试常见逻辑题收集及分析
- BOS12——多对多添加方法,多对多页面需要字段问题(不多的话直接提供get方法),修改Realm中授权方法(查询数据库),缓存Java对象的方法,加载左侧菜单(ztree提供pId)...
- ztree 获取当前选中节点的子节点集合
- hadoop中unhealthynodes的问题解决