华南农业大学数据结构java版实验二.docx

上传人:b****8 文档编号:9169359 上传时间:2023-05-17 格式:DOCX 页数:44 大小:49.23KB
下载 相关 举报
华南农业大学数据结构java版实验二.docx_第1页
第1页 / 共44页
华南农业大学数据结构java版实验二.docx_第2页
第2页 / 共44页
华南农业大学数据结构java版实验二.docx_第3页
第3页 / 共44页
华南农业大学数据结构java版实验二.docx_第4页
第4页 / 共44页
华南农业大学数据结构java版实验二.docx_第5页
第5页 / 共44页
华南农业大学数据结构java版实验二.docx_第6页
第6页 / 共44页
华南农业大学数据结构java版实验二.docx_第7页
第7页 / 共44页
华南农业大学数据结构java版实验二.docx_第8页
第8页 / 共44页
华南农业大学数据结构java版实验二.docx_第9页
第9页 / 共44页
华南农业大学数据结构java版实验二.docx_第10页
第10页 / 共44页
华南农业大学数据结构java版实验二.docx_第11页
第11页 / 共44页
华南农业大学数据结构java版实验二.docx_第12页
第12页 / 共44页
华南农业大学数据结构java版实验二.docx_第13页
第13页 / 共44页
华南农业大学数据结构java版实验二.docx_第14页
第14页 / 共44页
华南农业大学数据结构java版实验二.docx_第15页
第15页 / 共44页
华南农业大学数据结构java版实验二.docx_第16页
第16页 / 共44页
华南农业大学数据结构java版实验二.docx_第17页
第17页 / 共44页
华南农业大学数据结构java版实验二.docx_第18页
第18页 / 共44页
华南农业大学数据结构java版实验二.docx_第19页
第19页 / 共44页
华南农业大学数据结构java版实验二.docx_第20页
第20页 / 共44页
亲,该文档总共44页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

华南农业大学数据结构java版实验二.docx

《华南农业大学数据结构java版实验二.docx》由会员分享,可在线阅读,更多相关《华南农业大学数据结构java版实验二.docx(44页珍藏版)》请在冰点文库上搜索。

华南农业大学数据结构java版实验二.docx

华南农业大学数据结构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在当前单链表之后

publicNodesearch(Eelement)//若查找到指定,则返回结点,否则返回null

publicbooleancontain(Eelement)//以查找结果判断单链表是否包含指定对象

publicbooleanremove(Eelement)//移去首次出现的指定对象

publicbooleanreplace(Objectobj,Eelement)//将单链表中的obj对象替换为对象element

publicbooleanequals(Objectobj)//比较两条单链表是否相等

(1)实现代码:

packageQ2;

publicclassNode{

publicTdata;

publicNodenext;

publicNode(Tdata,Nodenext){

this.data=data;

this.next=next;

}

publicNode(){

this(null,null);

}

}

packageQ2;

publicclassSinglyLinkedList{

Nodehead;

publicSinglyLinkedList(E[]element){//由指定数组中的多个对象构造单链表

head=newNode();

NodeList=head;

for(inti=0;i

List.next=newNode(element[i],null);

List=List.next;

}

}

publicSinglyLinkedList(SinglyLinkedListlist){//以单链表list构造新的单链表,复制单链表

head=newNode();

Nodelist_new=head;

Nodep=list.head;

if(p.data==null){

p=p.next;

list_new=list_new.next;

}

while(p!

=null){

list_new.next=newNode(p.data,null);

list_new=list_new.next;

p=p.next;

}

}

publicvoidconcat(SinglyLinkedListlist){//将指定单链表list在当前单链表之后

Noderear=null;

Nodep=head;

Nodeq=list.head.next;

if(p.data==null)

p=p.next;

while(p!

=null){

rear=p;

p=p.next;

}

if(q==null){

q=q.next;

}

rear.next=q;

}

publicNodesearch(Eelement){//若查找到指定,则返回结点,否则返回null

Nodep=this.head;

Nodetemp=null;

if(p==null)

p=p.next;

while(p.next!

=null){

if(p.data==element){

temp=p;

}

p=p.next;

}

returntemp;

}

publicbooleancontain(Eelement){//以查找结果判断单链表是否包含指定对象

booleanflag=false;

Nodep=this.head;

Nodetemp=null;

if(p==null)

p=p.next;

while(p!

=null){

if(p.data==element){

flag=true;

}

p=p.next;

}

returnflag;

}

publicbooleanremove(Eelement){//移去首次出现的指定对象

Nodep=this.head;

Nodetemp=null;

Nodefront=head;

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){

Nodep=this.head;

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};

staticSinglyLinkedListList1=newSinglyLinkedList(x);

staticSinglyLinkedListList2=newSinglyLinkedList(x1);

staticSinglyLinkedListList3=newSinglyLinkedList(x2);

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;

publicNodenext;

publicNode(Tdata,Nodenext){

this.data=data;

this.next=next;

}

publicNode(){

this(null,null);

}

publicStringtoString(){

returnthis.data.toString();

}

}

(3)CircSinglyLinkedList类

publicclassCircSinglyLinkedListimplementsLList{

publicNodehead;

Override

publicbooleanisEmpty(){

//TODOAuto-generatedmethodstub

returnthis.head.next==null;

}

Override

publicintsize(){

//TODOAuto-generatedmethodstub

Nodetemp=this.head.next;

intcount=0;

while(temp!

=null){

count++;

temp=temp.next;

}

returncount;

}

Override

publicTget(inti){

//TODOAuto-generatedmethodstub

Nodetemp=this.head;

intcount=0;

while(true){

count+=1;

temp=temp.next;

if(count==i){

returntemp.data;

}

}

}

Override

publicvoidset(inti,Tx){

//TODOAuto-generatedmethodstub

Nodetemp=this.head;

intcount=0;

while(true){

count+=1;

temp=temp.next;

if(count==i){

temp.data=x;

}

}

}

Override

publicTremove(inti){

//TODOAuto-generatedmethodstub

intcount=0;

Nodetemp=this.head;

Nodep;

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;

Nodetemp=this.head.next;

for(;temp.next!

=null;temp=temp.next){

i+=1;

if(temp.data.equals(key)){

break;

}

}

returni;

}

Override

publicbooleancontains(Objectkey){

//TODOAuto-generatedmethodstub

Nodetemp=this.head.next;

for(;temp.next!

=null;temp=temp.next){

if(temp.data.equals(key)){

returntrue;

}

}

returnfalse;

}

Override

publicNodeinsertDifferent

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

当前位置:首页 > IT计算机 > 电脑基础知识

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

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