习题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首先到达尾结点.此时 ...
随机推荐
- python 数据类型之数值型
1.在python在数值的类型大概包括int float complex decimal 1.int 在3.x中它不在有最大值的限制. 2.float它更加连近于C语言中的double 3.compl ...
- vi全局替换方法
来自:http://blog.sina.com.cn/s/blog_736f1c59010136ry.html 1. 基本的替换 :s/vivian/sky/ 替换当前行第一个 vivian 为 sk ...
- SQL Server中各个系统表的作用
sysaltfiles 主数据库 保存数据库的文件 syscharsets 主数据库 字符集与排序顺序 s ...
- ID3决策树算法原理及C++实现(其中代码转自别人的博客)
分类是数据挖掘中十分重要的组成部分.分类作为一种无监督学习方式被广泛的使用. 之前关于"数据挖掘中十大经典算法"中,基于ID3核心思想的分类算法C4.5榜上有名.所以不难看出ID3 ...
- poj 1149 PIGS_网络流
#include<iostream> #include<queue> #include<cstdio> #include<cstring> using ...
- BLOB存储图片文件二进制数据是非对错
子在一天一天虚度,生活也在一天一天中茫然 做人做事哪能尽如人意,付出多少收获多少虽然存在偏颇,但是不劳而获的心态是万万不对的,更不能去怨天尤人,低调为人.做好自己就可以了 改进你的系统的最好的方法是先 ...
- Hibernate + MySQL中文乱码问题
如果持久化的类中有包括了汉字的String对象,那么对应到数据库中汉字的部分就会是乱码.这主要是由于MySQL数据表的字符集与我们当前使用的本地字符集不相同造成的. 如果是windows系统,那么系统 ...
- eclipse开发c++时cout和endl报错
1.Symbol 'cout' could not be resolved , 选择kepler版本号就不报错了. 2. 下载gdb, ./configure make sudo make inst ...
- 02安卓用户界面优化之(二)SlidingMenu使用方法
一.SlidingMenu配置方法 1.下载SlidingMenu:https://github.com/jfeinstein10/SlidingMenu 2.拷贝SlidingMenu-master ...
- JQ第一篇
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...