简单链表实现

xiaoxiao2021-02-27  301

package app01; import java.util.Collection; @SuppressWarnings("rawtypes") public class MyLinkedList <E>{ public Node<E> first; public Node<E> last; public int size; @SuppressWarnings("hiding") private class Node<E>{ E value; Node<E> next; Node<E> prev;   Node(E value, Node<E> prev){ this.value = value;  this.prev = prev; } } public Node getLast() { return last; } public void setLast(Node<E> last) { this.last = last; } /** * <p>Discription:[b为true,正序遍历,否则逆序]</p> * Created on 2017年5月4日 * @param flag * @author:[风陵渡口] */ @SuppressWarnings("unchecked") public void iterator(boolean flag){ E value; if(flag){ Node it = first; while(null != it){ value = (E) it.value; System.out.println(value); it = it.next;  } }else{ Node it = last; while(null != it){ value = (E) it.value; System.out.println(value); it = it.prev;  } } } /** * <p>Discription:[链表中添加节点]</p> * Created on 2017年5月4日 * @param e * @author:[风陵渡口] */ public void add(E e){ Node<E> node = new Node<E>(e, last); if(null != last){ last.next = node; last = node; }else{ last = node; first = node; } size++; } /** * <p>Discription:[删除节点]</p> * Created on 2017年5月4日 * @param e * @author:[风凌渡口] */ public void delete(E e){ Node<E> toDel = getNode(e); if(null == toDel){ System.out.println("该节点不存在"); return; } //中间节点 if(!e.equals(first.value) && !e.equals(last.value)){ toDel.prev.next = toDel.next; toDel.next.prev = toDel.prev; return; }else if(e.equals(first.value)){     //第一个节点 first = first.next; return; } last = last.prev; last.next = null; } /** * <p>Discription:[获取等于当前值的元素节点]</p> * Created on 2017年5月4日 * @param e * @return * @author:[风凌渡口] */ public Node<E> getNode(E e){ if(null == e){ return null; } Node<E> tmp = first; while(!e.equals(tmp)){ tmp = tmp.next; if(null == tmp){ return null; }else if(e.equals(tmp.value)){ return tmp; } } return tmp; //返回第一个 } /** * <p>Discription:[获取指定位置元素的值]</p> * Created on 2017年5月4日 * @param index * @return * @author:[风凌渡口] */ public E getByIndex(int index){ checkIndex(index); Node<E> node = getNode(index); return  node.value; } private Node<E> getNode(int index){ Node<E> node = null; int tmp; if(index < (size >> 1)){ node = first; tmp = 0; while(tmp < index){ node = node.next; tmp++; } }else{ node = last; tmp = size-1; while(tmp > index){ node = node.prev; tmp--; } } return node; } /** * <p>Discription:[元素位置检验]</p> * Created on 2017年5月4日 * @param index * @author:[风凌渡口] */ private void checkIndex(int index) { if(!isElementIndex(index)) throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); } /** * <p>Discription:[判定索引的正确性]</p> * Created on 2017年5月4日 * @param index * @return * @author:[风凌渡口] */ private boolean isElementIndex(int index) { return index >= 0 && index <= size; } /** * <p>Discription:[在指定位置插入元素]</p> * Created on 2017年5月4日 * @param index * @param e * @author:[风凌渡口] */ public void add(int index, E e){ Node<E> before = getNode(index-1); Node<E> after = before.next; Node<E> newNode = new Node<E>(e, before); before.next = newNode; newNode.next = after; after.prev = newNode; size++; } /** * <p>Discription:[指定位置插入集合]</p> * Created on 2017年5月4日 * @param index * @param collection * @author:[风凌渡口] */ @SuppressWarnings("unchecked") public void add(int index, Collection<E> collection){ Node<E> before = getNode(index-1); Node<E> after = before.next; E[] arr = (E[]) collection.toArray(); for(int i = 0; i < arr.length; i++){ Node newNode = new Node<E>(arr[i], before); before.next = newNode; before = before.next; //处理尾部 if(i == (arr.length-1)){ before.next = after; after.prev = before; } } } }
转载请注明原文地址: https://www.6miu.com/read-2180.html

最新回复(0)