《C语言》数据机构实验报告.docx

上传人:b****1 文档编号:1743203 上传时间:2023-05-01 格式:DOCX 页数:19 大小:18.87KB
下载 相关 举报
《C语言》数据机构实验报告.docx_第1页
第1页 / 共19页
《C语言》数据机构实验报告.docx_第2页
第2页 / 共19页
《C语言》数据机构实验报告.docx_第3页
第3页 / 共19页
《C语言》数据机构实验报告.docx_第4页
第4页 / 共19页
《C语言》数据机构实验报告.docx_第5页
第5页 / 共19页
《C语言》数据机构实验报告.docx_第6页
第6页 / 共19页
《C语言》数据机构实验报告.docx_第7页
第7页 / 共19页
《C语言》数据机构实验报告.docx_第8页
第8页 / 共19页
《C语言》数据机构实验报告.docx_第9页
第9页 / 共19页
《C语言》数据机构实验报告.docx_第10页
第10页 / 共19页
《C语言》数据机构实验报告.docx_第11页
第11页 / 共19页
《C语言》数据机构实验报告.docx_第12页
第12页 / 共19页
《C语言》数据机构实验报告.docx_第13页
第13页 / 共19页
《C语言》数据机构实验报告.docx_第14页
第14页 / 共19页
《C语言》数据机构实验报告.docx_第15页
第15页 / 共19页
《C语言》数据机构实验报告.docx_第16页
第16页 / 共19页
《C语言》数据机构实验报告.docx_第17页
第17页 / 共19页
《C语言》数据机构实验报告.docx_第18页
第18页 / 共19页
《C语言》数据机构实验报告.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

《C语言》数据机构实验报告.docx

《《C语言》数据机构实验报告.docx》由会员分享,可在线阅读,更多相关《《C语言》数据机构实验报告.docx(19页珍藏版)》请在冰点文库上搜索。

《C语言》数据机构实验报告.docx

《C语言》数据机构实验报告

数据结构实验报告

实验一:

抽象数据类型的实现-以集合为例

问题:

以集合为例设计一个抽象数据类型;

1.集合是对象的无序组合,我们可以把集合定义成一个新的数据类型;

2.为了简化可以把集合限制为有限整数元素的集合,有了这个限制,集合可以作为一个静态整数数组实现;

3.为了操作方便,在处理对象中还包括集合的元素个数(即:

集合基数)。

C语言程序代码:

//-------------------------------------------------------------

#include

#include

#definemaxCard16

#definemaxLen64

#definenoErr1

#defineoverflow0

//-------------------------------------------------------------

//定义结构体Set和Set_product

structSet{

intelems[maxCard];

intcard;

};

 

structSet_product{

charelems[maxCard*maxCard][maxLen];

intcard;

};

//-------------------------------------------------------------

//函数声明

voidEmptySet(Set*);

intAddElem(Set*,int);

voidPrint(Set*);

voidRmvElem(Set*,int);

boolMember(Set*,int);

voidCopy(Set*,Set*);

voidIntersect(Set*,Set*,Set*);

intUnion(Set*,Set*,Set*);

boolEqual(Set*,Set*);

boolSubset(Set*,Set*);

boolPSubset(Set*,Set*);

voidCartesian_product(Set*,Set*,Set_product*);

voidPrint(Set_product*);

//-------------------------------------------------------------

//主函数调用

intmain()

{

Sets1,s2,s3;

Set_productss1;

EmptySet(&s1);

EmptySet(&s2);

EmptySet(&s3);

AddElem(&s1,10);AddElem(&s1,20);AddElem(&s1,30);AddElem(&s1,40);

AddElem(&s2,30);AddElem(&s2,50);AddElem(&s2,10);AddElem(&s2,60);

cout<<"s1=";Print(&s1);

cout<<"s2=";Print(&s2);

RmvElem(&s2,50);

cout<<"s2-{50}=";Print(&s2);

if(Member(&s1,20))

cout<<"20isins1"<

Intersect(&s1,&s2,&s3);

cout<<"s1intsecs2=";Print(&s3);

Union(&s1,&s2,&s3);

cout<<"s1unions2=";Print(&s3);

if(Subset(&s1,&s3))

cout<<"s1isthesubsetofs3"<

if(PSubset(&s1,&s3))

cout<<"s1isthepuresubsetofs3"<

if(!

Equal(&s1,&s2))

cout<<"s1/=s2"<

else

cout<<"s1=s2"<

Cartesian_product(&s1,&s2,&ss1);

cout<<"s1products2=";Print(&ss1);

getchar();

}//main

