基于单链表实现集合的并交差运算实验报告Word文档下载推荐.docx

上传人:b****6 文档编号:19696232 上传时间:2023-01-08 格式:DOCX 页数:23 大小:97.83KB
下载 相关 举报
基于单链表实现集合的并交差运算实验报告Word文档下载推荐.docx_第1页
第1页 / 共23页
基于单链表实现集合的并交差运算实验报告Word文档下载推荐.docx_第2页
第2页 / 共23页
基于单链表实现集合的并交差运算实验报告Word文档下载推荐.docx_第3页
第3页 / 共23页
基于单链表实现集合的并交差运算实验报告Word文档下载推荐.docx_第4页
第4页 / 共23页
基于单链表实现集合的并交差运算实验报告Word文档下载推荐.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

基于单链表实现集合的并交差运算实验报告Word文档下载推荐.docx

《基于单链表实现集合的并交差运算实验报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《基于单链表实现集合的并交差运算实验报告Word文档下载推荐.docx(23页珍藏版)》请在冰豆网上搜索。

基于单链表实现集合的并交差运算实验报告Word文档下载推荐.docx

InitList(&

L)

操作结果;

构造一个空的线性表L

DestroyList(&

初始条件:

线性表L已存在

操作结果:

销毁线性表L

ClearList(&

将L置为空表

ListEmpty(L)

线性表已存在

若L为空表,则返回TRUE,否则返回FALSE

ListLength(L)

返回L中数据元素的个数

GetElem(L,i)

初始条件:

线性表已存在,1<

=i<

=ListLength(L)

用e返回L中第i个数据元素的值

LocateElem(L,i,e)

线性表已存在,用循环遍历整个线性表,如果e与线性表中的元素相同;

用此时的i+1返回该元素在线性表的位序

ListInsert(&

L,i,e)

线性表存在,1<

=ListLength(L)+1;

在L中第i个位置之前插入新的数据元素,e,L的长度加1。

ListDelete(&

L,i,&

e)

线性表L已存在且非空,1<

=ListLength(L);

删除L的第i个数据元素,并用e返回其值,L的长度减1

}ADTList

3.2存储结构的定义;

typedefcharElemType;

typedefstructLNode

{

ElemTypedata;

structLNode*next;

}LinkList;

3.3基本操作实现:

/*单链表的初始化*/

voidInitList(LinkList*&

L=(LinkList*)malloc(sizeof(LinkList));

L->

next=NULL;

}

/*向单链表中插入数据元素*/

boolListInsert(LinkList*&

L,intx,chare)

intj=0;

LinkList*p=L,*s;

while(p!

=NULL&

&

j<

x-1)

{

p=p->

next;

j++;

}

if(p==NULL)

returnfalse;

else

s=(LinkList*)malloc(sizeof(LinkList));

s->

data=e;

next=p->

p->

next=s;

returntrue;

/*输出单链表*/

voidDispList(LinkList*L)

LinkList*p=L->

=NULL)

printf("

%c"

p->

data);

\n"

);

/*求单链表的长度*/

intListLength(LinkList*L)

inti=0;

i++;

returni;

/*查看单链表是否为空*/

boolListEmpty(LinkList*L)

returnL->

next==NULL;

/*求单链表中某个数据元素值*/

boolGetElem(LinkList*L,inti,ElemType&

LinkList*p=L;

j<

i)

p=p->

e=p->

data;

/*在单链表中查找元素*/

intLocateElem(LinkList*L,ElemTypee)

data!

=e)

return0;

/*删除单链表中第i个元素*/

boolListDelete(LinkList*&

L,inti,ElemType&

LinkList*p=L,*q;

i-1)

q=p->

if(q==NULL)

e=q->

next=q->

free(q);

/*删除单链表*/

