当前链表的大小
*/
transient int size;
public boolean add(E e) {
linkLast(e);
return true;
}
public int getSize() {
return size;
}
public void addFirst(E e) {
Node h = head;
//创建新节点
Node newNode = new Node<>(null, e, head);
head = newNode;
if (head == null) {
tail = newNode;
} else {
h.pre = newNode;
}
size++;
}
public E get(int index) {
if (isPositionIndex(index)) {
return searchNode(index).data;
}
return null;
}
public void add(int index, E e) {
addIndex(index, e);
}
public E remove() {
return removeFirst();
}
private E removeFirst() {
Node h = head;
if (h == null)
throw new NoSuchElementException();
return unlinkFirst(h);
}
private E unlinkFirst(Node h) {
//删除的 数据
E deleData = h.data;
//找到要删除的后驱
Node next = h.next;
//清理节点
h.data = null;
h.next = null;
//将当前要删除的后驱置为链表头
head = next;
if (next == null) {
tail = null;
} else {
next.pre = null;
}
h = null;
size–;
return deleData;
}
private void addIndex(int index, E e) {
if (isPositionIndex(index)) {
//找到当前需要插入的索引位置
if (index == size) {
linkLast(e);
} else {
add(e, searchNode(index));
}
}
}
private void add(E e, Node searchNode) {
//找到 searchNode 前驱节点
Node snPre = searchNode.pre;
//创建新节点
Node newNode = new Node<>(snPre, e, searchNode);
searchNode.pre = newNode;
//这里判断 snPre 是否为空 入股为空说明 head 没有数据,如果有数据 就直接把 snPre . next() = newNode
if (snPre == null) {
head = newNode;
} else {
snPre.next = newNode;
}
size++;
}
private Node searchNode(int index) {
//优化寻找节点 如果 index > size / 2 就从尾部开始查询,反之从 head 开始遍历查询
if (index > (size >> 1)) {
Node t = tail;
for (int i = size - 1; i > index; i–) {
t = t.pre;
}
return t;
} else {
Node h = head;
for (int i = 0; i < index; i++) {
h = h.next;
}
return h;
}
}
private void linkLast(E e) {
//拿到尾部的节点数据
Node t = tail;
//创建新的节点数据,因为是存在当前节点的尾部,
// 那么直接默认将当前添加进来的 E 的前驱设置为
// 当前链表中的尾部数据,现在已经形成单链表了
// 下一步直接形成双向链表
Node newNode = new Node<>(t, e, null);
//现在是双向链表,要把新的节点指向当前的尾部节点,尾部节点指向新的节点
tail = newNode;
//如果尾部节点为空 那么说明 head 也是空数据 那就吧新节点数据赋值给 head
if (t == null) {
head = newNode;
} else {
t.next = newNode;
}
size++;
}
public CustomlinkedList() {
}
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
private Node next;
public Node(Node pre, E data, Node last) {
this.pre = pre;
this.data = data;
this.next = last;
}
}
}
测试代码
@Test
public void testCustomlinkedList() {
CustomlinkedList linkedList = new CustomlinkedList();
linkedList.add(22);
linkedList.add(2);
linkedList.add(77);
linkedList.add(6);
linkedList.add(43);
linkedList.add(76);
linkedList.add(89);
linkedList.add(0,0);
for (int i = 0; i < linkedList.size; i++) {
int integer = linkedList.get(i);
System.out.println("–CustomlinkedList–CustomlinkedList" +integer+"");
}
System.out.println("nn");
Integer remove = linkedList.remove();
System.out.println("–CustomlinkedList–CustomlinkedList" +remove);
Integer remove1 = linkedList.remove();
System.out.println("–CustomlinkedList–CustomlinkedList" +remove1+"");
Integer remove2 = linkedList.remove();
System.out.println("–CustomlinkedList–CustomlinkedList" + remove2 + “”);
System.out.println("nn");
for (int i = 0; i < linkedList.size; i++) {
int integer = linkedList.get(i);
System.out.println("–CustomlinkedList–CustomlinkedList" +integer+"");
}
}
测试结果
编写简单的 ArrayList CURDpublic class CustomArrayList {
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
private static final Object[] EMPTY_ELEMENTDATA = {};
private Object[] elementData = null;
private int size = 0;
private static final int DEFAULT_CAPACITY = 10;
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
public CustomArrayList(){
elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public CustomArrayList(int initCapacity){
if (initCapacity > 0){
elementData = new Object[initCapacity];
}else if (initCapacity == 0){
elementData = EMPTY_ELEMENTDATA;
}else {
throw new IllegalArgumentException("Illegal Capacity: "+
initCapacity);
}
}
public boolean add(E e){
//判断是否需要开辟容量空间
checkIsNeedCapacity(size + 1);
//添加添加数据
elementData[size++] = e;
return true;
}
private void checkIsNeedCapacity(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA){
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOf
MemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
}
public class Singlelinked {
int size = 0;
图片转存中…(img-2GjremKp-1643622153004)]
MemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
}
编写单向链表结构的 CURD
[外链图片转存中…(img-BoHjjyls-1643622153004)]
public class Singlelinked {
int size = 0;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)