第二篇 集合与容器(二)

第一版

package com.zzp.demo.myCollection;

public class Node {
    
    Node previous; //上一节点
    Node next; //下一节点
    Object element; //数据
    
    public Node(Object element) {
        super();
        this.element = element;
    }

    public Node(Node previous, Node next, Object element) {
        super();
        this.previous = previous;
        this.next = next;
        this.element = element;
    }
}
package com.zzp.demo.myCollection;
/**
 * 
 * 自定义链表
 * @author java
 *
 */
public class LinkedList01 {
    private Node first;
    private Node last;
    private int size;
    
    //添加元素
    public void add(Object obj){
        Node node = new Node(obj);
        if(first == null){
            first = node;
            last = node;
        }else{
            node.previous = last;
            node.next = null;
            last.next = node;
            last = node;
        }
    }
    
    //打印字符串
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        Node temp = first;
        while(temp != null){
            sb.append(temp.element + ",");
            temp = temp.next;
        }
        sb.setCharAt(sb.length() - 1, ']');
        return sb.toString();
    }
    
    public static void main(String[] args) {
        LinkedList01 ls = new LinkedList01();
        ls.add("sa");
        ls.add("sdsf");
        ls.add("kajs");
        System.out.println(ls.toString());
    }
}

 第二版

package com.zzp.demo.myCollection;
/**
 * 
 * 自定义链表
 * 增加get方法
 * @author java
 *
 */
public class LinkedList02 {
    private Node first;
    private Node last;
    private int size;
    
    public Object get(int index){
        
        if(index < 0 || index > size - 1){
            throw new RuntimeException("索引不合法"+index);
        }
        
        Node temp = null;
        
        if(index < (size >> 1)){
            temp = first;
            for(int i =0;i<index;i++){
                temp = temp.next;
            }
        }else{
            temp = last;
            for(int i=size-1;i>index;i--){
                temp = temp.previous;
            }
        }    
        return temp.element;
    }
    
    //添加元素
    public void add(Object obj){
        Node node = new Node(obj);
        if(first == null){
            first = node;
            last = node;
        }else{
            node.previous = last;
            node.next = null;
            last.next = node;
            last = node;
        }
        size++;
    }
    
    //打印字符串
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        Node temp = first;
        while(temp != null){
            sb.append(temp.element + ",");
            temp = temp.next;
        }
        sb.setCharAt(sb.length() - 1, ']');
        return sb.toString();
    }
    
    public static void main(String[] args) {
        LinkedList02 ls = new LinkedList02();
        ls.add("sa");
        ls.add("sdsf");
        ls.add("kajs");
        ls.add("sajd");
        ls.add("sdsfgf");
        ls.add("kajsds");
        System.out.println(ls.size);
        System.out.println(ls.get(3));
    }
}

猜你喜欢

转载自www.cnblogs.com/zhangzhipeng001/p/9508840.html