习题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首先到达尾结点.此时 ...
随机推荐
- A + B Problem II---hdu1002
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- CompressFilterAttribute 文件压缩特性
/// <summary> /// 文件压缩特性 /// </summary> public class CompressFilterAttribute : ActionFil ...
- lastcomm搜索并显示以前执行过的命令信息
lastcomm搜索并显示以前执行过的命令信息
- magent编译安装及常见错误
安装magent到/usr/local/下 cd /usr/local mkdir magent cd magent/ wget http://memagent.googlecode.com/file ...
- ORACLE告警日志
告警日志介绍 告警日志文件是一类特殊的跟踪文件(trace file).告警日志文件命名一般为alert_<SID>.log,其中SID为ORACLE数据库实例名称.数据库告警日志是按时间 ...
- 【MFC学习笔记-作业6-sin图像】【OnDraw(CDC* pDC)】
根据这段源代码 一步一步剖析CDC的使用 void CDrawSinXView::OnDraw(CDC* pDC) { CDrawSinXDoc* pDoc = GetDocument(); ASSE ...
- JqueryUI-2
本文在于巩固基础 jQuery UI Widgets jQuery UI Widgets控件-Accordion <!DOCTYPE html> <html> <head ...
- 《JavaScript 闯关记》之初探
当学习一门新的编程语言的时候,应该边学边做,反复演练以加深理解.因此,你需要一个 JavaScript 解释器.幸运的是,每一个 Web 浏览器都包含一个 JavaScript 解释器. 可以通过在 ...
- 关于js对象值的传递
结合红宝书和网上的一些文章,记录下自己对关于js对象的值的传递的一些理解. js对象是保存在堆内存中的,当把对象赋值给变量时,是把对象在堆内存的引用(地址)赋值给了变量,变量通过地址来访问对象.下面来 ...
- Java多线程相关知识
1)wait() notify() sleep() sleep是Thread类的函数,wait和notify是Object的函数. sleep的时候keep对象锁,wait的时候release 对 ...