Java泛型实现单链表.docx
《Java泛型实现单链表.docx》由会员分享,可在线阅读,更多相关《Java泛型实现单链表.docx(12页珍藏版)》请在冰豆网上搜索。
![Java泛型实现单链表.docx](https://file1.bdocx.com/fileroot1/2022-12/29/500a94c9-9359-40c3-a0e0-f58eb5091bae/500a94c9-9359-40c3-a0e0-f58eb5091bae1.gif)
Java泛型实现单链表
学号11710115
天津城建大学
Java语言程序设计C
实验报告
实验3:
泛型实现链表
学生姓名
路江飞
班级
11卓越七班
一、实验内容
1.掌握使用Java语言进行结构化程序设计;
2.熟悉Java泛型。
3.熟悉Eclipse开发环境,编写简单的Application程序,并编译和执行。
二、实验要求
1.调试程序、编译,运行后得到正确的结果;
2.写出实验报告,要求记录编译和执行Java程序当中的系统错误信息提示,并给出解决办法。
三、实验结果
文件1:
package_List;
classNode{
Ee;
Nodenext;
Node(Ee){
this.e=e;
next=null;
}
}
文件2:
package_List;
publicclassShow{
voidprint(){
System.out.println("*************************************");
System.out.println("*1.按位查找*");
System.out.println("*2.按值查找*");
System.out.println("*3.插入*");
System.out.println("*4.按位删除*");
System.out.println("*5.按值删除*");
System.out.println("*6.修改*");
System.out.println("*7.遍历*");
System.out.println("*8.查看链表长度*");
System.out.println("*9.退出*");
System.out.println("*************************************");
}
}
文件3:
package_List;
importjava.util.Scanner;
publicclassList{
Nodehead;
List(){
this.head=null;
}
List(Ee[],intn){//构造函数
this.head=newNode(e[0]);
Noder=this.head;
for(inti=1;iNodes=newNode(e[i]);
r.next=s;
r=s;
}
r.next=null;
}
voidadd(Ee){//按位置插入
intindex,b=1;
Scannerinput=newScanner(System.in);
for(intj=0;b==1;j++){
System.out.print("请输入插入的位置(第一个数据之前是0号位置,以此类推):
");
try{
index=input.nextInt();
}
catch(Exceptiona){
System.out.println("输入错误!
");
continue;
}
if(index>this.Length()||index<0){
System.out.println("插入位置错误!
");
continue;
}
if(index==0){
Nodes=newNode(e);
s.next=this.head;
this.head=s;
b=0;
}
else{
inti=1;
Nodep=this.head;
while(ip=p.next;
i++;
}
Nodes=newNode(e);
s.next=p.next;
p.next=s;
b=0;
}
}
}
voiddelete(){//按位置删除
intindex,b=1;
for(intj=0;b==1;j++){
Scannerinput=newScanner(System.in);
System.out.print("请输入删除的位置(第一个数据是1号位置,以此类推):
");
try{
index=input.nextInt();
}
catch(Exceptione){
System.out.println("输入错误!
");
continue;
}
if(index>this.Length()||index<=0){
System.out.println("删除位置错误!
");
continue;
}
Nodep=this.head;
inti=1;
if(index==1){
this.head=p.next;
b=0;
}
else{
while(ip=p.next;
i++;
}
p.next=p.next.next;
b=0;
}
}
}
voiddelete(Ee){//按值删除
Nodep=this.head;
Nodeq=null;
if(e.equals(p.e)){//处理数据在1号位置的特殊情况
this.head=p.next;
}
else{
while(p!
=null){
if(e.equals(p.e)){
q.next=p.next;
break;
}
q=p;//p为要删除数据的前一个
p=p.next;
}
}
if(p==null){
System.out.println("删除的数据不存在!
");
}
}
voidSet(Ea){//按位置修改
intindex,b=1;
Scannerinput=newScanner(System.in);
for(intj=0;b==1;j++){
System.out.print("请输入修改的位置:
");
try{
index=input.nextInt();
}
catch(Exceptione){
System.out.println("输入错误!
");
continue;
}
inti=1;
Nodep=this.head;
if(index<=0||index>this.Length()){
System.out.println("修改位置错误!
");
continue;
}
while(ip=p.next;
i++;
}
p.e=a;
b=0;
}
}
intLength(){//链表长度
intcount=0;
Nodep=this.head;
while(p!
=null){
count++;
p=p.next;
}
returncount;
}
voidGet(){//按位查找
intindex,b=1;
for(intj=0;b==1;j++){
Scannerinput=newScanner(System.in);
System.out.print("请输入查询的位置:
");
try{//检测异常
index=input.nextInt();
}
catch(Exceptione){//捕捉异常
System.out.println("输入错误!
");
continue;
}
inti=1;
Nodep=this.head;
if(index>this.Length()||index<=0){
System.out.println("查找位置错误!
");
continue;
}
while(ip=p.next;
i++;
}
System.out.println("第"+index+"个元素为:
"+p.e.toString());
b=0;
}
}
voidGet1(Ee){//按值查找
Nodep=this.head;
intindex=1;
for(inti=0;p!
=null;i++){
if(e.equals(p.e)){//不能写成p.e==e和s1==s2
System.out.println(p.e.toString()+"是第"+index+"个数!
");
break;
}
else{
p=p.next;
index++;
}
}
if(p==null)
System.out.println("所找的数据不存在!
");
}
voidprintlist(){//遍历
Nodep=this.head;
if(p==null){
System.out.println("链表没有元素");
}
else{
while(p.next!
=null){
System.out.print(p.e.toString()+"");
p=p.next;
}
System.out.println(p.e.toString());
}
}
}
文件4:
package_List;
importjava.util.Scanner;
publicclassMainClass{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Stringstr[]={"12","43","54","23","abc"};
intlenth=str.length;
intk=1,t=0;
Lista=newList(str,lenth);//初始化链表
Showshow=newShow();
show.print();
for(inti=0;k==1;i++){
System.out.print("请选择您的操作:
");
try{
Scannerinput=newScanner(System.in);
t=input.nextInt();
}
catch(Exceptione){
System.out.println("输入错误!
");
continue;
}
Scannerinput1=newScanner(System.in);
switch(t){
case1:
a.Get();
break;
case2:
System.out.print("请输入您要查找的数据:
");
Stringh=input1.nextLine();
a.Get1(h);
break;
case3:
System.out.print("请输入您要插入的数据:
");
Stringm=input1.nextLine();
a.add(m);
break;
case4:
a.delete();
break;
case5:
System.out.print("请输入您要删除的数据:
");
Strings=input1.nextLine();
a.delete(s);
break;
case6:
System.out.print("请输入您要修改后的数据:
");
Stringn=input1.nextLine();
a.Set(n);
break;
case7:
a.printlist();
break;
case8:
System.out.println("链表的长度:
"+a.Length());
Break;
case9:
k=0;
break;
default:
System.out.println("输入错误!
");
break;
}
}
}
}
运行结果: