zoukankan      html  css  js  c++  java
  • 链表底层实现(LinkList)

    ArrayList

    LinkList

    链表底层实现

    Node.java  (节点类)

    package cn.Collection;
    
    //用来表示一个节点
    public class Node{
        Node previous;   //上一个节点
        Object obj;
        Node next;        //下一个节点
        
        public Node() {
            
        }
    
        public Node(Node previous, Object obj, Node next) {
            super();
            this.previous = previous;
            this.obj = obj;
            this.next = next;
        }
    
        public Node getPrevious() {
            return previous;
        }
    
        public void setPrevious(Node previous) {
            this.previous = previous;
        }
    
        public Object getObj() {
            return obj;
        }
    
        public void setObj(Object obj) {
            this.obj = obj;
        }
    
        public Node getNext() {
            return next;
        }
    
        public void setNext(Node next) {
            this.next = next;
        }
        
    }

    Test01.java

    package cn.Collection;
    
    public class Test01 {
        private Node first;
        private Node last;
        private int size;
        
        public void add(Object obj) {
            Node n=new Node();
            
            if(first==null) {
                
                n.setPrevious(null);
                n.setObj(obj);
                n.setNext(null);
                
                first=n;
                last=n;
            }
            else {
                //直接往last节点后增加新节点
                
                n.setPrevious(last);
                n.setObj(obj);
                n.setNext(null);
                last.setNext(n); 
                
                last=n;
                
                
                
            }
            size++;
        }
        public int size() {
            return size;
        }
        public Object get(int index) {   //2
            //0,1,2,3,4
            Node temp=null;
            if(first!=null) {
                temp=first;
                for(int i=0;i<index;i++) {
                    temp=temp.next;
                }
                
                
            }
            return temp.obj;
            
        }
        
        
        
        public static void main(String[] args) {
            Test01 list=new Test01(); 
            list.add("aaa");
            list.add("dd");
            list.add("d");
            System.out.println(list.size());
            System.out.println(list.get(1));
        }
    }
  • 相关阅读:
    redis初步入门(2)
    redis初步入门(1)
    iOS9 中 alertView 的使用
    iOS应用 数据存储方式 (一)
    Python 选课系统
    Python 计算器
    Python ATM
    Python 购物车
    Python 城市列表
    Python 基础登入接口
  • 原文地址:https://www.cnblogs.com/ssxblog/p/11221853.html
Copyright © 2011-2022 走看看