//其他跟单向列表一样,add操作需要注意的地方
public void add(int index, E element) {
// TODO Auto-generated method stub
rangeCheekForAdd(index);
if (index == 0) {
Node<E> newFirst = new Node<>(element, first);
//拿到最后一个节点
Node<E> last = (size == 0)? newFirst:node(size-1);
last.next = newFirst;
first = newFirst;
}else {
Node<E> prev = node(index - 1);
prev.next = new Node<> (element, prev.next);
}
size ++;
}
public E remove(int index) {
// TODO Auto-generated method stub
Node<E> node = first;
if (index == 0) {
if (size == 1) {
first = null;
}else {
Node<E> last = node(size-1);
first = first.next;
last.next = first;
}
}else {
Node<E> prev = node(index - 1);
node = prev.next;
prev.next = node.next;
}
size --;
return node.element;
}
代码如下:
package com.weyan;
public class SingleCircleLinkedList<E> implements List<E> {
private static final int ELEMENT_NOT_FOUND = -1;
private int size;
private Node<E> first;
//内部类
private static class Node<E> {
E element;
Node<E> next;
//构造函数
public Node(E element, Node<E> next) {
super();
this.element = element;
this.next = next;
}
}
@Override
public void clear() {
// TODO Auto-generated method stub
size = 0;
first = null;
}
@Override
public int size() {
// TODO Auto-generated method stub
return size;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return size == 0;
}
@Override
public boolean contains(E element) {
// TODO Auto-generated method stub
return indexOf(element) != ELEMENT_NOT_FOUND;
}
@Override
public void add(E element) {
// TODO Auto-generated method stub
add(size, element);
}
@Override
public E get(int index) {
// TODO Auto-generated method stub
return node(index).element;
}
@Override
public E set(int index, E element) {
// TODO Auto-generated method stub
Node<E> node = node(index);
E old = node.element;
node.element = element;
return old;
}
@Override
public void add(int index, E element) {
// TODO Auto-generated method stub
rangeCheekForAdd(index);
if (index == 0) {
Node<E> newFirst = new Node<>(element, first);
//拿到最后一个节点
Node<E> last = (size == 0)? newFirst:node(size-1);
last.next = newFirst;
first = newFirst;
}else {
Node<E> prev = node(index - 1);
prev.next = new Node<> (element, prev.next);
}
size ++;
}
@Override
public E remove(int index) {
// TODO Auto-generated method stub
Node<E> node = first;
if (index == 0) {
if (size == 1) {
first = null;
}else {
Node<E> last = node(size-1);
first = first.next;
last.next = first;
}
}else {
Node<E> prev = node(index - 1);
node = prev.next;
prev.next = node.next;
}
size --;
return node.element;
}
@Override
public int indexOf(E element) {
// TODO Auto-generated method stub
Node<E> node = first;
if (element == null) {
for (int i = 0; i < size; i++) {
if (node.element == null) return i;
node = node.next;
}
}else {
for (int i = 0; i < size; i++) {
if (element.equals(node.element)) return i;
node = node.next;
}
}
return ELEMENT_NOT_FOUND;
}
/**
* 获取index位置对应的节点对象
*/
private Node<E> node(int index) {
rangeCheck(index);
Node<E> node = first;
for (int i = 0; i < index; i++) {
node = node.next;
}
return node;
}
private void outOfBounds(int index) {
throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size);
}
private void rangeCheck(int index) {
// TODO Auto-generated method stub
if (index < 0 || index >= size) {
outOfBounds(index);
}
}
public void rangeCheekForAdd(int index) {
// TODO Auto-generated constructor stub
if (index < 0 || index > size) {
outOfBounds(index);
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("size=").append(size).append(",[");
Node<E> node = first;
for (int i = 0; i < size; i++) {
if (i != 0) {
stringBuilder.append(", ");
}
stringBuilder.append(node.element);
node = node.next;
}
stringBuilder.append("]");
return stringBuilder.toString();
}
}
验证结果:
网友评论