java典型数据结构实现与操作.docx
- 文档编号:9229770
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:17
- 大小:33.62KB
java典型数据结构实现与操作.docx
《java典型数据结构实现与操作.docx》由会员分享,可在线阅读,更多相关《java典型数据结构实现与操作.docx(17页珍藏版)》请在冰豆网上搜索。
java典型数据结构实现与操作
实验二典型数据结构实现与操作
一、实验目的
1、设计、实现并测试一系列Java引用类型;
2、训练学习者面向对象高级特性的应用能力,包括类的继承、方法重写、抽象类与接口应用、程序流程控制;
二、实验内容
1、Performer接口,描述一切具有“自我表现”能力的事物,其中至少应提供一个show()方法用于显示当前事物的相关说明信息。
2、Person类,实现Performer接口,描述人员信息及相关操作,包括但不限于人员姓名、年龄等。
3、Book类,实现Performer接口,描述图书信息及相关操作,包括但不限于书号、书名、价格等。
4、抽象类Node,实现Performer接口,描述通用数据节点,其中应封装一整型的数据值value及相关操作功能。
5、LinkedListNode类,继承抽象类Node,描述单向链表节点,在Node数据结构基础上添加一个next属性,以指向其后继节点。
6、TreeNode类 ,继承抽象类Node,描述二叉树节点,在Node数据结构基础上添加lchild及rchild属性,分别用于引用其“左孩子”、“右孩子”节点。
三、实验要求
1、实验前书写实验预习报告;
2、掌握继承、多态、方法重写
3、掌握抽象类、接口
4、了解关键字super、static
5、学会流程控制
6、了解数据结构(链表、二叉树)及相关算法
7、写出实验报告
四、实验学时
8学时
五、实验步骤
1、进入MyEclipse环境,新建一个Java Project;
2、编写实验内容中提到的类;
3、编写TestPerformer类,测试应用程序类,在该类中定义一个测试方法introduce(Performerp),并分别创建与使用Person、Book、LinkedListNode及TreeNode类型对象为实参调用introduce()方法,以验证Java接口与其实现类之间的多态性。
类似地,还可以再定义一个测试方法getInfo(Noden),并分别使用LinkedListNode及TreeNode类型对象为实参调用,以验证Java父类与子类之间的多态性机制。
;
4、编写LinkedListTool类,单向链表工具类,在该类中提供一系列static方法,实现单向链表的常规操作功能,包括但不限于:
构造一个测试用新链表、遍历链表、向链表尾部追加节点、删除链表中符合特定条件的节点(例如删除链表中value属性为某一特定值的所有节点)、链表排序、向有序链表中插入一个新节点(仍保持其有序)、有序链表合并(结果仍为有序链表),并对上述方法进行测试。
5、调试运行程序。
六.实验流程图
Permorfer
B
O
O
K
N
O
D
E
P
E
R
S
O
N
TreeNode
LinkedListNode
七。
实验代码
1、person
importcom、ambow、Performer;
publicclassPersonimplementsPerformer{
privateStringname;
privateintage;
publicPerson(Stringname,intage){
super();
this、name=name;
this、age=age;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this、name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this、age=age;
}
publicvoidshow(){
System、out、println("个人信息,姓名:
"+name+",年龄:
"+age);
}
}
2、book
importcom、ambow、Performer;
publicclassBookimplementsPerformer{
privateStringid;
privateStringname;
privatedoubleprice;
publicBook(Stringid,Stringname,doubleprice){
super();
this、id=id;
this、name=name;
this、price=price;
}
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
this、id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this、name=name;
}
publicdoublegetPrice(){
returnprice;
}
publicvoidsetPrice(doubleprice){
this、price=price;
}
publicvoidshow(){
System、out、println("图书简介,书号:
"+id+",书名:
"+name+",价格:
"+price);
}
}
3、Node
importcom、ambow、Performer;
publicabstractclassNodeimplementsPerformer{
privateintvalue;
publicNode(){
}
publicNode(intvalue){
this、value=value;
}
publicintgetValue(){
returnvalue;
}
publicvoidsetValue(intvalue){
this、value=value;
}
}
4、LinkedListNode
publicclassLinkedListNodeextendsNode{
privateLinkedListNodenext;
publicLinkedListNode(){
super();
}
publicLinkedListNode(intvalue){
super(value);
}
publicLinkedListNode(LinkedListNodenext){
super();
this、next=next;
}
publicLinkedListNode(intvalue,LinkedListNodenext){
super(value);
this、next=next;
}
publicLinkedListNodegetNext(){
returnnext;
}
publicvoidsetNext(LinkedListNodenext){
this、next=next;
}
publicvoidshow(){
System、out、println("单向链表节点,value="+this、getValue());
}
}
5、TreeNode
publicclassTreeNodeextendsNode{
privateTreeNodelchild;
privateTreeNoderchild;
publicTreeNode(){
super();
}
publicTreeNode(TreeNodelchild,TreeNoderchild){
super();
this、lchild=lchild;
this、rchild=rchild;
}
publicTreeNode(intvalue,TreeNodelchild,TreeNoderchild){
super(value);
this、lchild=lchild;
this、rchild=rchild;
}
publicTreeNodegetLchild(){
returnlchild;
}
publicvoidsetLchild(TreeNodelchild){
this、lchild=lchild;
}
publicTreeNodegetRchild(){
returnrchild;
}
publicvoidsetRchild(TreeNoderchild){
this、rchild=rchild;
}
publicvoidshow(){
System、out、println("二叉树节点,value="+this、getValue());
}
}
6、TestPerformer
importcom、ambow、Performer;
publicclassTestPerformer{
publicstaticvoidmain(String[]args){
TestPerformertp=newTestPerformer();
tp、introduce(newPerson("张三",18));
tp、introduce(newBook("ISBN1001","Java核心技术",38、50));
tp、introduce(newLinkedListNode(20,null));
tp、introduce(newTreeNode(33,null,null));
}
publicvoidintroduce(Performerp){
p、show();
}
}
7、LinkedListTool
publicclassLinkedListTool{
publicstaticvoidmain(String[]args){
//创建一个单向链表
LinkedListNodehead=LinkedListTool、createLinkedList();
//遍历它
LinkedListTool、show(head);
//尾部追加节点
LinkedListNoderesult;
result=LinkedListTool、append(head,newLinkedListNode(99,null));
LinkedListTool、show(result);
head=result;
//删除其中符合特定条件的节点
result=LinkedListTool、delete(head,23);
LinkedListTool、show(result);
head=result;
//排序
result=LinkedListTool、sort(head);
LinkedListTool、show(result);
head=result;
//向有序链表中插入一个新节点
result=LinkedListTool、insert(head,newLinkedListNode(88,null));
LinkedListTool、show(result);
head=result;
//合并两个有序链表
LinkedListNodeh2=LinkedListTool、createLinkedList();
result=LinkedListTool、merge(head,h2);
LinkedListTool、show(result);
}
publicstaticLinkedListNodecreateLinkedList(){
int[]a={44,23,45,23,78,33,121,34,322,-76};
intsize=a、length;
LinkedListNodehead=newLinkedListNode(a[size-1]);
for(inti=size-2;i>=0;i--){
head=newLinkedListNode(a[i],head);
}
returnhead;
}
publicstaticvoidshow(LinkedListNodehead){
while(head!
=null){
System、out、print(head、getValue());
head=head、getNext();
if(head!
=null){
System、out、print("->");
}
}
System、out、println("\n---------------------------");
}
publicstaticLinkedListNodeappend(LinkedListNodehead,LinkedListNodenew_p){
if(head==null){
head=new_p;
}else{
//p指向当前头节点(非空的)
LinkedListNodep=head;
//n为p的后继节点(可能为空值)
LinkedListNoden=p、getNext();
while(n!
=null){
p=n;
n=p、getNext();
}
p、setNext(new_p);
}
returnhead;
}
publicstaticLinkedListNodedelete(LinkedListNodehead,intv){
//删除链表开头的连续多个节点
while(head!
=null&&head、getValue()==v){
head=head、getNext();
}
//head要保留
if(head!
=null){//删除符合条件中间节点
//p指向当前头节点(非空的)
LinkedListNodep=head;
//n为p的后继节点(可能为空值)
LinkedListNoden=p、getNext();
while(n!
=null){
if(n、getValue()==v){//该删除
p、setNext(n、getNext());
}else{
p=n;
}
n=p、getNext();
}
}
returnhead;
}
publicstaticLinkedListNodesort(LinkedListNodehead){
//创建一个空的结果链表
LinkedListNodenhead=null;
//依次取出源链表中的每一个节点,并将之插入到有序的结构链表中,仍然结构链表的有序状态
while(head!
=null){
LinkedListNodec=head;
head=head、getNext();
c、setNext(null);
//将c这个节点插入到有序的链表nhead中
nhead=LinkedListTool、insert(nhead,c);
}
returnnhead;
}
//向有序链表中插入一个新节点,并保持其有序状态
publicstaticLinkedListNodeinsert(LinkedListNodehead,LinkedListNoden){
//如果目标链表为空链表
if(head==null){
head=n;
}elseif(n、getValue()
n、setNext(head);
head=n;
}else{
LinkedListNodep1=head;
LinkedListNodep2=head、getNext();
while(p2!
=null&&n、getValue()>p2、getValue()){
p1=p2;
p2=p2、getNext();
}
//将n节点插入到p1与p2两个节点之间
p1、setNext(n);
n、setNext(p2);
}
returnhead;
}
publicstaticLinkedListNodemerge(LinkedListNodehead1,LinkedListNodehead2){
head2=LinkedListTool、sort(head2);
//依次取出head1链表中的每一个节点,并将之插入到有序的head2链表中,仍然结构链表的有序状态
while(head1!
=null){
LinkedListNodec=head1;
head1=head1、getNext();
c、setNext(null);
//将c这个节点插入到有序的链表nhead中
head2=LinkedListTool、insert(head2,c);
}
returnhead2;
}
}
8、TreeTool
ublicclassTreeTool{
publicstaticvoidmain(String[]args){
TreeNoderoot=TreeTool、createLinkedList();
TreeTool、list(root);
}
//构建一颗二叉树
publicstaticTreeNodecreateLinkedList(){
TreeNoden1=newTreeNode(9,null,null);
TreeNoden2=newTreeNode(12,null,null);
TreeNoden3=newTreeNode(49,n1,n2);
TreeNoden4=newTreeNode(54,null,null);
TreeNoden5=newTreeNode(37,n3,n4);
returnn5;
}
//遍历二叉树(中序遍历)
publicstaticvoidlist(TreeNoderoot){
if(root!
=null){
list(root、getLchild());
System、out、println(root、getValue());
list(root、getRchild());
}
}
//遍历二叉树(前序遍历)
publicstaticvoidlist2(TreeNoderoot){
if(root!
=null){
System、out、println(root、getValue());
list2(root、getLchild());
list2(root、getRchild());
}
}
//遍历二叉树(后序遍历)
publicstaticvoidlist3(TreeNoderoot){
if(root!
=null){
list3(root、getLchild());
list3(root、getRchild());
System、out、println(root、getValue());
}
}
}
八.实验效果图
1、输入年份
2、万年历的实现
九.结果分析:
以上程序运行结果不对,缺少符号等,存在的问题及解决方法:
由于在MyEclipse中打代码,很多细节容易忽视,因此在MyEclipse中打完代码,还需运行一次,避免错误的出现。
十.实验体会与总结:
对于代码的学习,我们应注重细节,不能忽视极小的字母与符号的顺序与格式,多敲代码,有益编程。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 典型 数据结构 实现 操作