voidDestroyList(LinkList*&

LinkList*q=p->

while(q!

free(p);

p=q;

3.4解题思路:

1.先通过CreateListR函数将集合a和b中的元素添加到顺序表ha和hb中,添加过程使用的是顺序表原有的Initlist函数(初始化表)和ListInsert函数(向表中插入元素)。

2.因为原集合是无序的,所以我通过sort函数(选择排序),使得集合变得有序。

3.得到有序集合ha和hb后,便可以使用Union函数(类似归并的思想写出来的求并集的函数),求出ha和hb的并集。

4.而求交集的方法则是,通过将集合a中的元素一个一个取出,并通过函数LocateElem,查看集合hb中是否存在该元素,如果存在则将元素放入hc,如果不存在,则舍去。

以此求得两集合的交集。

5.求两集合的差则可以反过来,同样通过将集合a中的元素一个一个取出,并通过函数LocateElem,查看集合hb中是否存在该元素,如果不存在则将元素放入hc,如果存在,则舍去。

以此求得两集合的差集。

(单链表求交并差集合的思想和顺序表求交并差集合的思想基本相同)

3.5解题过程:

实验源代码如下:

3.5.1单链表的各种运算

#include<

iostream>

cstdio>

malloc.h>

usingnamespacestd;

/*定义单链表数据*/

intmain()

LinkList*h;

ElemTypee;

单链表的基本运算如下:

(1)初始化单链表\n"

InitList(h);

(2)依次采用尾插法插入a,b,c,d,e元素\n"

ListInsert(h,1,'

a'

ListInsert(h,2,'

b'

ListInsert(h,3,'

c'

ListInsert(h,4,'

d'

ListInsert(h,5,'

e'

(3)输出单链表:

"

DispList(h);

(4)单链表h的长度=%d\n"

ListLength(h));

(5)单链表h为%s\n"

(ListEmpty(h)?

空"

:

非空"

));

GetElem(h,3,e);

(6)单链表h的第3个元素=%c\n"

e);

(7)元素a的位置=%d\n"

LocateElem(h,'

(8)在第4个元素位置上插入f元素\n"

f'

(9)输出单链表h:

(10)删除h的第3个元素\n"

ListDelete(h,3,e);

(11)输出单链表h:

(12)释放单链表h\n"

DestroyList(h);

3.5.2求集合的并交差

%c"

voidCreateListR(LinkList*&

L,ElemTypee[],intn)

InitList(L);

inti;

for(i=0;

i<

n;

++i)

if(!

LocateElem(L,e[i]))

ListInsert(L,i+1,e[i]);

voidInsterSect(LinkList*a,LinkList*b,LinkList*&

c)

DestroyList(c);

InitList(c);

LinkList*p=a->

if(LocateElem(b,p->

data))

ListInsert(c,++i,p->

voidSubs(LinkList*a,LinkList*b,LinkList*&

LocateElem(b,p->

voidUnion(LinkList*a,LinkList*b,LinkList*&

LinkList*q=b->

intk=0;

q!

if(p->

data<

q->

data)

ListInsert(c,k+1,p->

k++;

elseif(p->

data==q->

q=q->

ListInsert(c,k+1,q->

///cout<

<

hehe"

endl;

voidsort(LinkList*&

LinkList*p,*pre,*q,*k;

InitList(p);

charc;

while(!

ListEmpty(L))

pre=L->

c=pre->

while(pre!

if(c>

=pre->

pre=pre->

ListInsert(p,++i,c);

inttag=LocateElem(L,c);

ListDelete(L,tag,c);

L=p;

 

LinkList*ha,*hb,*hc;

ElemTypea[]={'

'

h'

};

ElemTypeb[]={'

g'

集合的运算如下\n"

CreateListR(ha,a,4);

CreateListR(hb,b,6);

原集合A:

"

DispList(ha);

原集合B:

DispList(hb);

sort(ha);

sort(hb);

有序集合A:

有序集合B:

Union(ha,hb,hc);

集合的并

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1