手动模拟LinkedList

手动模拟LinkedList

/**
 * 自己用链表写一个Linked_List
 * 索引定为从1开始
 * @param <T>
 */
public class Linked_List<T> {
    //定义一个节点类
    private class Node {
        private T data;
        private Node next;

        public Node() {
        }

        private Node(T data, Node next) {
            this.data = data;
            this.next = next;
        }
    }

    //设置头尾,方便对头尾进行增删改查
    private Node head;
    private Node tail;
    int size;

    public Linked_List() {
        head = null;
        tail = null;
    }

    public Linked_List(T element) {
        //只有一个元素时,头就是尾
        head = new Node(element, null);
        tail = head;
        size++;
    }

    /**
     * 从头部插入
     *
     * @param element
     */
    public void addFirst(T element) {
        head = new Node(element, head);
        if (tail == null) {
            tail = head;
        }
        size++;
    }

    /**
     * 从尾部插入
     *
     * @param element
     */
    public void addLast(T element) {
        if (head == null) {
            head = new Node(element, head);
            tail = head;
        } else {
            Node node = new Node(element, null);
            tail.next = node;
            tail = node;
        }
        size++;
    }

    /**
     * 在指定位置处插入新节点
     *
     * @param element
     * @param index
     */
    public void addByIndex(T element, int index) {
        Node add = new Node(element, findByIndex(index));
        Node lastNode = findByIndex(index - 1);
        lastNode.next = add;
        size++;
    }

    /**
     * 获取头部数据
     *
     * @return
     */
    public T getFirst() {
        return head.data;
    }

    /**
     * 获取尾部数据
     *
     * @return
     */
    public T getLast() {
        return tail.data;
    }

    /**
     * 获取指定位置处数据
     *
     * @param index
     * @return
     */
    public T getByIndex(int index) {
        return findByIndex(index).data;
    }

    /**
     * 获取指定位置处的节点,索引位置从1开始
     *
     * @param index
     * @return
     */
    public Node findByIndex(int index) {
        if (index == 1) {
            return head;
        }
        int count = 1;
        Node temp = head;
        while (count != index) {
            count++;
            temp = temp.next;
        }
        return temp;
    }

    /**
     * 删除头部
     *
     * @return
     */
    public boolean removeFirst() {
        if (head == null) {
            return false;
        }
        head = head.next;
        size--;
        return true;
    }

    /**
     * 删除尾部
     *
     * @return
     */
    public boolean removeLast() {
        if (head == null) {
            return false;
        }
        Node byIndex = findByIndex(size - 1);
        tail = byIndex;
        tail.next = null;
        size--;
        return true;
    }

    /**
     * 通过索引位置来删除元素
     *
     * @param index
     * @return
     */
    public boolean removeByIndex(int index) {
        if (index > size || index < 1) {
            return false;
        }
        findByIndex(index - 1).next = findByIndex(index + 1);
        size--;
        return true;
    }

    /**
     * 修改头部数据
     *
     * @param element
     * @return
     */
    public boolean setFirst(T element) {
        if (head == null) {
            addFirst(element);
        }
        head.data = element;
        return true;
    }

    /**
     * 修改尾部数据
     *
     * @param element
     * @return
     */
    public boolean setLast(T element) {
        if (head == null) {
            addLast(element);
        }
        tail.data = element;
        return true;
    }

    /**
     * 修改指定索引位置元素
     *
     * @param element
     * @param index
     * @return
     */

    public boolean setByIndex(T element, int index) {
        if (index < 1 || index > size) {
            return false;
        }
        findByIndex(index).data = element;
        return true;
    }


    /**
     * 遍历Linked_List
     */
    public String toString() {
        Node temp = head;
        String str = "[";
        while (temp.next != null) {
            str += (temp.data + ",");
            temp = temp.next;
        }
        str += (temp.data + "]");
        return str;

    }


}

测试类:

/**
 * 测试Linked_List
 */
public class TestLinked_List {
    public static void main(String[] args) {
        //创建对象,添加元素:
        Linked_List<String> list = new Linked_List<>("1111");
        list.addFirst("2222");
        list.addLast("3333");
        list.addByIndex("4444",3);
        //遍历元素,重写了toString方法
        System.out.println(list);
        //获取元素
        System.out.println(list.getByIndex(3));
        System.out.println(list.getFirst());
        System.out.println(list.getLast());
        //获取长度
        System.out.println(list.size);
        //删除元素
        list.removeFirst();
        System.out.println(list);
        list.removeLast();
        System.out.println(list);
        list.removeByIndex(2);
        System.out.println(list);
        //修改元素
        list.setByIndex("1234",1);
        System.out.println(list);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值