华南农业大学数据结构java版实验二.docx
- 文档编号:9169359
- 上传时间:2023-05-17
- 格式:DOCX
- 页数:44
- 大小:49.23KB
华南农业大学数据结构java版实验二.docx
《华南农业大学数据结构java版实验二.docx》由会员分享,可在线阅读,更多相关《华南农业大学数据结构java版实验二.docx(44页珍藏版)》请在冰点文库上搜索。
华南农业大学数据结构java版实验二
实验报告二线性表
华南农业大学信息(软件)学院
《数据结构(JAVA)》综合性、设计性实验成绩单
开设时间:
2017学年第二学期
班级
16信管3班
学号
2016250403xx
黄xx
实验题目
实验二线性表的基本操作
成绩
教师签名
一,实验目的:
(1)理解线性表的逻辑结构、两种存储结构和数据操作,熟练运用JAVA语言实现线性表的基本操作,分析各种操作算法特点和时间复杂度。
(2)掌握单链表的遍历、插入和删除等操作算法,实现多项式相加。
二,实验容:
1、设计一个有序顺序表(元素已排序,递增或递减),实现插入、删除等操作,元素插入位置由其值决定。
实现:
(1)升序排序顺序表类名为:
SortedSeqList,存成SortedSeqList.java文件;
(2)另外编写SortedSeqList_ex.java文件来演示调用排序顺序表
publicclassSortedSeqList{
privateintMAX_SIZE=10;
privateint[]ary=newint[MAX_SIZE];
privateintlength=0;
publicSortedSeqList(int[]array){
if(array==null||array.length==0){
this.length=0;
}else{
ary=array;
length=array.length;
}
}
publicvoidclear(){
length=0;
}
publicbooleanisEmpty(){
returnlength==0;
}
publicvoiddelete(intindex)throwsException{
if(length==0){
thrownewException("Noelmenttodelete");
}
intnewAry[]=newint[ary.length-1];
for(inti=0,j=0;i if(i==index){ continue; }else{ newAry[j++]=ary[i]; } } ary=newAry; length--; } publicintinsert(intvalue)throwsException{ if(length==MAX_SIZE){ thrownewException("Listisfull,can'tinsertmore"); } int[]newAry=newint[length+1]; inti=0,j=0; for(;i if(ary[i]>=value){ newAry[j]=value; break; }else{ newAry[j]=ary[i]; } } while(i newAry[++j]=ary[i]; i++; } ary=newAry; length++; returnvalue; } publicvoiddisplay(){ System.out.println("\nListnowis: "); for(inti=0;i System.out.print(ary[i]+"\t"); } } } (2)SortedSeqList_ex.java文件来演示调用排序顺序表 publicclassSortedSeqList_ex{ publicstaticvoidmain(String[]args)throwsException{ int[]ary={1,2,3,5,7}; SortedSeqListlist=newSortedSeqList(ary); list.display(); list.insert(4); list.display(); list.delete (2); list.display(); } } (3)实验结果 2、在SinglyLinkedList类中增加下列成员方法。 publicSinglyLinkedList(E[]element)//由指定数组中的多个对象构造单链表 publicSinglyLinkedList(SinglyLinkedListlist)//以单链表list构造新的单链表,复制单链表 publicvoidconcat(SinglyLinkedListlist)//将指定单链表list在当前单链表之后 publicNode publicbooleancontain(Eelement)//以查找结果判断单链表是否包含指定对象 publicbooleanremove(Eelement)//移去首次出现的指定对象 publicbooleanreplace(Objectobj,Eelement)//将单链表中的obj对象替换为对象element publicbooleanequals(Objectobj)//比较两条单链表是否相等 (1)实现代码: packageQ2; publicclassNode publicTdata; publicNode publicNode(Tdata,Node this.data=data; this.next=next; } publicNode(){ this(null,null); } } packageQ2; publicclassSinglyLinkedList Node publicSinglyLinkedList(E[]element){//由指定数组中的多个对象构造单链表 head=newNode Node for(inti=0;i List.next=newNode(element[i],null); List=List.next; } } publicSinglyLinkedList(SinglyLinkedListlist){//以单链表list构造新的单链表,复制单链表 head=newNode Node Node if(p.data==null){ p=p.next; list_new=list_new.next; } while(p! =null){ list_new.next=newNode list_new=list_new.next; p=p.next; } } publicvoidconcat(SinglyLinkedListlist){//将指定单链表list在当前单链表之后 Node Node Node if(p.data==null) p=p.next; while(p! =null){ rear=p; p=p.next; } if(q==null){ q=q.next; } rear.next=q; } publicNode Node Node if(p==null) p=p.next; while(p.next! =null){ if(p.data==element){ temp=p; } p=p.next; } returntemp; } publicbooleancontain(Eelement){//以查找结果判断单链表是否包含指定对象 booleanflag=false; Node Node if(p==null) p=p.next; while(p! =null){ if(p.data==element){ flag=true; } p=p.next; } returnflag; } publicbooleanremove(Eelement){//移去首次出现的指定对象 Node Node Node booleanflag=false; if(p==null) p=p.next; while(p! =null&&temp==null){ if(p.data==element){ temp=p; flag=true; break; } p=p.next; front=front.next; } front=p.next; returnflag; } publicbooleanreplace(Objectobj,Eelement){//将单链表中的obj对象替换为对象element booleanflag=false; if(obj! =null&&element! =null){ Node while(p! =null){ if(obj.equals(p.data)){ p.data=element; flag=true; } p=p.next; } } returnflag; } publicbooleanequals(Objectobj){//比较两条单链表是否相等 booleanflag=true; SinglyLinkedListx=(SinglyLinkedList)obj; Nodet=x.head.next; Nodes=this.head.next; while(t! =null&&s! =null){ if(t.data! =s.data){ flag=false; break; } t=t.next; s=s.next; } returnflag; } } packageQ2; importjava.util.*; publicclassTest{ staticInteger[]x={3,5,8,11}; staticInteger[]x1={3,5,8,11}; staticInteger[]x2={2,6,8,9,11,15,20}; staticSinglyLinkedList staticSinglyLinkedList staticSinglyLinkedList publicstaticvoiddisList(SinglyLinkedListList){ for(Nodetemp=List.head.next;temp! =null;temp=temp.next){ System.out.printf("%-5d",temp.data); } System.out.println(); } publicstaticvoidconcat(){ List1.concat(List3); } publicstaticvoidFind(){ System.out.println("请输入需要查找的数: "); Scannerscan=newScanner(System.in); Integerelement=scan.nextInt(); Nodet=List1.search(element); if(t! =null) System.out.println(t.data); else System.out.println("List1中找不到指定的数。 "); } publicstaticvoidContain(){ System.out.println("请输入所需包含的数: "); Scannerscan=newScanner(System.in); Integerelement=scan.nextInt(); if(List3.contain(element)) System.out.printf("List3包含指定的数%-5d\n",element); else System.out.printf("List3不包含指定的数%-5d\n",element); } publicstaticvoidremove(){ System.out.println("请输入指定移除的数: "); Scannerscan=newScanner(System.in); Integerelement=scan.nextInt(); if(List3.remove(element)) System.out.printf("%-5d已在List3中移除\n",element); elseSystem.out.printf("%-5d无法在List3中找到,无法移除\n",element); } publicstaticvoidreplace(){ System.out.println("请输入所需交换的两个数: "); Scannerscan=newScanner(System.in); Integerobj=scan.nextInt(); Integerelement=scan.nextInt(); if(List3.replace(obj,element)) System.out.printf("%-5d与%-5d两个数成功交换\n",obj,element); elseSystem.out.println("无法改动"); } publicstaticvoidequals(){ if(List1.equals(List2)) System.out.println("List1与List2两个单链表相等"); elseSystem.out.println("List1与List2两个单链表不相等"); if(List1.equals(List3)) System.out.println("List1与List3两个单链表相等"); elseSystem.out.println("List1与List3两个单链表不相等"); } publicstaticvoidmain(String[]args){ disList(List1); disList(List2); disList(List3); concat(); disList(List1); Find(); Contain(); remove(); replace(); equals(); } } (2)实验结果 3,先写出LList接口并写上要实现的方法的方法头,再CircSinglyLinkedList类中继承该接口并实现其方法。 (1)写出LList接口; publicinterfaceLList booleanisEmpty(); intsize(); Tget(inti); voidset(inti,Tx); StringtoString(); Tremove(inti); voidclear(); intsearch(Tkey); booleancontains(Tkey); TinsertDifferent(Tx); Tremove(Tkey); booleanequals(Objectobj); } (2)结点类 packageLab2; publicclassNode publicTdata; publicNode publicNode(Tdata,Node this.data=data; this.next=next; } publicNode(){ this(null,null); } publicStringtoString(){ returnthis.data.toString(); } } (3)CircSinglyLinkedList类 publicclassCircSinglyLinkedList publicNode Override publicbooleanisEmpty(){ //TODOAuto-generatedmethodstub returnthis.head.next==null; } Override publicintsize(){ //TODOAuto-generatedmethodstub Node intcount=0; while(temp! =null){ count++; temp=temp.next; } returncount; } Override publicTget(inti){ //TODOAuto-generatedmethodstub Node intcount=0; while(true){ count+=1; temp=temp.next; if(count==i){ returntemp.data; } } } Override publicvoidset(inti,Tx){ //TODOAuto-generatedmethodstub Node intcount=0; while(true){ count+=1; temp=temp.next; if(count==i){ temp.data=x; } } } Override publicTremove(inti){ //TODOAuto-generatedmethodstub intcount=0; Node Node for(;;temp=temp.next){ if(count+1==i){ p=temp.next; temp.next=temp.next.next; return(T)p; } count++; } } Override publicvoidclear(){ //TODOAuto-generatedmethodstub this.head.next=null; } Override publicintsearch(Objectkey){ //TODOAuto-generatedmethodstub inti=0; Node for(;temp.next! =null;temp=temp.next){ i+=1; if(temp.data.equals(key)){ break; } } returni; } Override publicbooleancontains(Objectkey){ //TODOAuto-generatedmethodstub Node for(;temp.next! =null;temp=temp.next){ if(temp.data.equals(key)){ returntrue; } } returnfalse; } Override publicNode
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 华南 农业大学 数据结构 java 实验