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