链表的操作1

链表

1、结点的定义

每个结点除了可以保存数据之外还必须有对下一个结点的引用,结点就是数据的载体

package lianbiao;

public class Node<T> {
    private T data;
    private Node nextNode;
    public Node() {
    }
    public Node(T data) {
        this.data = data;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
    public Node getNextNode() {
        return nextNode;
    }
    public void setNextNode(Node nextNode) {
        this.nextNode = nextNode;
    }
}

package lianbiao;

public class Test2 {
    public static void main(String[] args) {
        Node<String> root = new Node<>("one");
        Node<String> n1 = new Node<>("two");
        Node<String> n2 = new Node<>("three");
        Node<String> n3 = new Node<>("four");
        //维护结点之间的关系
        root.setNextNode(n1);
        n1.setNextNode(n2);
        n2.setNextNode(n3);
        print(root);
        //使用循环输出节点数据
//        Node<String> currentNode = root;
//        while (currentNode!=null){
//            System.out.print(currentNode.getData()+">>");
//            currentNode = currentNode.getNextNode();
//        }

    }

    /**
     * 利用递归的方式输出链表数据
     * @param currentNode
     */
    public static void print(Node currentNode){
        if (currentNode==null){
            //当为空指向时就不执行函数了,递归的截止条件
            return;
        }
        System.out.print(currentNode.getData()+">>");
        print(currentNode.getNextNode());
    }

}

2、内部类的方式
package lianbiao;
public class Link<T> {
    private Node<T> root;//链表的根节点
    private int foot;//表示链表结点的索引
    private class Node<T>{
        private T data;
        private Node nextNode;
        public Node() {
        }
        public Node(T data) {
            this.data = data;
        }
        public T getData() {
            return data;
        }
        public void setData(T data) {
            this.data = data;
        }
        public Node getNextNode() {
            return nextNode;
        }

        public void setNextNode(Node nextNode) {
            this.nextNode = nextNode;
        }
        /**
         * 添加结点
         */
        public void addNode(Node<T> newNode){
            if (this.getNextNode()==null){
                this.setNextNode(newNode);
            }else {
                this.getNextNode().addNode(newNode);
            }
        }
        /**
         * 输出结点数据
         */
        public void printNode(){
            System.out.println(this.data);
            if (this.nextNode!=null){
                this.nextNode.printNode();
            }
        }
    }
    //在外部类定义主题能够访问的方法,从而调用内部类的方法

    //添加结点
    public void add(T data){
        Node<T> newNode = new Node<>(data);
        if (this.root==null){
            this.root = newNode;
        }else {
            this.root.addNode(newNode);
        }
    }
    //输出数据
    public void print(){
        if (this.root == null){
            return;
        }else {
            this.root.printNode();
        }
    }
}

package lianbiao;

public class Test2 {
    public static void main(String[] args) {
       Link<String> link = new Link<>();
       //添加数据
       link.add("火车头");
       link.add("1车厢");
       link.add("2车厢");
       link.add("3车厢");
       link.add("4车厢");
       link.add("车尾");
       //输出数据
       link.print();

    }

}

在这里插入图片描述
将节点内定义内部类是为了:
让客户端代码变得简洁,将节点的创建以及节点之间的关系维护封装隐藏
让数据更安全,因为其他的类不能直接访问节点类了,只能通过外部类类访问

在外部类中添加判断链表是否为空和统计链表大小的方法
package lianbiao;
public class Link<T> {
    private Node<T> root;//链表的根节点
    private int foot;//表示链表结点的索引
    private int count;
    private class Node<T>{
        private T data;
        private Node nextNode;
        public Node() {
        }
        public Node(T data) {
            this.data = data;
        }
        public T getData() {
            return data;
        }
        public void setData(T data) {
            this.data = data;
        }
        public Node getNextNode() {
            return nextNode;
        }

        public void setNextNode(Node nextNode) {
            this.nextNode = nextNode;
        }
        /**
         * 添加结点
         */
        public void addNode(Node<T> newNode){
            if (this.getNextNode()==null){
                this.setNextNode(newNode);
            }else {
                this.getNextNode().addNode(newNode);
            }
        }
        /**
         * 输出结点数据
         */
        public void printNode(){
            System.out.println(this.data);
            if (this.nextNode!=null){
                this.nextNode.printNode();
            }
        }
    }
    //在外部类定义主题能够访问的方法,从而调用内部类的方法

    //添加结点
    public void add(T data){
        Node<T> newNode = new Node<>(data);//创建结点对象
        if (this.root==null){
            this.root = newNode;
        }else {
            this.root.addNode(newNode);
        }
        count++;
    }
    //输出数据
    public void print(){
        if (this.root == null){
            return;
        }else {
            this.root.printNode();
        }
    }
    //判断结点的长度,或者结点的个数
    public int size(){
        return this.count;
    }
    //判断结点是否为空
    public boolean isEmpty(){
        return this.count==0;
    }

}
package lianbiao;
public class Test2 {
    public static void main(String[] args) {
       Link<String> link = new Link<>();
       //添加数据
       link.add("火车头");
       link.add("1车厢");
       link.add("2车厢");
       link.add("3车厢");
       link.add("4车厢");
       link.add("车尾");
       //输出数据
       link.print();
       System.out.println(link.size());
       System.out.println(link.isEmpty());
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值