//-------------------------------------------------------------

//函数EmptySet

voidEmptySet(Set*set)

{

set->card=0;

}

intAddElem(Set*set,intelem)

{

for(inti=0;icard;++i)

if(set->elems[i]==elem)

returnnoErr;

if(set->card

set->elems[set->card++]=elem;

returnnoErr;

}

else

returnoverflow;

}//AddElem

//-------------------------------------------------------------

//函数Print

voidPrint(Set*set)

{

if(set->card==0)

//cout<<"TheSethasnoelement!

\n";

cout<<"{}"<

else{

cout<<"{";

for(inti=0;icard-1;++i)

cout<elems[i]<<",";

if(set->card>0)

cout<elems[set->card-1];

cout<<"}"<

}

}//Print

//-------------------------------------------------------------

//函数Print

voidPrint(Set_product*set)

{

if(set->card==0)

//cout<<"TheSethasnoelement!

\n";

cout<<"{}"<

else{

cout<<"{";

for(inti=0;icard-1;++i)

cout<elems[i]<<",";

if(set->card>0)

cout<elems[set->card-1];

cout<<"}"<

}

}//PrintCartesian_product

//-------------------------------------------------------------

//函数RmvElem

voidRmvElem(Set*set,intelem)

{

for(inti=0;icard;++i)

if(set->elems[i]==elem){

for(;icard-1;++i)

set->elems[i]=set->elems[i+1];

--set->card;

}

}//RmvElem

//-------------------------------------------------------------

//函数Member

boolMember(Set*set,intelem)

{

for(inti=0;icard;++i)

if(set->elems[i]==elem)

returntrue;

returnfalse;

}//Member

//-------------------------------------------------------------

//函数Copy

voidCopy(Set*set,Set*res)

{

for(inti=0;icard;++i)

res->elems[i]=set->elems[i];

res->card=set->card;

}//Copy

//-------------------------------------------------------------

//函数Equal

boolEqual(Set*set1,Set*set2)

{

if(set1->card!

=set2->card)

returnfalse;

for(inti=0;icard;++i)

if(!

Member(set2,set1->elems[i]))

returnfalse;

returntrue;

}//Equal

//-------------------------------------------------------------

//函数Intersect

voidIntersect(Set*set1,Set*set2,Set*res)

{

res->card=0;

for(inti=0;icard;++i)

for(intj=0;jcard;++j)

if(set1->elems[i]==set2->elems[j]){

res->elems[res->card++]=set1->elems[i];

break;

}

}//Intersect

//-------------------------------------------------------------

//函数Union

intUnion(Set*set1,Set*set2,Set*res)

{

Copy(set1,res);

for(inti=0;icard;++i)

if(AddElem(res,set2->elems[i])==overflow)

returnoverflow;

returnnoErr;

}//Union

//-------------------------------------------------------------

//函数Subset

boolSubset(Set*set1,Set*set2)

{

if(set1->card>set2->card)

returnfalse;

for(inti=0;icard;++i){

if(!

Member(set2,set1->elems[i]))

returnfalse;

}

returntrue;

}//Subset

//-------------------------------------------------------------

//函数PSubset

boolPSubset(Set*set1,Set*set2)

{

if(set1->cardcard&&Subset(set1,set2))

returntrue;

}//PSubset

//-------------------------------------------------------------

//函数Cartesian_product

voidCartesian_product(Set*set1,Set*set2,Set_product*res)

{

intk=0;

res->card=set1->card*set2->card;

for(inti=0;icard;++i)

for(intj=0;jcard;++j,++k)

sprintf(res->elems[k],"(%d,%d)",set1->elems[i],set2->elems[j]);

}

 

实验二:

链表及其应用

问题:

试编一个算法,将一个用单链表表示的稀疏多项式分解两个多项式。

1.这两个多项式中各自仅含奇次项或偶次项;

2.要求利用原链表中的节点空间构成这两个多项式。

C语言程序代码:

#include

#include

typedefstruct{

floatcoef;

intexpnum;

}term,ElemType;//数据包括底数,次数;

typedefstructLNode{

ElemTypedata;

structLNode*next;

}LNode,*LinkList;//声明节点为Lnode型和LinkList的指针;

voidCreateList_L(LinkList&L,intn);

voidDisplay_L(LinkListL);

voidSplit_L(LinkList&plyn,LinkList&odd);

