自定义链表集合,无实现任何接口,继承
来源:互联网 发布:高新区网络问政平台 编辑:程序博客网 时间:2024/06/04 19:44
用Java实现了一个链表集合,虽然已经有LinkedList,试着写一个,也算是了解下底层结构吧~
我不会告诉你我是因为忘记C语言才这么写的。
package other;public class MyLinkedList<E> { final public int MAX_SIZE = Integer.MAX_VALUE - 2; /** * 头结点 */ private ListNode head; /** * 尾节点 */ private ListNode end; /** * 节点数 */ private int size; public MyLinkedList(){ head = new ListNode(); head.next = null; size = 0; end = head; } public void add(E value) { ListNode node = new ListNode(); node.value = value; node.next = null; end.next = node; node.last = end; end = node; size++; } public E remove(E element) { ListNode now = head.next; ListNode next = now.next; while(now != null) { if(now.isSame(element)) { now.next = next; next.last = now; return now.value; } now = now.next; next = now.next; } return null; } public E remove(int index) { rangeCheck(index); ListNode now ; ListNode next; int n; //是否降序查找 boolean desc; if(index>size%2) { now = end; next = now.last; desc = false; n = 0; }else { now = head.next; next = now.next; desc = true; n = size-1; } while(now != null) { if(n == index) { now.next = next; next.last = now; return now.value; } if(!desc) { now = now.next; next = now.next; n++; } else { now = now.last; next = now.last; n--; } } return null; } public int indexof(E element) { ListNode now = head.next; int n = -1; while(now != null) { if(now.isSame(element)) return n; now = now.next; n++; } return -1; } public int lastIndexof(E element) { ListNode now = end; int n = size-1; while(now != null) { if(now.isSame(element)) return n; now = now.last; n--; } return -1; } public void set(int index,E element) { rangeCheck(index); ListNode now; int n; //是否降序查找 boolean desc; if(index>size%2) { now = end; desc = false; n = 0; }else { now = head.next; desc = true; n = size-1; } while(now != null) { if(n == index) { now.value = element; } now = now.next; if(!desc) n++; else n--; } } public E get(int index) { rangeCheck(index); ListNode now; int n; //是否降序查找 boolean desc; if(index>size%2) { now = end; desc = false; n = 0; }else { now = head.next; desc = true; n = size-1; } while(now != null) { if(n == index) { return now.value; } now = now.next; if(!desc) n++; else n--; } return null; } public int size() { return size; } public boolean isEmpty() { return size==0?true:false; } private void rangeCheck(int index) { if (index >= size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+size; } //节点 private class ListNode{ //值 private E value; //上一节点 private ListNode last; //下一个节点 private ListNode next; private boolean isSame(E element) { if(element == null) return false; if(element.getClass() != getclass()) return false; E e = (E)element; if(e.equals(value)) return true; return false; } private Class<? extends Object> getclass() { return value.getClass(); } }}
阅读全文
1 0
- 自定义链表集合,无实现任何接口,继承
- 自定义集合 -- 能够安全管理任何类型
- C# 实现Sort接口 排序自定义实体类集合
- 接口继承和实现继承
- 接口继承和实现继承
- 接口继承与实现继承
- 接口继承和实现继承
- 接口继承与实现继承
- 接口继承与实现继承
- 接口继承与实现继承
- 接口继承与实现继承
- 接口继承与实现继承
- 继承:继承接口和实现
- java接口 接口实现 继承
- java集合框架集合框架是为表示和操作集合而规定的一种统一的标准的体系结构。任何集合框架都包含三大块内容:对外的接口、接口的实现和对集合运算的算法。
- 自定义实现无模块
- 为什么在集合框架中已经继承了抽象类还要实现接口
- Struts中继承接口SessionAware来实现session中的map集合
- Android应用资源总结二:Android的问号?和@符号的用法
- 100. Same Tree
- 测试Markdown
- MySQL_008_基础_索引[简介]
- zookeeper集群某台机器故障替换步骤
- 自定义链表集合,无实现任何接口,继承
- 战狼2很火,但闲余之时看了 上甘岭、高山下的花环
- 树状数组
- Java Socket编程
- Install TensorFlow
- 29、C#:C#去除字符串的最后一个字符
- git的分支管理策略
- Trie+拓扑排序——Luogu3065 [USACO12DEC]第一!First!
- ZOJ 3449 Doraemon's Number Game III (数论)