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();
                }
            }
        }
    }
  • 相关阅读:
    [代码]codeforces 150c Smart Cheater
    [存档]Young Tableau
    [转载][毁童年]种太阳
    [转载]教练,我也想再要一个同桌
    静夜
    CodeProject每日精选: Applications
    设计模式 装饰模式(Decorator) 精选经验合集
    槟榔味儿办公室
    盗不盗非常盗
    月末之周末
  • 原文地址:https://www.cnblogs.com/ganhang-acm/p/5154268.html
Copyright © 2011-2022 走看看