voidReverse_L(LinkListL);

 

voidmain()

{

intNodeNum=4;

LinkListList,oddList;

CreateList_L(List,NodeNum);

Display_L(List);

Reverse_L(List);

Display_L(List);

Split_L(List,oddList);

Display_L(List);

Display_L(oddList);

}

//输入n个元素的值,利用尾插法建立带表头结点的单链线性表L

voidCreateList_L(LinkList&L,intn)

{

LinkListp,Pre;

inti;

L=(LinkList)malloc(sizeof(LNode));//先建立一个带头节点的线性链表;

Pre=L;

for(i=0;i

{

p=(LinkList)malloc(sizeof(LNode));//生成新结点

printf("theexpnumis:

");//输入数据的次数;

scanf("%d",&p->data.expnum);

printf("thecoefis:

");

scanf("%f",&p->data.coef);//输入数据的底数;

Pre->next=p;

Pre=p;//修改尾指针;

}

Pre->next=NULL;

}

voidDisplay_L(LinkListL)//输出次数与底数;

{

L=L->next;

while(L)

{

printf("Theexpnumis:

");//次数;

printf("%d\n",L->data.expnum);

printf("Thecoefis:

");

printf("%.2f\n",L->data.coef);

L=L->next;

}

printf("\n");

}

voidReverse_L(LinkListL)

{

LinkListp=NULL,q,s;

q=L->next;

while(q)

{

s=q->next;

q->next=p;

p=q;

q=s;

}

L->next=p;

}//Reverse_L

 

voidSplit_L(LinkList&plyn,LinkList&odd)

{

LinkListp,q,s;

odd=(LinkList)malloc(sizeof(LNode));

odd->next=NULL;

q=plyn;

p=plyn->next;

s=odd;

while(p)

{

if(p->data.expnum%2==0)

{

q=p;

p=p->next;

}

else

{

q->next=p->next;//在原表中删除奇次项节点

s->next=p;//将奇次项节点插入到新表中

p=q->next;

s=s->next;

s->next=NULL;

}

}

}//Split_L

 

实验三:

队列和栈的使用

问题:

试编写一个算法,利用栈将队列逆置;

1.栈和队列的数据元素都使用整型变量;

2.队列用链式存储,栈用动态的顺序存储;

C语言程序代码:

//-------------------------------------------------------------

#include

//#include

#include

//-------------------------------------------------------------

typedefstruct{

floatcoef;

intexpnum;

}term,ElemType;

typedefstructLNode{

ElemTypedata;

structLNode*next;

}LNode,*LinkList;

//-------------------------------------------------------------

voidCreateList_L(LinkList&,int);

voidDisplay_L(LinkList);

voidSplit_L(LinkList&,LinkList&);

voidReverse_L(LinkList);

//-------------------------------------------------------------

voidmain()

{

intNodeNum=4;

LinkListList,oddList;

CreateList_L(List,NodeNum);

Display_L(List);

Reverse_L(List);

Display_L(List);

Split_L(List,oddList);

Display_L(List);

Display_L(oddList);

}//main

//--------------------------------------------------------------------------------------------------

voidCreateList_L(LinkList&L,intn){

//逆位序输入n个元素的值,建立带表头结点的单链线性表L

LinkListp;

inti;

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

L->next=NULL;//先建立一个带头结点的单链表

for(i=n;i>0;--i){

p=(LinkList)malloc(sizeof(LNode));//生成新结点

cout<<"expnum=?

";cin>>p->data.expnum;

cout<<"coef=?

";cin>>p->data.coef;

p->next=L->next;L->next=p;//插入到表头

}

//cout<<"链表生成."<

}//CreateList_L

 

//-------------------------------------------------------------

voidDisplay_L(LinkListL)

{

L=L->next;

cout<<"{";

while(L)

{

cout<<"("<data.coef<<",";

cout<data.expnum<<")";

L=L->next;

}

cout<<"}\n";

}//Display_L

//------------------------------------------------------------------------------

voidReverse_L(LinkListL)

{

LinkListp=NULL,q,s;

q=L->next;

while(q){

s=q->next;q->next=p;

p=q;q=s;

}

L->next=p;

}//Reverse_L

//------------------------------------------------------------------------------

voidSplit_L(LinkList&plyn,LinkList&odd)

{

LinkListp,q,s;

odd=(LinkList)malloc(sizeof(LNode));

odd->next=NULL;

q=ply

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

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2