题目
已知单链线性表
LA
和
LB
的元素按值非递减排列,假设利用两个线性表
LA
和
LB
分别表示两个集合
A
和
算法
void ListIntersection_L(LinkList &a,LinkList &b,LinkList &Lc){
pa=La->next;pb=Lb->next;
Lc=pc=la;//用La的头结点作为Lc的头结点
while(pa && pb){
if(pa->data == pb->data)//数值相等时
{
pc=pa;pa=pa->next;
}
else if(pa->data < pb->data)//如果小于
{
pc->next = pa->next;
free(pa);
pa=pc->next;
}
else
pb=pb->next;
}
pc->next = NULL;
while(pa)
{pc=pa;pa=pc->next;free(pc);}
pb = lb;
while(pb)
{pc=pb;pb=pb->next;free(pc);}
}//ListIntersection_L
这里采用C作为临时的指向,以便访问
算法动态演示
算法具体实现
//所有定义
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
typedef int Status;//为了方便算法可用性,算法的Status可以通过这里可改
typedef char ElemType;//为了输出的可用性,数据的ElemType可以通过这里更改
struct LNode
{
ElemType data;
struct LNode *next;
};
typedef struct LNode *LinkList; /* 另一种定义LinkList的方法 */
//包含的头文件
#include <iostream>
using namespace std;
Status InitList(LinkList *L)
{ /* 操作结果:构造一个空的线性表L */
*L = (LinkList)malloc(sizeof(struct LNode)); /* 产生头结点,并使L指向此头结点 */
if (!*L) /* 存储分配失败 */
exit(OVERFLOW);
(*L)->next = NULL; /* 指针域为空 */
return OK;
}
void ListIntersection_L(LinkList &La, LinkList &Lb, LinkList &Lc){
LinkList pa, pb, pc;
pa = La->next; pb = Lb->next;
Lc = pc = La;//用La的头结点作为Lc的头结点
while (pa && pb){
if (pa->data == pb->data)//数值相等时
{
pc = pa; pa=pa->next;//
}
else if (pa->data < pb->data)//如果小于
{
pc->next = pa->next;
free(pa);
pa = pc->next;
}
else
pb = pb->next;
}
pc->next = NULL;
while (pa)//如果b完成了,就是a未的其他部分都删除掉
{
pc = pa; pa = pc->next; free(pc);
}
pb = Lb;
while (pb)//如果a完成了,就把b从头整个链都删除掉
{
pc = pb; pb = pb->next; free(pc);
}
}//ListIntersection_L
Status ListInsert_L(LinkList &L, int i, ElemType e) { // 算法2.9
// 在带头结点的单链线性表L的第i个元素之前插入元素e
LinkList p, s;
p = L;
int j = 0;
while (p && j < i - 1) { // 寻找第i-1个结点
p = p->next;
++j;
}
if (!p || j > i - 1) return ERROR; // i小于1或者大于表长
s = (LinkList)malloc(sizeof(LNode)); // 生成新结点
s->data = e; s->next = p->next; // 插入L中
p->next = s;
return OK;
} // LinstInsert_L
Status ListTraverse(LinkList L, void(*vi)(LinkList&))
{ /* 初始条件:顺序线性表L已存在 */
/* 操作结果:依次对L的每个数据元素调用函数vi()。一旦vi()失败,则操作失败 */
LinkList p = L->next;
cout << " ";
while (p != NULL)
{
vi(p);
p = p->next;
}
return OK;
}
void visit(LinkList &node)
{
cout << node->data << " ";
}
int main()
{
//创建三个表
LinkList A, B, C;
//本程序说明,是在VS2013中运行的,若是其他地方无法运行,建议修改代码
//程序声明
cout << "***************************************************************************" << endl;
cout << " 《数据结构》<C语言版本>严蔚敏 吴伟名 编著 " << endl;
cout << " 编写年月2016年3月 " << endl;
cout << " 编写者:YuYunTan " << endl;
cout << " 链表交集 " << endl;
cout << "***************************************************************************" << endl;
//表的初始化,即每一个表都能有一个头结点,方便进行指向使用
//状态判断
InitList(&A);
InitList(&B);
ListInsert_L(A, 1, 'A');
ListInsert_L(A, 2, 'B');
ListInsert_L(A, 3, 'C');
ListInsert_L(A, 4, 'F');
ListInsert_L(A, 4, 'G');
cout << "List A = <";
ListTraverse(A, visit);
cout << ">" << endl;
ListInsert_L(B, 1,'B');
ListInsert_L(B, 2, 'C');
ListInsert_L(B, 3, 'K');
ListInsert_L(B, 4, 'I');
cout << "List B = <";
ListTraverse(B, visit);
cout << ">" << endl;
cout << "ListIntersection_L(A,B);" << endl;
ListIntersection_L(A, B, C);
cout << "List C = <";
ListTraverse(C, visit);
cout << ">" << endl;
system("pause");
return 0;
}