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

定义:

所谓链表就是指在某节点存储数据的过程中还要有一个属性用来指向下一个链表节点,这样的数据存储方式叫做链表

链表优缺点:

优点:易于存储和删除

缺点:查询起来较麻烦

下面我们用java来实现如下链表结构:

首先定义节点类:

package LinkTest;/** * 链表节点类

* @author admin

*

*/publicclass Node {

    privateintvalue;//存储数据privateNode next;//下一个节点/**    * 定义构造器

    * @param vlaue

    * @param value

    */publicNode(int value){

        this.value=value;

    }

    publicint getValue() {

        return value;

    }

    publicvoidsetValue(int value) {

        this.value = value;

    }

    public Node getNext() {

        return next;

    }

    publicvoid setNext(Node next) {

        this.next = next;

    }

}


然后定义一个链表类:

* 注意:遍历链表定义了两个方法,一个是普通方法,一个是递归方法,都可以遍历出来

package LinkTest;/** * 链表

* @author admin

*

*/publicclass Link {

    private Node current;

    private Node root;

    publicvoidinsert(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();

        }

    }

    //普通遍历publicvoid getList(){

        this.current=this.root;

        while(this.current!=null){

            System.out.print(this.current.getValue());

            this.current=this.current.getNext();

            if(this.current!=null){

                System.out.print("------->");

            }

        }

    }

    //递归遍历publicvoid getList2(){

        DG(this.root);

    }

    //递归方法publicvoid DG(Node node){

        System.out.print(node.getValue()+"----->");

        if(node.getNext()!=null){

            DG(node.getNext());

        }else{

            return;

        }

    }

}


测试类:

package LinkTest;/** * 测试类

* @author admin

*

*/publicclass Test {

    publicstaticvoid 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实现了一个简单的链表结构。                                                                                                            欢迎工作一到五年的Java工程师朋友们加入Java群: 891219277

群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 链表(Linked List)是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链...
    盖世英雄_ix4n04阅读 630评论 0 0
  • 一. 认识双向链表 双向链表介绍 单向链表: 我们可以轻松的到达下一个节点, 但是回到钱一个节点是很难的. 但是,...
    小码哥教育520it阅读 15,218评论 0 0
  • 1.HashMap是一个数组+链表/红黑树的结构,数组的下标在HashMap中称为Bucket值,每个数组项对应的...
    谁在烽烟彼岸阅读 1,039评论 2 2
  • 想想军训去了。昨晚哄着念念就一起睡着了。没有人半夜唤我,不用担心她晚上会悄悄刷手机,不会半夜惊醒,于是,终于睡了完...
    fyhong阅读 338评论 3 0
  • 我在深深地夜里写过长长的信 信的末尾没有落款 只有时间年月日分秒时 里面的人不知道过得怎么样 反正写字的人过得不好
    哀慕熙荣阅读 185评论 2 1