packagecom.lhs;publicclassSinglyLinkedList<E>implementsList<E>{// 头节点privateNode<E> first;// 尾节点privateNode<E> last;// 节点数量privateint size;publicstaticclassNode<E>{E data;Node<E> next;Node(E data,Node<E> next){this.data = data;this.next = next;}}// 返回节点数量@Overridepublicintsize(){return size;}// 判断链表是否为空@OverridepublicbooleanisEmpty(){return size ==0;}// 判断链表中是否包含某个元素@Overridepublicbooleancontains(Object o){if(o ==null){for(Node<E> node = first;node !=null;node = node.next){if(node.data ==null){returntrue;}}returnfalse;}else{for(Node<E> node = first;node !=null;node = node.next){if(o.equals(node.data)){returntrue;}}}returnfalse;}// 向链表中添加元素@Overridepublicbooleanadd(E e){Node<E> l = last;Node<E> eNode =newNode<>(e,null);if(l !=null){l.next = eNode;}else{first = eNode;}last = eNode;size++;returntrue;}// 获取链表中指定索引位置的元素@OverridepublicEget(int index){if(index >= size || index <0){thrownewIndexOutOfBoundsException(index +"is out of bounds");}Node<E> node = first;for(int i =0; i < index; i++){node = node.next;}return node.data;}// 设置链表中指定索引位置的元素@OverridepublicEset(int index,E e){if(index >= size || index <0){thrownewIndexOutOfBoundsException(index +"is out of bounds");}Node<E> node = first;for(int i =0; i < index; i++){node = node.next;}E oldVal = node.data;node.data = e;return oldVal;}// 移除链表中指定索引位置的元素@OverridepublicEremove(int index){if(index >= size || index <0){thrownewIndexOutOfBoundsException(index +"is out of bounds");}Node<E> node = first;Node<E> before =null;E oldVal;if(index ==0){oldVal = first.data;first = first.next;}else{for(int i =0; i < index; i++){if(i == index -1){before = node;}node = node.next;}oldVal = node.data;before.next = node.next;}size--;return oldVal;}// 向链表中添加元素到头部@OverridepublicvoidaddFirst(E e){Node<E> oldVal = first;Node<E> eNode =newNode<>(e, oldVal);first = eNode;size++;}// 向链表中添加元素到尾部@OverridepublicvoidaddLast(E e){add(e);}// 从链表中移除头部元素@OverridepublicEremoveFirst(){returnremove(0);}// 从链表中移除尾部元素@OverridepublicEremoveLast(){returnremove(size);}}
测试
packagecom.lhs;importorg.junit.Test;importstaticjunit.framework.TestCase.*;importstaticorg.junit.Assert.assertThrows;/*** SinglyLinkedList Test* * @since 1.0.0 2020年5月4日* @author <a href="https://waylau.com">Way Lau</a>*/publicclassSinglyLinkedListTest{@TestpublicvoidtestSize(){// 实例化SinglyLinkedListList<String> list =newSinglyLinkedList<String>();assertTrue(list.size()==0);list.add("Java");assertTrue(list.size()==1);}@TestpublicvoidtestIsEmpty(){// 实例化SinglyLinkedListList<String> list =newSinglyLinkedList<String>();assertTrue(list.isEmpty());list.add("Java");assertFalse(list.isEmpty());}@TestpublicvoidtestContains(){// 实例化SinglyLinkedListList<String> list =newSinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");list.add("Python");list.add("TypeScript");// 判断存在assertTrue(list.contains("Java"));// 判断不存在assertFalse(list.contains("Java++"));}@TestpublicvoidtestAdd(){// 实例化SinglyLinkedListList<Integer> list =newSinglyLinkedList<Integer>();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);assertFalse(list.isEmpty());}@TestpublicvoidtestGet(){// 实例化SinglyLinkedListList<String> list =newSinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");// 判断存在assertEquals("C++", list.get(1));// 判断不存在int index =6;Throwable excpetion =assertThrows(IndexOutOfBoundsException.class,()->{list.get(index);// 抛异常});assertEquals(index +"is out of bounds",excpetion.getMessage());}@TestpublicvoidtestSet(){// 实例化SinglyLinkedListList<String> list =newSinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");// 判断存在assertEquals("C", list.set(2,"Python"));// 判断不存在int index =6;Throwable excpetion =assertThrows(IndexOutOfBoundsException.class,()->{list.set(index,"Python");// 抛异常});assertEquals(index +"is out of bounds",excpetion.getMessage());}@TestpublicvoidtestRemove(){// 实例化SinglyLinkedListList<String> list =newSinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");// 判断存在assertEquals("C", list.remove(2));assertEquals("Java", list.get(0));assertEquals("C++", list.get(1));// 判断不存在int index =6;Throwable excpetion =assertThrows(IndexOutOfBoundsException.class,()->{list.remove(index);// 抛异常});assertEquals(index +"is out of bounds",excpetion.getMessage());}@TestpublicvoidtestAddFirst(){// 实例化SinglyLinkedListList<String> list =newSinglyLinkedList<String>();list.addFirst("Java");list.addFirst("C++");list.addFirst("C");// 判断存在assertEquals("C", list.get(0));assertEquals("C++", list.get(1));assertEquals("Java", list.get(2));}@TestpublicvoidtestAddLast(){// 实例化SinglyLinkedListList<String> list =newSinglyLinkedList<String>();list.addLast("Java");list.addLast("C++");list.addLast("C");// 判断存在assertEquals("Java", list.get(0));assertEquals("C++", list.get(1));assertEquals("C", list.get(2));}@TestpublicvoidtestRemoveFirst(){// 实例化SinglyLinkedListList<String> list =newSinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");// 判断存在assertEquals("Java", list.removeFirst());assertEquals("C++", list.removeFirst());assertEquals("C", list.removeFirst());}@TestpublicvoidtestRemoveLast(){// 实例化SinglyLinkedListList<String> list =newSinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");// 判断存在assertEquals("C", list.removeLast());assertEquals("C++", list.removeLast());assertEquals("Java", list.removeLast());}}