数据结构-自定义单链表

复制代码
package cn.dragon.arithmetic.model;

import java.util.NoSuchElementException;

//单向链表
public class MySingleTrackLInkedList<T> {

    private static class Node<T> {
        T val;

        Node<T> next;

        public Node(T val) {
            this.val = val;
            this.next = null;
        }
    }

    private Node<T> head;
    private Node<T> tail;
    private int size;

    public MySingleTrackLInkedList() {
        this.head = new Node<>(null);
        this.tail = head;
        this.size = 0;
    }

    public void addFirst(T d) {
        Node<T> newNode = new Node<>(d);
        newNode.next = head.next;
        head.next = newNode;
        if (size == 0) {
            tail = newNode;
        }
        size++;
    }

    public void addLast(T d) {
        Node<T> newNode = new Node<>(d);
        tail.next = newNode;
        tail = newNode;
        size++;
    }

    public void add(int index, T element) {
        checkPositionIndex(index);

        if (index == size) {
            addLast(element);
            return;
        }

        Node<T> prev = head;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node<T> newNode = new Node<>(element);
        newNode.next = prev.next;
        prev.next = newNode;
        size++;
    }

    public T removeFirst() {
        if (isEmpty()) {
            throw new NoSuchElementException();
        }
        Node<T> first = head.next;
        head.next = first.next;
        if (size == 1) {
            tail = head;
        }
        size--;
        return first.val;
    }

    public T removeLast() {
        if (isEmpty()) {
            throw new NoSuchElementException();
        }
        Node<T> prev = head;
        while (prev.next != tail) {
            prev = prev.next;
        }
        T val = tail.val;
        prev.next = null;
        tail = prev;
        size--;
        return val;
    }

    public T remove(int index) {
        checkElementIndex(index);
        Node<T> prev = head;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }

        Node<T> nodeToRemove = prev.next;
        prev.next = nodeToRemove.next;
        if (index == size - 1) {
            tail = prev;
        }
        size--;
        return nodeToRemove.val;
    }

    public T getFirst() {
        if (isEmpty()) {
            throw new NoSuchElementException();
        }
        return head.next.val;
    }

    public T getLast() {
        if (isEmpty()) {
            throw new NoSuchElementException();
        }
        return getNode(size - 1).val;
    }

    public T get(int index) {
        checkElementIndex(index);
        Node<T> p = getNode(index);
        return p.val;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    private boolean isElementIndex(int index) {
        return index >= 0 && index < size;
    }

    private boolean isPositionIndex(int index) {
        return index >= 0 && index <= size;
    }

    private void checkElementIndex(int index) {
        if (!isElementIndex(index)){
            throw new IndexOutOfBoundsException();
        }
    }

    private void checkPositionIndex(int index) {
        if (!isPositionIndex(index)) {
            throw new IndexOutOfBoundsException();
        }
    }

    private Node<T> getNode(int index) {
        Node<T> p = head.next;
        for (int i = 0; i < index; i++) {
            p = p.next;
        }
        return p;
    }

    public static void main(String[] args) {
        MySingleTrackLInkedList<Integer> list = new MySingleTrackLInkedList<>();
        list.addFirst(1);
        list.addFirst(2);
        list.addLast(3);
        list.addLast(4);
        list.add(2, 5);

        System.out.println(list.removeFirst()); // 2
        System.out.println(list.removeLast()); // 4
        System.out.println(list.remove(1)); // 5

        System.out.println(list.getFirst()); // 1
        System.out.println(list.getLast()); // 3
        System.out.println(list.get(1)); // 3
    }

}
相关推荐
许小燚5 小时前
线性表——双向链表
数据结构·链表
qqxhb7 小时前
零基础数据结构与算法——第四章:基础算法-排序(上)
java·数据结构·算法·冒泡·插入·选择
晚云与城8 小时前
【数据结构】顺序表和链表
数据结构·链表
FirstFrost --sy9 小时前
数据结构之二叉树
c语言·数据结构·c++·算法·链表·深度优先·广度优先
Yingye Zhu(HPXXZYY)9 小时前
Codeforces 2021 C Those Who Are With Us
数据结构·c++·算法
liulilittle10 小时前
LinkedList 链表数据结构实现 (OPENPPP2)
开发语言·数据结构·c++·链表
秋说11 小时前
【PTA数据结构 | C语言版】两枚硬币
c语言·数据结构·算法
☆璇12 小时前
【数据结构】栈和队列
c语言·数据结构
chao_78915 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
秋说15 小时前
【PTA数据结构 | C语言版】将数组中元素反转存放
c语言·数据结构·算法