删除两个双向链表中值相同的结点--带空白头结点

有两个双向链表,空白头结点为:ListA和ListB,要求删除这两个链表中关键字相同的结点, C语言实现,结点结构如下:

view plain
  1. struct node                             // 双向链表结点  
  2. {  
  3.     int key;  
  4.     struct node *front, *next;  
  5. };  

 完整代码:

/*功能:删除两个双向链表(都带空白头结点)中值(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>


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/406871.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

寻求空间支持

寻求支持.NET Framework 3.5以及SQL Server的空间&#xff0c;最好有本地磁盘读写权限&#xff08;特定数据目录&#xff09;。

判断浏览器版本语句大全

<!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> <!--[if IE]> 所有的IE可识别 <![endif]--> <!--[if IE 5.0]> 只有IE5.0可以识别 <![endif]--> <!--[if IE 5]> 仅IE5.0与IE5.5可以识别 <![endif]--> <!--[…

微信小程序API~GET

框架提供丰富的微信原生API&#xff0c;可以方便的调起微信提供的能力&#xff0c;如获取用户信息&#xff0c;本地存储&#xff0c;支付功能等 1. wx.on 开头的 API 是监听某个事件发生的API接口&#xff0c;接受一个 CALLBACK 函数作为参数。当该事件触发时&#xff0c;会调…

如何用DOS命令批量删除文件?(_desktop.ini 或 thumbs.db)

如何用DOS命令批量删除文件&#xff1f;比如viking蠕虫病毒会在系统里产生大量的“_desktop.ini”文件&#xff0c;虽然杀毒后系统无问题了&#xff0c;但看着总归不爽。我们可使用DOS命令批量删除“_desktop.ini”。 点击“开始”→“运行”&#xff0c;输入&#xff1a;“CMD…

Vue CLI3 开启gzip压缩

gizp压缩是一种http请求优化方式&#xff0c;通过减少文件体积来提高加载速度。html、js、css文件甚至json数据都可以用它压缩&#xff0c;可以减小60%以上的体积。 webpack在打包时可以借助 compression webpack plugin 实现gzip压缩&#xff0c;首先需要安装该插件&#xff…

让Sql Server也能出现如VS一样的智能提示工具--资源更新

一个非常实用的小插件&#xff0c;适合Sql Server2005以上版本&#xff0c;免费、无须注册。下面的图片是效果&#xff1a; http://rzhh.com.cn/down/sqlassc.rar 刚刚资源出错了&#xff0c;现在已经更新了资源&#xff0c;请大家下载。 转载于:https://www.cnblogs.com/zhang…

据库中事务、会话、线程这几个概念是什么关系

1、会话可以创建多个事务比如&#xff1a;使用客端连接数据库&#xff0c;这样你就可以执行很多个事务了2、一个事务只能由一个会话产生在数据库里的事务&#xff0c;如果在执行的SQL都是由会话发起的&#xff0c;哪怕是自动执行的JOB也是由系统会话发起的3、一个事务可能会产生…

什么是虚拟化,虚拟化的现状

什么事虚拟化 虚拟化是一个比较广义的概念&#xff0c;在计算机方面通常是指计算元件在虚拟的基础上而不是真实的基础上运行&#xff1b;也可以说是一种解决方案&#xff0c;为了简化管理&#xff0c;最大化利用现有资源。例如&#xff1a;现有有一台服务器&#xff0c;平…

Wireshark数据包分析(一)——使用入门

Wireshark简介&#xff1a; Wireshark是一款最流行和强大的开源数据包抓包与分析工具&#xff0c;没有之一。在SecTools安全社区里颇受欢迎&#xff0c;曾一度超越Metasploit、Nessus、Aircrack-ng等强悍工具。该软件在网络安全与取证分析中起到了很大作用&#xff0c;作为一款…

给控件做数字签名

参考&#xff1a; http://babyt.cnblogs.com/archive/2005/03/14/118302.html http://babyt.cnblogs.com/archive/2005/03/14/118309.html 在控件(.ocx)开发完成后&#xff0c;一般需要将其打成cab包并发布出去&#xff0c;这样当用户的机器上无此控件时&#xff0c;浏览器…

不使用任何路由协议使3台路由器通信

R1---------R2---------R3R2上什么都不能做。。。3台路由不能做任何的路由协议。。让R1和R3通信。。。。。配置如下&#xff1a;路由器0配置&#xff1a;enconf thost R1int s0/0ip add 192.168.1.1 255.255.255.0no shexitip default-gateway 192.168.1.2&#xff08;默认网关…

[转]根据时间(NSDate)获取具体的信息:月份、星期、天等

[iPhone类代码]计算时间&#xff0c;month,week and day,hour , minutes and second //initializtion parameterNSCalendar *calendar [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];NSDate *now;NSDateComponents *comps [[NSDateComponents alloc]…

微信小程序 禁止弹框下面的内容滑动 弹窗禁止底部内容滚动

我们需要解决的问题&#xff1a;当弹框显示的时候&#xff0c;弹框下面的内容不能滚动 小程序的弹框特别多,像下面这样&#xff0c;弹框的底部是一个可以下拉的页面&#xff0c;但是当弹框出现的时候&#xff0c;需要禁止底部的滚动&#xff0c;那么怎么做呢&#xff1f; 解决…

[转]给控件做数字签名

From: http://www.mzwu.com/article.asp?id1118 我做了一个WebRtf的控件&#xff0c;主要是封装了RichtextBox控件&#xff0c;增加一些方法&#xff0c;如打印等&#xff0c;用来实现在Web上显示RTF文档的功能。 编译成WebRtf.ocx&#xff0c;在VB中测试成功&#xff0c;为了…

Mybatis使用详解

简介 MyBatis是一款优秀的持久层框架&#xff0c;它支持普通SQL查询&#xff0c;存储过程和高级映射。MyBatis通过简单的XML或注解用于配置和原始映射&#xff0c;将接口和Java的POJOs&#xff08;Plain Ordinary Java Object&#xff0c;普通的Java对象&#xff09;映射成数据…