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
}
}
数据结构-自定义单链表
Dragonlongbo2024-11-05 13:59
相关推荐
smj2302_796826528 小时前
解决leetcode第3943题递增后的数对数量梓䈑10 小时前
【算法题攻略】快速排序 和 归并排序he___H12 小时前
leetcode100-普通数组不知名的老吴13 小时前
经典算法实战:重新排列日志文件(二)CS创新实验室13 小时前
数据结构和算法:斐波那契堆guslegend15 小时前
2.Redis核心数据结构Daydream.V15 小时前
Redis 零基础入门到实战:数据结构 + 常用命令 + 场景全覆盖bnmoel15 小时前
数据结构深度剖析二叉树・中篇:堆的概念及结构 ,实现应用全解析fu的博客15 小时前
【数据结构15】哈夫曼树构建、编码(附手绘图解)bnmoel15 小时前
数据结构深度剖析二叉树・上篇:基础概念、结构特性、存储结构全解析