zoukankan      html  css  js  c++  java
  • Java-单向链表算法

    /**
     * 数据结构之链表(单向链表)
     * @author Administrator
     *
     */
    public class LinkNodeTest {
    
    	public static void main(String[] args) {
    		LinkManager manager = new LinkManager();
    		manager.addNode("节点1");
    		manager.addNode("节点2");
    		manager.addNode("节点3");
    		manager.addNode("节点4");
    		manager.addNode("节点5");
    		manager.printNode();
    		manager.delNode("节点3");
    		manager.printNode();
    	}
    }
    
    class LinkManager{
    	private Node root;//根节点
    	//添加节点
    	public void addNode(String name){
    		if(root==null){
    			root = new Node(name);
    		}else{
    			root.add(name);
    		}
    	}
    	//删除节点
    	public void delNode(String name){
    		if(root!=null){
    			if(root.name.equals(name)){
    				root = root.next;
    			}else{
    				root.del(name);
    			}
    		}
    	}
    	//输出所有节点
    	public void printNode(){
    		if(root != null){
    			System.out.print(root.name);
    			root.print();
    			System.out.println();
    		}
    	}
    	//节点内部类
    	class Node{
    		private String name;
    		private Node next;
    		public Node(String name){
    			this.name = name;
    		}
    		//添加
    		public void add(String name){
    			if(this.next==null){
    				this.next = new Node(name);
    			}else{
    				this.next.add(name);
    			}
    		}
    		//删除
    		public void del(String name){
    			if(this.next != null){
    				if(this.next.name.equals(name)){
    					this.next = this.next.next;
    				}else{
    					this.next.del(name);
    				}
    			}
    		}
    		//输出
    		public void print(){
    			if(this.next != null){
    				System.out.print("-->"+this.next.name);
    				this.next.print();
    			}
    		}
    	}
    }

  • 相关阅读:
    java集合总结
    css基础:块级元素与行内元素
    第九周web作业:history of grammar
    正则表达式(regular expression rules)
    用indexOf查找字符出现次数
    DOM与BOM的概念
    css的单位以及调色法
    获取登录cookieColletion在cef里面打开网页
    引用CefSharp编译支持AnyCpu的办法
    解决H5移动端history.back无效
  • 原文地址:https://www.cnblogs.com/liuyanmin/p/5146531.html
Copyright © 2011-2022 走看看