package step3; /** * Created by zengpeng on 2017/12/25. / public class MyLinkedList {…
- User
package step3;
/** * Created by zengpeng on 2017/12/25. */ public class MyLinkedList {
private Node first;//头结点,不存数据 private Node last;//指向链表的最后一个节点 private int size;
public MyLinkedList() { size = 0; first = new Node(0, null); last = null; }
/** * 添加到链表尾部 * * @param item / public void add(int item) { /********* Begin *********/
/********** End *********/ }
/** * 添加数据item到指定位置index * index从0开始 * @param index * @param item */ public void add(int index, int item) { checkPosIndex(index);
/********** Begin *********/
/********** End *********/ }
/** * 删除指定位置index处的元素并返回, index从0开始 * @param index * @return */ public int remove(int index) { checkPosIndex(index);
/********** Begin *********/
/********** End *********/ }
public int size() { return size; }
private void checkPosIndex(int index) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); } }
//结点内部类 private static class Node { int item; Node next;
Node(int item, Node next) { this.item = item; this.next = next; } } }
- Assistant