美文网首页
java 链表

java 链表

作者: liust15 | 来源:发表于2018-04-16 12:32 被阅读0次

实例地址

  • 一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据
  • 而是在每一个节点里存到是下一个节点的指针(pointer)
  • 链表与数组:线性数据结构
  • 数组适合查找,遍历,固定长度
  • 链表适合插入,删除,不宜过长,否则遍历性能下降

/**
 * @program: lianbiao
 * @description: 链表
 * @author: liust
 * @create: 2018-04-16 09:45
 **/
public class Main {
    public static void main(String[] args) {
        NodeManager nodeManager = new NodeManager();
        System.out.println("--------------add-----------------");
        nodeManager.add(5);
        nodeManager.add(4);
        nodeManager.add(3);
        nodeManager.add(2);
        nodeManager.add(1);
        nodeManager.add(0);
        nodeManager.print();
        System.out.println("---------------del----------------");
        nodeManager.del(4);
        nodeManager.print();
        System.out.println("---------------find----------------");
        System.out.println(nodeManager.find(4));
        System.out.println(nodeManager.find(3));

        System.out.println("---------------update----------------");
        nodeManager.update(2, 10);
        nodeManager.print();
        System.out.println("---------------install----------------");
        nodeManager.insert(0, 20);
        nodeManager.print();
    }
}

class NodeManager {
    private Node root;//根节点
    private int currentIndex = 0;//节点的序号,每次操作从0开始

    //添加节点
    public void add(int data) {
        if (root == null) {
            root = new Node(data);
        } else {
            root.addNode(data);
        }
    }

    //删除节点
    public void del(int data) {
        if (root == null) return;
        if (root.getData() == data) {
            root = root.next;
        } else {
            root.delNode(data);
        }
    }

    //输出所有节点
    public void print() {
        if (root != null) {
            System.out.println(root.getData() + "->");
            root.printNode();
            System.out.println();
        }
    }

    //查找节点是否存在
    public boolean find(int data) {
        if (root == null) return false;
        if (root.getData() == data) {
            return true;
        } else {
            return root.findNode(data);
        }
    }

    //修改节点
    public boolean update(int oldData, int newData) {
        if (root == null) return false;
        if (root.getData() == oldData) {
            root.setData(newData);
            return true;
        } else {
            return root.updateNode(oldData, newData);
        }
    }

    //插入节点想索引之前插入
    public void insert(int index, int data) {
        if (index < 0) return;
        currentIndex = 0;
        if (index == currentIndex) {
            Node newNode = new Node(data);
            newNode.next = root;
            root = newNode;
        } else {
            root.insertNode(index, data);
        }
    }

    private class Node {//谁有数据,谁提供方法
        private int data;
        private Node next;//把当前类型作为属性

        public Node(int data) {
            this.data = data;
        }

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }

        //添加节点
        public void addNode(int data) {
            if (this.next == null) {
                this.next = new Node(data);
            } else {
                this.next.addNode(data);
            }
        }

        //删除节点
        public void delNode(int data) {
            if (this.next != null) {
                this.next = this.next.next;
            } else {
                this.next.delNode(data);
            }
        }

        //输出所有节点
        public void printNode() {
            if (this.next != null) {
                System.out.println(this.next.data + "->");
                this.next.printNode();
            }

        }

        //查找节点
        public boolean findNode(int data) {
            if (this.next != null) {
                if (this.next.data == data) {
                    return true;
                } else {
                    return this.next.findNode(data);
                }
            }
            return false;
        }

        //修改节点
        public boolean updateNode(int oldData, int newData) {
            if (this.next != null) {
                if (this.next.data == oldData) {
                    this.next.data = newData;
                    return true;
                } else {
                    return this.next.updateNode(oldData, newData);
                }
            }
            return false;
        }

        //插入节点
        public void insertNode(int index, int data) {
            currentIndex++;
            if(index==currentIndex){
                Node newNode=new Node(data);
                newNode.next=this.next;
                this.next=newNode;
            }else {
                this.next.insertNode(index,data);
            }
        }
    }
}

实例地址https://github.com/liust15/java-node

相关文章

  • 链表

    单链表 C实现 Java实现 双链表 C实现 Java实现

  • 链表

    一、单向链表 单向链表的普通实现 Java实现: Kotlin实现: 单向链表的递归实现 Java实现: 二、双向...

  • 15反转链表

    题目描述 输入一个链表,反转链表后,输出新链表的表头。 Java实现

  • 数据结构 | 其二 链表

    冰河winner - 数据结构之链表 2.1 单向链表 数据结构(一) 单链表的实现-JAVA 2.2 双端链表 ...

  • LeetCode2

    用链表表示整数,求其相加得到的结果。考察基本的链表操作。因为用的是Java刷题,所以要清楚Java的链表实现。Ja...

  • HashMap

    HashMap 解决Hash冲突 java 中的HashMap 通过链表法解决Hash冲突 链表法 链表法就是将相...

  • 单向链表

    链表本身也是线性表,只不过物理存储上不连续 Node.java:结点类 LinkList.java:单向链表类(核...

  • 链表面试题Java实现【重要】

    链表面试题Java实现【重要】

  • 队列

    基于数组的循环队列 Java实现 基于链表的队列实现 Java实现

  • 单向链表反转(含图解)

    前言 上次讲解了单向链表的原理《Java实现单向链表功能》,今天拓展一下实现链表的翻转。下面直接上代码。 链表初始...

网友评论

      本文标题:java 链表

      本文链接:https://www.haomeiwen.com/subject/eraskftx.html