若是是null,就说明后面没有节点了,若是不是null就说明后面要链接节点
每一个节点包含2部分,数据域,和一个指向下一个节点引用的指针next
data域--存放结点值的数据域
next域--存放结点的直接后继的地址的指针域(链域)
链表经过每一个结点的链域将线性表的n个结点按其逻辑顺序连接在一块儿的,每一个结点只有一个链域的链表称为单链表(Single Linked List)。
单链表
显示全部节点信息spa
/** * 添加结点至链表头部 * * @param value */ public void addHeadNode(T value) { Node newNode = new Node(value); //头结点不存在,新结点成为头结点 if (head == null) { head = newNode; return; } //新结点next直接指向当前头结点 newNode.next = head; //新结点成为新的头结点 head = newNode; }
/** * 添加结点至链表尾部 * * @param value */ public void addTailNode(T value) { Node newNode = new Node(value); //头结点不存在,新结点成为头结点 if (head == null) { head = newNode; return; } //找到最后一个结点 Node last = head; while (last.next != null) { last = last.next; } //新结点插入到链表尾部 last.next = newNode; }
/** * 结点插入至指定位置 * * @param value 结点数据 * @param index 插入位置 */ public void addNodeAtIndex(T value, int index) { if (index < 0 || index > size()) { //注意index是能够等于size()的 throw new IndexOutOfBoundsException("IndexOutOfBoundsException"); } if (index == 0) { //插入到头部 addHeadNode(value);//前面定义的方法 } else if (index == size()) { //插入到尾部 addTailNode(value);//前面定义的方法 } else { //插到某个中间位置 Node newNode = new Node(value); int position = 0; Node cur = head; //标记当前结点 Node pre = null; //记录前置结点 while (cur != null) { if (position == index) { newNode.next = cur; pre.next = newNode; return; } pre = cur; cur = cur.next; position++; } } }
插入一个节点作当前节点的下一个节点.net
/** * 删除指定位置的结点 * * @param index 指定位置 */ public void deleteNodeAtIndex(int index) { if (index < 0 || index > size() - 1) { throw new IndexOutOfBoundsException("IndexOutOfBoundsException"); } if (index == 0) { //删除头 head = head.next; return; } int position = 0; //记录当前位置 Node cur = head; //标记当前结点 Node pre = null; //记录前置结点 while (cur != null) { if (position == index) { pre.next = cur.next; cur.next = null; //断开cur与链表的链接 return; } pre = cur; cur = cur.next; position++; } }
/** * 链表反转 */ public void reverse() { Node cur = head; //标记当前结点 Node pre = null; //标记当前结点的前一个结点 Node temp; while (cur != null) { //保存当前结点的下一个结点 temp = cur.next; //cur.next指向pre,指针顺序置换 cur.next = pre; //pre、cur继续后移 pre = cur; cur = temp; } //最后一个结点变成新的头结点 head = pre; }
/** * 倒数第k个结点 * * @param k * @return */ public T getLastK(int k) { if (k < 0 || k > size()) { //注意index是能够等于size()的 throw new IndexOutOfBoundsException("IndexOutOfBoundsException"); } Node cur = head; for (int i = 1; i < size() - k + 1; i++) { cur = cur.next; } return cur.value; }
循环链表指针
双向链表code
循环双向列表一开始的pre和next都是本身blog