package com.jaky.Collection; import java.util.Iterator; //单链 public class MyLinkedList { // 实际大小 private int size; // 头部 private Entry header; private class Entry { String element; Entry next; Entry(String element, Entry next) { this.element = element; this.next = next; } } // 返回实际大小 public int size() { return this.size; } /** * 根据索引查找节点 * * @return */ private Entry findEntry(int index) { if (index < 0 || index > size - 1 || size == 0) { // return null; throw new ArrayIndexOutOfBoundsException("索引无效,不在范围内"); } int cursor = 0;// 计数器 Entry entry = header; // 顺藤摸瓜 while (cursor < index) { entry = entry.next; cursor++; } return entry; } /** * 获取指定元素的值 */ public String get(int index) { // 获取指定节点 Entry entry = findEntry(index); return entry == null ? null : entry.element; } /** * 指定位置添加元素(插入元素) * * @return */ public void add(int index, String element) { // 1、索引是否有效 // 可以等于size 相当于添加到最后一个位置 if (index < 0 || index > size) { // return null; throw new ArrayIndexOutOfBoundsException("索引无效,不在范围内"); } // 2、找到 根据索引查找前一个节点 Entry previous = findEntry(index - 1);// 前一个节点 Entry next = previous.next;// 原来索引的节点 Entry newEntry = new Entry(element, next); previous.next = newEntry; size++; } /** * 容器最后添加元素 * 1、是否存在元素 查看头是否存在 * 2、不存在: 该元素为头部 否则:顺藤摸瓜(循环)最后一个元素 * 3、记录大小 * */ public boolean add(String element) { // 没有头部 if (null == header) { header = new Entry(element, null); } else { // 顺藤摸瓜 Entry entry = header; while (null != entry.next) { entry = entry.next; } entry.next = new Entry(element, null); } size++; return true; } /** * 修改元素 */ public String set(int index, String newValue) { // 获取指定的节点 Entry entry = findEntry(index); String oldValue = null; if (null != entry) { oldValue = entry.element; entry.element = newValue; } return oldValue; } /** * 删除元素 删除最后一个 * 1、倒数第二个 * 2、next =null */ public void remove() { Entry entry = this.findEntry(this.size -2); entry.next = null; this.size--; } /** * 指定位置 删除元素 */ public void remove(int index) { Entry delNode = findEntry(index); if (0 == index) { // 删除第一个 Entry newHead = header.next; header = newHead; } else if (this.size - 1 == index) { // 删除最后一个 remove(); } else { Entry pre = findEntry(index - 1); // 前一个 pre.next = delNode.next; } this.size--; } // 匿名内部类 public Iterator iterator() { return new Iterator() { private int coursor = -1; // 加入自己迭代器 判断 指针 是否 在size内 // 只是判断,不会移动 public boolean hasNext() { return coursor < size - 1; } // 移动指针 +获取值 public String next() { if (!hasNext()) { throw new ArrayIndexOutOfBoundsException("索引无效,不在范围内"); } coursor++; // 移动指针 return get(coursor); } @Override public void remove() { if (!hasNext()) { throw new ArrayIndexOutOfBoundsException("索引无效,不在范围内"); } coursor++; // 移动指针 MyLinkedList.this.remove(coursor); coursor--; } }; } }
测试程序如下:
package com.jaky.Collection; import java.util.Iterator; public class MyLinkedListApp { public static void main(String[] args) { MyLinkedList list = new MyLinkedList(); list.add("父亲"); list.add("儿子"); list.add("狗"); //list.add(1, "母亲"); list.remove(); Iterator it = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }

浙公网安备 33010602011771号