习题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操作redis-zset
#!/usr/bin/python #!coding: utf-8 import redis if __name__=="__main__": try: conn=redis.St ...
- js监听和观察者模式
http://redhacker.iteye.com/blog/1756436 https://developer.mozilla.org/en-US/docs/Web/Guide http://ww ...
- cf452A Eevee
A. Eevee time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- linux之SQL语句简明教程---WHERE
我们并不一定每一次都要将表格内的资料都完全抓出.在许多时候,我们会需要选择性地抓资料.就我们的例子来说,我们可能只要抓出营业额超过 $1,000 的资料.要做到这一点,我们就需要用到 WHERE 这个 ...
- 什么是dandy 风格_百度知道
什么是dandy 风格_百度知道 什么是dandy 风格 2010-06-21 10:56 平ping123 | 分类:服装/首饰 | 浏览11257次 题谢谢 有没有比 ...
- nyoj 1238 最少换乘(dijkstra)
描述 欧洲某城是一个著名的旅游胜地,每年都有成千上万的人前来观光旅行.Dr. Kong决定利用暑假好好游览一番.. 年轻人旅游不怕辛苦,不怕劳累,只要费用低就行.但Dr. Kong年过半百,他希望乘坐 ...
- Asp.net异步IHttpAsyncHandler示例
/// <summary> /// 异步IHttpHandler,实现了一个简单的统计流量的功能, /// 由于是示例代码,所以没有判断IP或者MAC /// </summary&g ...
- ZCTF-final-restaurant1
和线上赛的题目差别不大,但是需要自己去泄露堆的地址.除了线上赛的溢出之外,还多了一个Use After Free的洞.我写了两种利用方法. 线上赛writeup见:http://www.cnblogs ...
- T-SQL视图
视图(view) 用于存储封装一个select语句,使用方法和表一样.也可通过界面对视图进行操作. create view ordersWithNum --创建一个视图ordersWithNum as ...
- 浅谈web前端就业的学习路线
初级前端 主要学习三个部分:HTML,CSS,JavaScript 一.html + css部分: 这部分特别简单,到网上搜资料,书籍视频非常多.css中盒子模型,流动,block,inline,层叠 ...