#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: 求两链表的交集和并集的更多相关文章

  1. 【转载】 C#使用Union方法求两个List集合的并集数据

    在C#语言的编程开发中,有时候需要对List集合数据进行运算,如对两个List集合进行交集运算或者并集运算,其中针对2个List集合的并集运算,可以使用Union方法来快速实现,Union方法的调用格 ...

  2. python两个 list 交集,并集,差集的方法+两个tuple比较操作+两个set的交集,并集,差集操作+两个dict的比较操作

    转自:http://blog.chinaunix.net/uid-200142-id-3992553.html 有时候,为了需求,需要统计两个 list 之间的交集,并集,差集.查询了一些资料,现在总 ...

  3. LINUX Shell 下求两个文件交集和差集的办法

    http://blog.csdn.net/autofei/article/details/6579320 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d ...

  4. grep和map计算两个集合交集、并集、补集

    #!/usr/bin/perl use strict; ######################################## 用grep 和map 获取两个列表的交集并集.补集###### ...

  5. 二叉树系列 - 求两节点的最低公共祖先,例 剑指Offer 50

    前言 本篇是对二叉树系列中求最低公共祖先类题目的讨论. 题目 对于给定二叉树,输入两个树节点,求它们的最低公共祖先. 思考:这其实并不单单是一道题目,解题的过程中,要先弄清楚这棵二叉树有没有一些特殊的 ...

  6. [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 ...

  7. [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 ...

  8. 求n个排序链表的交集

    题目大致意思是 给出n个排序list,每个list只有两个方法 (1)bool goNext(); 判断是否有下一个元素,没有元素返回false, 有元素返回true (2)int next(); 返 ...

  9. 求单链表倒数第m个结点

    问题:求单链表倒数第m个结点,要求不准求链表的长度,也不许对链表进行逆转 解:设置两个指针p和q,p.q指向第一个结点.让p先移动到链表的第m个结点,然后p和q同时向后移动,直到p首先到达尾结点.此时 ...

随机推荐

  1. C++类继承中的构造函数和析构函数 调用顺序

    思想: 在C++的类继承中,构造函数不能被继承(C11中可以被继承,但仅仅是写起来方便,不是真正的继承) 建立对象时,首先调用基类的构造函数,然后在调用下一个派生类的构造函数,依次类推: 析构对象时, ...

  2. nginx的请求接收流程(二)

    在ngx_http_process_request_line函数中,解析完请求行之后,如果请求行的uri里面包含了域名部分,则将其保持在请求结构的headers_in成员的server字段,heade ...

  3. XML的DTD约束

    DTD约束:DTD的声明和引用 1.内部DTD文档 <!DOCTYPE 根元素 [定义内容]> 2.外部DTD文档 <!DOCTYPE 根元素 SYSTEM "DTD文件路 ...

  4. QNDTU外壳及开发板

        昨天从淘宝上淘来了个DTU外壳,翻出来之前的STM32开发板和GPRS模块开发板,今天准备复习一下开发板,把裸板跑起来.     晒一下装备:     两块开发板:           51n ...

  5. CentOS7 lamp安装 centoOS6 lamp

    快速lamp安装 How To Install Linux, Apache, MySQL, PHP (LAMP) stack On CentOS 7 Introduction A "LAMP ...

  6. git 之别名配置

    在git操作中有很多命令我们自己可以起别名,以提高操作效率. 1. 配置方式 1)项目级别的配置,仅对当前项目生效(将写入到.git/config文件中)    $ git config --glob ...

  7. Cocos2d-x Render-NewCulling

    .cpp layout->setBackGroundImageScale9Enabled(true); layout->setBackGroundImage("green_edi ...

  8. cocos2d_android 瞬间动作

    该文章所写的瞬间动作主要有CCFlipX,CCFlipY,CCHide,CCShow 当中CCFlipX是以Y轴为中心旋转,CCFlipY是以X轴为中心旋转,CCHide将精灵对象隐藏,CCShow将 ...

  9. Matlab lugui

    function [L,U,pv,qv] = lugui(A,pivotstrat) %LUGUI Gaussian elimination demonstration. % % LUGUI(A) s ...

  10. mma ctf 1st && csaw 2015

    (很久以前做的,现在发一下)最近做了两个CTF,水平太渣,做了没几道题,挑几个自己做的记录一下. mma ctf 1st 之 rps: from socket import * s = socket( ...