Java:-重写系统方法--LinkedList

2018-07-13  本文已影响19人  风的低语
import java.util.LinkedList;
import java.util.List;

public class MyLinkedList {
    // 链表的首地址
    private Node first = null;
    // 尾节点
    private Node last = null;
    // 记录链表的有效长度()
    private int size = 0;
    // 链表中添加元素(在尾部)
    public boolean add(Object e)  {
        this.addLast(e);
        return true;
    }
    // 在此列表中指定的位置插入指定的元素
    public void add(int index, Object element)  {
        // index越界判断
        if (index < 0 || index > size) {
            return;
        }
        // 创建新节点
        Node node = new Node(); 
        node.data = element;
        node.next = null;
        // 链表为null处理
        if (this.last == null) { // 链表为null
            this.first = node;
            this.last = node;
        } else {
            if (index == 0) { // 头部
                this.addFirst(element);
            } else if (index == size) { // 尾部
                this.addLast(element);
            } else { // 中间
                Node preNode =  this.node(index - 1);
                Node indexNode = preNode.next;
                node.next = indexNode;
                preNode.next = node;
                this.size++;
            }
        }
    }
    
    // 根据下表找节点
     private Node node(int index) {
         // 判断index
         if (index < 0 || index >= size) {
            return null;
        }
         int tag = 0;
         Node temp = null;
         for(Node l = first; l != null; l = l.next) {
             if (tag == index) {
                 temp = l;
                 break;
            }
             tag++;
         }
         return temp;
     }
     
     // 根据下表找元素
     public Object get(int index)  {
         // 判断index
         if (index < 0 || index >= size) {
            return null;
         }
         // 返回根据下表找到节点的元素
         return this.node(index).data;
     }
    
    // 在头部添加   将指定元素插入此列表的开头
    public void addFirst(Object e) {
        // 创建新节点
        Node node = new Node(); 
        node.data = e;
        node.next = null;
        // 链表中一个元素都没有
        if (this.last == null) { // 链表为null
            this.first = node;
            this.last = node;
        } else {  // 链表不为null 
            // 链表中有元素
            node.next = first;
            this.first = node;
        }
        this.size++;
    }
    // 在尾部添加    将指定元素添加到此列表的结尾
    public void addLast(Object e)  {
        // 创建一个新节点
        Node node = new Node(); 
        node.data = e;
        node.next = null;
        // 一个元素都没有
        if (this.last == null) {  //没有元素
            this.first = node;
            this.last = node;
        } else { // 有元素
            this.last.next = node;
            this.last = node;
        }
        this.size++;
    }
    
    // 返回链表的长度
    public int size() {
        return this.size;
    }
    
    // 构造方法
    public MyLinkedList() {
        
    }
    
    // 构造一个包含指定 List 中的元素的列表
    public MyLinkedList(List list) {
        
    }
    
    @Override
    public String toString() {
        String str = "[";
        // 链表的遍历
        for(Node l = first; l != null;l = l.next) {
            Object obj = l.data;
            str = str + obj.toString();
            str = str + ",";
        }
        if (str.length() >= 2) {
            str = str.substring(0, str.length() - 1);
        }
        str+="]";
        return str;
    }
    
}

// 节点
class Node {
    Object data; // 数据
    Node next;   // 下个节点的引用
}

上一篇 下一篇

猜你喜欢

热点阅读