利用java实现一个简单的链表结构

上传人:m**** 文档编号:65293827 上传时间:2018-12-31 格式:DOCX 页数:4 大小:21.23KB
返回 下载 相关 举报
利用java实现一个简单的链表结构_第1页
第1页 / 共4页
利用java实现一个简单的链表结构_第2页
第2页 / 共4页
利用java实现一个简单的链表结构_第3页
第3页 / 共4页
利用java实现一个简单的链表结构_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《利用java实现一个简单的链表结构》由会员分享,可在线阅读,更多相关《利用java实现一个简单的链表结构(4页珍藏版)》请在金锄头文库上搜索。

1、利用java实现一个简单的链表结构定义:所谓链表就是指在某节点存储数据的过程中还要有一个属性用来指向下一个链表节点,这样的数据存储方式叫做链表链表优缺点:优点:易于存储和删除缺点:查询起来较麻烦下面我们用java来实现如下链表结构:首先定义节点类:package LinkTest;/* * 链表节点类 * author admin * */public class Node private int value;/存储数据 private Node next;/下一个节点 /* * 定义构造器 * param vlaue * param value */ public Node(int valu

2、e) this.value=value; public int getValue() return value; public void setValue(int value) this.value = value; public Node getNext() return next; public void setNext(Node next) this.next = next; 然后定义一个链表类:* 注意:遍历链表定义了两个方法,一个是普通方法,一个是递归方法,都可以遍历出来package LinkTest;/* * 链表 * author admin * */public class

3、 Link private Node current; private Node root; public void insert(int vlaue) Node newNode=new Node(vlaue); if(this.current=null) this.current=newNode; this.root=this.current; else this.current.setNext(newNode); this.current=this.current.getNext(); /普通遍历 public void getList() this.current=this.root;

4、while(this.current!=null) System.out.print(this.current.getValue(); this.current=this.current.getNext(); if(this.current!=null) System.out.print(-); /递归遍历 public void getList2() DG(this.root); /递归方法 public void DG(Node node) System.out.print(node.getValue()+-); if(node.getNext()!=null) DG(node.getNext(); else return; 测试类:package LinkTest;/* * 测试类 * author admin http:/f-1.cc * */public class Test public static void main(String args) Link l=new Link(); l.insert(1); l.insert(4); l.insert(5); l.insert(6); l.insert(9); l.insert(8); l.getList(); 测试类运行结果:1-4-5-6-9-8这样我们就用java实现了一个简单的链表结构。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > Java

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号