数据结构(二)之链表

时间:2019-04-18
本文章向大家介绍数据结构(二)之链表,主要包括数据结构(二)之链表使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.单链表

package cn.liusong.Array;

public class Node {
    int data;
    Node next;

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

    //为节点追加节点
    public Node append(Node node) {
        //当前节点
        Node currentNode = this;
        //循环向后找
        while (true) {
            //取出下一个节点
            Node nextNode = currentNode.next;
            //如果下一个节点为null,当前节点为最后的节点
            if (nextNode == null) {
                break;
            }
            //赋给当前节点
            currentNode = nextNode;
        }
        //把需要追加的节点到当前找到的节点的下一个节点
        currentNode.next = node;
        return this;
    }

    public Node next() {
        return this.next;
    }

    public void show() {
        Node currentNode = this;
        while (true) {
            System.out.print(currentNode.data + "->");
            Node newNext = currentNode.next;
            currentNode = newNext;
            if (currentNode == null) {
                System.out.print("null");
                System.out.println(" ");
                break;
            }
        }
    }

    //删除节点
    public void removeNext() {
        //  取出下下一个节点信息
        Node newNext = next.next;
        //把下下一个节点的信息给当前节点
        next = newNext;
    }

    //取出节点数据
    public int getData() {
        return this.data;
    }

    //插入一个新节点
    public void after(Node node) {
        //取出下一个节点作为下下一个节点
        Node nextNext = next;
        //插入新的节点作为下一个节点
        this.next = node;
        //取出下下一个节点连在下一个节点之后
        node.next = nextNext;
    }
}

  测试链表功能

package cn.liusong.Array;

public class TestNode {
    public static void main(String [] args){
        //创建节点
        Node n1 = new Node(1) ;
        Node n2 = new Node(2) ;
        Node n3 = new Node(3) ;
        Node n4 = new Node(4) ;
        Node n5 = new Node(5) ;
        //追加节点
        n1.append(n2).append(n3).append(n4);
        //显示节点链
        n1.show() ;
        //取出节点
      // System.out.println(n1.next().next().getData());
        n1.next.removeNext();
        n1.show();
        n1.after(n5);
        n1.show();
    }
}

测试结果

2.双链表

package cn.liusong.Array;

public class DoubleNode {
    DoubleNode pre = this ;
    DoubleNode next = this ;
    int data ;

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

    //增加一个节点
    public void after(DoubleNode node){
        //当前节点的下一个节点作为下下一个节点
        DoubleNode nextNext = next ;
        //把新的节点作为当前节点的下一个节点
        this.next = node ;
        //把当前节点作为新节点的前一个节点
        node.pre = this ;
        //把下下一个节点作为新节点的后一个节点
        node.next = nextNext ;
        //让原来的下一个节点作为上一个节点的新节点
        nextNext.pre = node ;
    }

    //下一个节点
    public DoubleNode getNext(){
        return this.next;
    }
    //获取上一个节点
    public DoubleNode getPre(){
        return this.pre ;
    }
    //获取数据
    public int getData(){
        return  this.data ;
    }
}

测试双链表

package cn.liusong.Array;

public class TestDoubleNode {
    public static void main(String[] args){
        DoubleNode n1 = new DoubleNode(1) ;
        DoubleNode n2 = new DoubleNode(2) ;
        DoubleNode n3 = new DoubleNode(3) ;
        //追加节点
        n1.after(n2);
        n2.after(n3);
        //查看上一个,下一个和自己节点的内容
        System.out.println(n2.pre.getData());
        System.out.println(n2.getData());
        System.out.println(n2.next.getData());
        System.out.println(n3.next.getData());
        System.out.println(n1.pre.getData());
    }
}

结果

循环链表

package cn.liusong.Array;

public class LoopNode {
    int data;
    LoopNode next = this;

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

    //删除节点
    public void removeNext() {
        //  取出下下一个节点信息
        LoopNode newNext = next.next;
        //把下下一个节点的信息给当前节点
        next = newNext;
    }

    //取出节点数据
    public int getData() {
        return this.data;
    }

    //插入一个新节点
    public void after(LoopNode node) {
        //取出下一个节点作为下下一个节点
        LoopNode nextNext = next;
        //插入新的节点作为下一个节点
        this.next = node;
        //取出下下一个节点连在下一个节点之后
        node.next = nextNext;
    }
}

测试循环链表

package cn.liusong.Array;

public class TestLoopNode {
    public static void main(String[] args){
        LoopNode n1 = new LoopNode(1) ;
        LoopNode n2 = new LoopNode(2) ;
        LoopNode n3 = new LoopNode(3) ;
        LoopNode n4 = new LoopNode(4) ;
        LoopNode n5 = new LoopNode(5) ;
        n1.after(n2);
        n2.after(n3);
        n3.after(n4);
        n4.after(n5);
        System.out.println(n1.next.getData());
        System.out.println(n2.next.getData());
        System.out.println(n3.next.getData());
        System.out.println(n4.next.getData());
        System.out.println(n5.next.getData());
    }
}

测试结果