zoukankan      html  css  js  c++  java
  • Java学习之链表

    数据结构学了,java实现下

    package com.gh.Link;
    /**
     * 链表的实现
     * @author ganhang
     *
     */
    public class Links {
        public static void main(String[] args) {
            NodeManage nm=new NodeManage();
            nm.add("节点1");
            nm.add("节点2");
            nm.add("节点3");
            nm.add("节点4");
            nm.add("节点5");
            nm.add("节点6");
            nm.add("节点7");
            nm.add("节点8");
            nm.add("节点9");
            nm.add("节点10");
            nm.print();
            nm.delete("节点3");
            nm.print();
            }
    }
    package com.gh.Link;
    /**
     * 链表管理(增删查)
     * @author ganhang
     *
     */
    public class NodeManage {
        private Node root;
    
        public void add(String name) {
            if (root == null) {
                root = new Node(name);
            } else {
                root.addNode(name);
            }
        }
    
        public void delete(String name) {
            if (root != null) {
                if (root.name.equals(name)) {
                    root = root.next;
                } else {
                    root.delNode(name);
                }
            }
        }
    
        public void print() {
            if(root!=null){
                System.out.print(root.name);
                root.printNode();
                System.out.println();
            }
        }
    
        class Node {//内部类实现根节点下节点的操作
            private String name;
            private Node next;
    
            public Node(String name) {
                this.name = name;
            }
    
            public void addNode(String name) {
                if (this.next == null) {
                    this.next = new Node(name);
                } else {
                    this.next.addNode(name);
                }
            }
    
            public void delNode(String name) {
                if (this.next != null) {
                    if (this.next.name.equals(name)) {
                        this.next = this.next.next;
                    } else {
                        this.next.delNode(name);
                    }
                }
            }
            public void printNode() {
                if(this.next!=null){
                    System.out.print("-->"+this.next.name);
                    this.next.printNode();
                }
            }
        }
    }
  • 相关阅读:
    SQL补充
    SQL练习题
    HDU 2907
    Codeforces 814D
    Codeforces 814C
    Codeforces 1004D
    Codeforces 1004E
    CodeForces 909F
    CodeForces 909E
    CodeForces 909D
  • 原文地址:https://www.cnblogs.com/ganhang-acm/p/5154268.html
Copyright © 2011-2022 走看看