习题3.4 & 3.5: 求两链表的交集和并集
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position; struct Node{
ElementType Ele;
PtrToNode Next;
}; Position
First( List L )
{
return L->Next;
}
Position
Next( List L, Position p )
{
return p->Next;
} List
CreateAndMakeEmpty( List L )
{
L = malloc( sizeof( struct Node ) );
if( L == NULL )
Error("out of space ");
L->Next = NULL;
return L;
} void
Insert( Position p, ElementType X )
{
Position TmpCell;
TmpCell = malloc( sizeof( struct Node ) );
if( TmpCell == NULL )
Error("out of space ");
TmpCell->Ele = X;
TmpCell->Next = p->Next;
p->Next = TmpCell;
}
List
FindSimilar( List L1, List L2 )
{
Position L1Pos,L2Pos,LresPos;
List Lres;
CreateAndMakeEmpty( Lres );
LresPos = Lres;
L1Pos = First(L1);
L2Pos = First(L2);
while( L1Pos != NULL && L2Pos != NULL )
{
if( L1Pos->Ele > L2Pos->Ele )
next( L2, L2Pos );
else if( L1Pos->Ele < L2Pos->Ele )
next( L1, L1Pos )
else
{
Insert( LresPos, L1Pos->Ele );
LresPos = LresPos->Next;
L1Pos = next( L1, L1Pos );
L2Pos = next( L2, L2Pos );
}
}
}
void
PrintList( List Lres )
{
Position p;
p = First( Lres );
while( p != NULL )
{
printf("%?",p->Ele);
p = p->Next;
}
} List
GetUnion( List L1, List L2 )
{
ElementType InsertEle;
List Lres;
Position L1Pos,L2Pos,LresPos;
L1Pos = First( L1 );
L2Pos = First( L2 );
Lres = CreateAndMakeEmpty( Lres );
LresPos = Lres;
while( L1Pos != NULL && L2Pos != NULL )
{
if( L1Pos->Ele < L2Pos->Ele )
{
InsertEle = L1Pos->Ele;
L1Pos = next( L1, L1pos );
}
else if( L1Pos->Ele > L2Pos->Ele )
{
InsertEle = L2Pos->Ele;
L2Pos = next( L2, L2Pos );
}
else
{
InsertEle = L1Pos->Ele;
L1Pos = next( L1, L1Pos ); L2Pos = next( L2, L2Pos );
}
Insert( LresPos, InsertEle );
LresPos = next( Lres, LresPos );
}
while( L1Pos != NULL )
{
InsertEle = L1Pos->Ele;
Insert( LresPos, InsertEle );
LresPos = next( Lres, LresPos );
L1Pos = next( L1Pos );
}
while( L2Pos != NULL )
{
InsertEle = L2Pos->Ele;
Insert( LresPos, InsertEle );
LresPos = next( Lres, LresPos );
L2Pos = next( L2, L2Pos );
}
printList();
}
习题3.4 & 3.5: 求两链表的交集和并集的更多相关文章
- 【转载】 C#使用Union方法求两个List集合的并集数据
在C#语言的编程开发中,有时候需要对List集合数据进行运算,如对两个List集合进行交集运算或者并集运算,其中针对2个List集合的并集运算,可以使用Union方法来快速实现,Union方法的调用格 ...
- python两个 list 交集,并集,差集的方法+两个tuple比较操作+两个set的交集,并集,差集操作+两个dict的比较操作
转自:http://blog.chinaunix.net/uid-200142-id-3992553.html 有时候,为了需求,需要统计两个 list 之间的交集,并集,差集.查询了一些资料,现在总 ...
- LINUX Shell 下求两个文件交集和差集的办法
http://blog.csdn.net/autofei/article/details/6579320 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d ...
- grep和map计算两个集合交集、并集、补集
#!/usr/bin/perl use strict; ######################################## 用grep 和map 获取两个列表的交集并集.补集###### ...
- 二叉树系列 - 求两节点的最低公共祖先,例 剑指Offer 50
前言 本篇是对二叉树系列中求最低公共祖先类题目的讨论. 题目 对于给定二叉树,输入两个树节点,求它们的最低公共祖先. 思考:这其实并不单单是一道题目,解题的过程中,要先弄清楚这棵二叉树有没有一些特殊的 ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 求n个排序链表的交集
题目大致意思是 给出n个排序list,每个list只有两个方法 (1)bool goNext(); 判断是否有下一个元素,没有元素返回false, 有元素返回true (2)int next(); 返 ...
- 求单链表倒数第m个结点
问题:求单链表倒数第m个结点,要求不准求链表的长度,也不许对链表进行逆转 解:设置两个指针p和q,p.q指向第一个结点.让p先移动到链表的第m个结点,然后p和q同时向后移动,直到p首先到达尾结点.此时 ...
随机推荐
- Gradle方式构建Java多项目
0: 安装IDEA:Linux 上只需下载IDEA的包 并且将路径配置在PATH全局变量中./etc/profile export PATH=$PATH:/opt/Software/IDEA/idea ...
- jQuery的touchstart,touchmove,touchend的获取位置
$('#webchat_scroller').on('touchstart',function(e) { var touch = e.originalEvent.targetTouches[0]; v ...
- 15 Tricks to Appear Smart in Emails(转)
英文原文:15 Tricks to Appear Smart in Emails 如果你不关心在邮件中表现聪明,就不要看本文了. 好吧,我们是孤独的. 在公司环境里,与电子通讯的肥沃土壤相比,没有什么 ...
- LINUX 暂停、继续进程
LINUX 暂停.继续进程 kill -STOP 1234 将该进程暂停. 如果要让它恢复到后台,用kill -CONT 1234 (很多在前台运行的程序这样是不行的) 如果要恢复到前台,请在当时运行 ...
- Opencv关于滑动条bar操作的实例
代码如下: //////////////////////////////////////////////////////////////////////// // // 该程序产生一个窗口10s 如果 ...
- (续)线性表之双向链表(C语言实现)
在前文实现单向链表的基本操作下,本文实现双向链表的基本操作. 双向链表与单链表差异,是双向链表结点中有前向指针和后向指针.所以在插入和删除新结点元素时候不见要考虑后向指针还要考虑前向指针. 以下是双向 ...
- Android解决异常apk on device '0292bea1': Unable to open sync connection!
方式一:使用手机管家(如腾讯手机管家,只要拖动发射火箭就行了)清理一下正在运行的后台程序. 方式二:把USB数据线拔了重新链接. 方法三:找到USB调试,关掉USB调试,然后重新开启.在设置 --&g ...
- Linux系统安装VM-Tools
安装 vmware-tools的安装包有两个,一个是rpm包,一个是tar包,下面分别是用了这两种方法安装: 一.rpm包安装 1.在启动LINUX 虚拟机之后,在WMWare 的菜单栏中点击&quo ...
- URAL 2025. Line Fighting (math)
2025. Line Fighting Time limit: 1.0 second Memory limit: 64 MB Boxing, karate, sambo- The audience i ...
- 想使用WM_CONCAT 函数进行多列转一行,但发现没有
查看数据库版本: SELECT * FROM v$version; 1 Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bi ...