Java实现链表

链表是一种很常见的数据结构,相比与顺序表它们两种结构都是存储数据的。

顺序表查找数据的速度更加迅速,更加方便那么链表存在的意义又是什么?

 这是链表实现的逻辑图,每一个data和next的整体就是一个节点也就是Node,每个Node的next又包含了指向下一个Node的地址值就这样像一个链子一样将所有数据都串了起来。看到这个图你就会发现链表增删数据会很方便只需要将Node的next值更改就行了

比如现在有个新的节点newNode,现在想将他插入head和1号结点之前我们首先得将newNode的next指向一号结点然后将head的next指向newNode这样才是插入成功

若是先将 head的next直接指向newNode那我们将无法找到1号结点的地址值了,因为你的第一步操作已经将含有1号结点的地址值的重新赋值了,了解了这个之后就方便了解链表的其他操作了

public class LinkList {              //创建链表的类
    public int val;                  //设置结点内的数据类型
    public ListNode next;            //设置下一个结点
    

    public static class ListNode{    //创建一个内部类这个结点的类
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
            this.next = null;
        }
        public void addNode(int val){            //设置添加结点的方法
            ListNode newNode = new ListNode(val);//创建一个新的结点
            if(this.next == null){               //判断是否为空
                this.next = newNode;
            }else {
                ListNode current = this.next;    //跳过头结点
                while(current.next!=null){
                    current = current.next;      //直至找到某一个结点的next值为空的结点
                }
                current.next = newNode;          
            }
        }
    }
}

首先这个代码创建了一个链表的类又在内部创建了一个链表结点的类 因为链表就是由一个个结点组成,这个ListNode的前缀也可以不加static可在外部使用这个类。后续又添加了添加结点的操作

    public void InitList(int N) {                    //初始化链表的操作
        ListNode current = null;                 //初始化当前结点为null
        for (int i = 1; i <= N; i++) {           //循环创建N个结点
            ListNode newNode = new ListNode(i);  //创建一个新的结点值为i;
            if (this.next == null) {             //判断当前结点是否为空
                this.next = newNode;             //将新的结点设置为链表的第一个结点
                current = newNode;
            } else {    
                if (current != null) {
                    current.next = newNode;      //如果不为空将当前结点的next指向新的结点
                }
                current = newNode;               //更新当前结点为新的结点
            }
        }
        // 如果当前节点不为 null,将其 next 指针设置为 null,表示链表结束
        if (current != null) {
            current.next = null;
        }
    }
    public void PrintList(){
        //this表示调用的地址值  this.next表示第一个有值的位置
        ListNode current = this.next;        
        while (current!=null){                    //循环直至指向空结点结束
            if(current.next==null){               //方便打印观察
                System.out.print(current.val);
                break;
            }else {
                System.out.print(current.val+"->");
                current = current.next;
            }
        }
        System.out.println();
    }

我们实现初始化链表的方法是尾插法 我们不断更新current使它一直指向链表的尾部方便添加新的结点。

 下列是执行的调用的一个小示例:

import tool.LinkList;

public class Test1{
    public static void main(String[] args) {
        LinkList list = new LinkList();
        list.InitList(5);
        list.PrintList();
    }
}

 

以上就已经成功实现了链表的初始化和打印。

 之后就是插入和删除的操作,插入的操作我们已经了解了,删除只需要跳过我们需要删除的结点的地址值就行了也就是我们需要得到需要删除结点的前一个结点,将前一个结点的next指向需要删除的next这样就删除了指向我们需要删除的结点的地址值了。

    public void InsertValue(int index,int val){
        //初始化当前结点为链表的第一个结点
        ListNode current = this.next;
        if(index==1){
            //创建一个新的结点,值为val
            ListNode newNode = new ListNode(val);
            //将新结点的next指向当前链表的第一个结点
            newNode.next = this.next;
            //将链表的第一个结点更新为新结点
            this.next = newNode;
        }else {
            //遍历链表找到插入位置的前一个结点
            for (int i = 1; i <= index-2; i++) {
                current = current.next;
            }
            //创建一个新的结点,值为val
            ListNode newNode = new ListNode(val);
            //将新结点的next指向当前结点的下一个结点
            newNode.next = current.next;
            current.next = newNode;
        }
    }
    public void removeValue(int index){
        //初始化current结点
        ListNode current = this.next;
        if(index == 1){
            //将链表的第一个结点更新为当前结点的下一个结点
            this.next = current.next;
            //将当前结点的next设置为null,断开与链表的连接
            current.next = null;
        }else{
            //遍历列表,找到需要删除的前一个结点
            for (int i = 1; i <= index-2; i++) {
                current = current.next;
            }
            //获取需要删除的结点
            ListNode p;
            p = current.next;
            //将当前结点的next指向需要删除结点的下一个结点
            current.next = p.next; 
            //断开链表的连接
            p.next = null;
        }
    }

以上就已经是链表的基本操作了

下列是实现的示例:

import tool.LinkList;

public class Test1{
    public static void main(String[] args) {
        LinkList list = new LinkList();
        System.out.println("初始链表数据");
        list.InitList(8);
        list.PrintList();
        System.out.println("插入后的链表数据");
        list.InsertValue(3,66);
        list.PrintList();
        System.out.println("删除数据后的链表");
        list.removeValue(6);
        list.PrintList();

    }
}

 

 下列是全部代码

public class LinkList {
    public int val;
    public ListNode next;

    public static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
            this.next = null;
        }
        public void addNode(int val){
            ListNode newNode = new ListNode(val);
            if(this.next == null){
                this.next = newNode;
            }else {
                ListNode current = this.next;
                while(current.next!=null){
                    current = current.next;
                }
                current.next = newNode;
            }
        }
    }
    public void InitList(){
        this.val = 0;
        this.next = null;
    }
    public void addNode(int val){
        ListNode newNode = new ListNode(val);
        if(this.next == null){
            this.next = newNode;
        }else {
            ListNode current = this.next;
            while(current.next!=null){
                current = current.next;
            }
            current.next = newNode;
        }
    }
    public void InitList(int N) {
        ListNode current = null;
        for (int i = 1; i <= N; i++) {
            ListNode newNode = new ListNode(i);
            if (this.next == null) {
                this.next = newNode;
                current = newNode;
            } else {
                if (current != null) {
                    current.next = newNode;
                }
                current = newNode;
            }
        }
        if (current != null) {
            current.next = null;
        }
    }
    public void PrintList(){
        //this表示调用的地址值  this.next表示第一个有值的位置
        ListNode current = this.next;
        while (current!=null){
            if(current.next==null){
                System.out.print(current.val);
                break;
            }else {
                System.out.print(current.val+"->");
                current = current.next;
            }
        }
        System.out.println();
    }
    public void InsertValue(int index,int val){
        ListNode current = this.next;
        if(index==1){
            ListNode newNode = new ListNode(val);
            newNode.next = this.next;
            this.next = newNode;
        }else {
            for (int i = 1; i <= index-2; i++) {
                current = current.next;
            }
            ListNode newNode = new ListNode(val);
            newNode.next = current.next;
            current.next = newNode;
        }
    }
    public void removeValue(int index){
        ListNode current = this.next;
        if(index == 1){
            this.next = current.next;
            current.next = null;
        }else{
            for (int i = 1; i <= index-2; i++) {
                current = current.next;
            }
            ListNode p;
            p = current.next;
            current.next = p.next;
            p.next = null;
        }
    }

感谢你看到这里若代码有任何错误或是无法成功运行请留言

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值