java版本链表MyLinkedList程序及测试用例demo
2019-03-27 16:13
260 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_17289971/article/details/88847928
1.单链表MyLinkedList实例
package com.hooware.mylinked; /** * @author hooware * @param <E> */ public class MyLinkedList<E> { /** * size : 链表长度 * head : 链表虚拟头结点 */ private int size; private Node head; public MyLinkedList() { this.head = new Node(); } class Node { Node next; E val; public Node() { } public Node(E val) { this.val = val; } } public int getSize() { return size; } public boolean isEmpty() { return size == 0; } public void addFirst(E value) { Node node = new Node(value); Node temp = head.next; head.next = node; node.next = temp; size++; } public void addLast(E value) { Node node = new Node(value); Node cur = head; while (cur.next != null) { cur = cur.next; } cur.next = node; size++; } public void add(E value, int index) { if (index < 0) throw new IllegalArgumentException("索引不合法"); if (index > size) { addLast(value); } else { Node node = new Node(value); Node cur = head; for (int i = 0; 7ff7 i < index; i++) { cur = cur.next; } Node temp = cur.next; cur.next = node; node.next = temp; size++; } } public E peek() { Node cur = head.next; if (cur == null) { return null; } return cur.val; } public E getFirst() { return peek(); } public E getLast() { Node cur = head; if (cur.next == null) { return null; } while (cur.next != null) { cur = cur.next; } return cur.val; } public E removeFirst() { Node cur = head; if (cur.next == null) { return null; } else { E value = cur.next.val; cur.next = cur.next.next; size--; return value; } } public E removeLast() { Node cur = head; if (cur.next == null) { return null; } else { while (cur.next.next != null) { cur = cur.next; } E value = cur.next.val; cur.next = cur.next.next; size--; return value; } } public E remove(int index) { if (index < 0) throw new IllegalArgumentException("索引不合法"); if (index >= size) return removeLast(); Node cur = head; if (cur.next == null) { return null; } for (int i = 0; i < index; i++) { cur = cur.next; } E value = cur.next.val; cur.next = cur.next.next; size--; return value; } public E updateFirst(E val) { Node cur = head.next; if (cur == null) { return null; } E value = cur.val; cur.val = val; return value; } public E updateLast(E val) { Node cur = head.next; if (cur == null) { return null; } while (cur.next != null) { cur = cur.next; } E value = cur.val; cur.val = val; return value; } public E update(E val, int index) { if (index < 0) throw new IllegalArgumentException("索引不合法"); if (index >= size) return updateLast(val); Node cur = head.next; if (cur == null) { return null; } for (int i = 0; i < index; i++) { cur = cur.next; } E value = cur.val; cur.val = val; return value; } @Override public String toString() { StringBuilder sb = new StringBuilder("MyLinkedList size is " + size + " !"); if (size > 0) { sb.append(" head : "); } Node cur = head.next; while (cur != null) { if (cur.next == null) { sb.append(cur.val + " tail"); cur = cur.next; continue; } sb.append(cur.val + "-->"); cur = cur.next; } return sb.toString(); } }
1.测试单链表实例demo
package com.hooware.mylinked; public class MyLinkDemo { public static void main(String[] args) { // test02(); // test03(); // test04(); // test05(); // test06(); // test07(); // test08(); test09(); } public static void test01() { MyLinkedList<Integer> list = new MyLinkedList<>(); System.out.println(list.getSize()); System.out.println(list.isEmpty()); list.addFirst(1); list.addFirst(2); list.addFirst(3); list.addFirst(4); list.addFirst(5); System.out.println(list.getSize()); System.out.println(list.isEmpty()); System.out.println(list); } public static void test02() { MyLinkedList<Integer> list = new MyLinkedList<>(); list.addLast(1); list.addLast(2); list.addFirst(222); list.addLast(3); list.addLast(4); list.addFirst(5); System.out.println(list.getSize()); System.out.println(list); } public static void test03() { MyLinkedList<Integer> list = new MyLinkedList<>(); System.out.println("peek : " + list.peek()); System.out.println("getLast : " + list.getLast()); list.add(10,0); list.add(11,0); list.add(12,0); //System.out.println("getLast : " + list.getLast()); list.add(13,6); list.add(14,2); list.add(100,123); System.out.println(list.getSize()); System.out.println(list); System.out.println("peek : " + list.peek( 8000 )); System.out.println("getLast : " + list.getLast()); } public static void test04() { MyLinkedList<Integer> list = new MyLinkedList<>(); System.out.println("peek : " + list.peek()); System.out.println("getLast : " + list.getLast()); list.add(10,0); list.add(11,0); list.add(12,0); //System.out.println("getLast : " + list.getLast()); list.add(13,6); list.add(14,2); System.out.println(list.getSize()); System.out.println(list); System.out.println("peek : " + list.peek()); System.out.println("getLast : " + list.getLast()); Integer value1 = list.removeFirst(); System.out.println("removeFirst 1 : " + value1); Integer value2 = list.removeFirst(); // list.removeFirst(); // list.removeFirst(); // list.removeFirst(); System.out.println("removeFirst 2 : " + value2); System.out.println(list); } public static void test05() { MyLinkedList<Integer> list = new MyLinkedList<>(); System.out.println("peek : " + list.peek()); System.out.println("getLast : " + list.getLast()); list.add(10,0); list.add(11,0); list.add(12,0); //System.out.println("getLast : " + list.getLast()); list.add(13,6); list.add(14,2); System.out.println(list); Integer value1 = list.removeLast(); System.out.println("removeLast 1 : " + value1); Integer value2 = list.removeFirst(); System.out.println("removeFirst 2 : " + value2); System.out.println(list); System.out.println(list.removeLast()); System.out.println(list.removeLast()); System.out.println(list.removeLast()); System.out.println(list.removeLast()); System.out.println(list); } public static void test06() { MyLinkedList<Integer> list = new MyLinkedList<>(); list.addFirst(1); list.addFirst(2); list.addFirst(3); list.addFirst(4); list.addFirst(5); list.addFirst(6); System.out.println(list); Integer val01 = list.remove(0); System.out.println(val01); System.out.println(list); Integer val02 = list.remove(8); System.out.println("val02 " + val02); System.out.println(list); Integer val03 = list.remove(2); System.out.println("val03 " + val03); System.out.println(list); System.out.println(list.remove(1)); System.out.println(list.remove(1)); System.out.println(list.remove(1)); System.out.println(list.remove(1)); System.out.println(list); } public static void test07() { MyLinkedList<Integer> list = new MyLinkedList<>(); System.out.println(list.updateFirst(100)); list.addFirst(1); list.addFirst(6); System.out.println(list); Integer value = list.updateFirst(10); System.out.println(list); System.out.println(value); } public static void test08() { MyLinkedList<Integer> list = new MyLinkedList<>(); System.out.println(list.updateLast(100)); list.addFirst(1); list.addFirst(6); System.out.println(list); Integer value = list.updateLast(10); System.out.println(list); System.out.println(value); } public static void test09() { MyLinkedList<Integer> list = new MyLinkedList<>(); System.out.println(list.update(100,10)); list.addFirst(1); list.addFirst(2); list.addFirst(3); list.addFirst(4); System.out.println(list); Integer value = list.update(10,2); System.out.println(list); System.out.println(value); System.out.println(list); Integer value1 = list.update(0,0); System.out.println(list); System.out.println 8000 (value1); Integer value2 = list.update(100,4); System.out.println(list); System.out.println(value2); } }
相关文章推荐
- 【Java笔试题】关于ListNode的操作及测试用例的编写,以反转链表操作为例
- 【DataStructure&AlgorithmInJava】Ch05-LinkedListDemo1
- Java数据结构-线性表之单链表LinkedList
- (8) Java源码分析 ---- LinkedList (对应数据结构中线性表中的双向循环链表,JDK1.6)
- HashMap存取效率高原因、ArrayList和LinkedList区别、JAVA实现链表的基本功能
- Java LinkedList特有方法程序小解 && 使用LinkedList 模拟一个堆栈或者队列数据结构。
- 城市链表-JAVA LinkedList
- leetcode解题之206 # Reverse Linked List Java版 (使用头插法反转链表)
- leetcode-328-奇偶链表(odd even linkedlist)-java
- java:手写MyLinkedList所有方法,增删改查
- LeetCode234_PalindromeLinkedList (判断是否为回文链表) Java题解
- 【LeetCode-面试算法经典-Java实现】【142-Linked List Cycle II(单链表中有环II)】
- 自行实现LinkedList (链表) --Java版
- Java基础之泛型——使用泛型链表类型(TryGenericLinkedList)
- Data structure-4 双向链表 DoubleLinkedList--Java语言实现
- java 集合 之 链表和线性表以及ArrayList的各方法的介绍及示例 及 ArrayList与LinkedList的区别分析 及 ArrayList与Array(数组)的区别
- Java程序的单元测试-分析并获取测试用例
- Java数据结构-线性表之单链表LinkedList
- Java基础之集合框架——使用真的的链表LinkedList<>(TryPolyLine)
- java:队列模拟(自定义链表结构 +LinkedList )