class Node {
public Node next;
public int val;
public Node(int i) {
this.val = i;
}
}
/**
* 单向链表
*
* @author vk
* @date 2016-10-31 21:10
*/
public class SingleLinkedList {
/**头节点*/
private Node headNode;
/**游标节点*/
private Node nextNode;
/**链表长度*/
private int size;
/**
* 插入新的头节点
* @param node 节点对象
* @return true:成功,false:失败,节点对象已存在链表中
*/
public boolean addHeadNode(Node node){
return addNode(node,0);
}
/**
* 按index索引位置前插入节点
* @param node 节点对象
* @param index 索引,范围[0...size-1]
* @return true:成功,游标重置;false:失败,节点对象已存在链表中
*/
public boolean addNode(Node node, int index) {
/*检测目标节点是否已存在链表中,仅检测引用地址是否相同*/
if (contains(node))
return false;
if (headNode == null) {
if (index == 0) {//链表是空,同时往头部插入节点
headNode = node;
size++;
moveToFirst();
return true;
} else
throw new IllegalArgumentException(
"index out of List bounds: size=" + size + " ,index="
+ index);
} else {
if (index == 0) {//链表非空,同时往头部插入节点
node.next = headNode;
headNode = node;
size++;
moveToFirst();
return true;
} else if (index > size - 1)//index越界
throw new IllegalArgumentException(
"index out of List bounds: size=" + size + " ,index="
+ index);
else {
int pos = 0;
Node tmpNode = headNode;
while (pos + 1 < index) {//判断当前节点的下一个索引值是否等于目标索引,index > 1
pos++;
tmpNode = tmpNode.next;
}
node.next = tmpNode.next;
tmpNode.next = node;
size++;
moveToFirst();
return true;
}
}
}
/**
* 移除索引对应的目标节点,返回被移出的节点对象,游标重置
* @param index 索引
* @return 返回目标节点
*/
public Node removeNode(int index) {
Node tmpNode = findNode(index);
if(index ==0){
headNode = tmpNode.next;
}else if(size > 1){
Node preMode = findNode(index -1);
preMode.next = tmpNode.next;
}
size--;
moveToFirst();
return tmpNode;
}
/**
* 按index索引查找目标节点
* @param index 索引
* @return 返回目标节点
*/
public Node findNode(int index) {
if (index >= size || index < 0)
throw new IndexOutOfBoundsException("Invalid index " + index
+ ", size is " + size);
int pos = 0;
Node tmpNode = headNode;
while (pos < index) {
pos++;
tmpNode = tmpNode.next;
}
return tmpNode;
}
/**
* 检测目标节点是否已经存在于链表中
* @param node 目标节点
* @return true:已存在于链表中,false:不存在于链表
*/
public boolean contains(Node node) {
if (node == null)
throw new IllegalArgumentException("node object can not be null");
Node tmpNode = headNode;
while (tmpNode != null) {
if (tmpNode == node)
return true;
tmpNode = tmpNode.next;
}
return false;
}
/**
* 游标移动到头部
*/
public void moveToFirst(){
nextNode = headNode;
}
/**
* 游标移动到指定位置
* @param index 索引
*/
public void moveToPosition(int index){
if (index >= size || index < 0)
throw new IndexOutOfBoundsException("Invalid index " + index
+ ", size is " + size);
int pos = 0;
Node tmpNode = headNode;
while(pos++ < index){
tmpNode = tmpNode.next;
}
nextNode = tmpNode;
}
/**
* 获取游标指定节点,并游标后移+1
* @return 返回当前游标指定节点
*/
public Node next(){
Node tmpNode = nextNode;
if(nextNode != null)
nextNode = nextNode.next;
return tmpNode;
}
/**
* 获取链表长度
* @return 长度
*/
public int size() {
return size;
}
/**
* 清空链表
*/
public void clear(){
size = 0;
nextNode = headNode = null;
}
/**
* 打印链表所有节点
*/
public void printAll() {
if (size == 0)
return;
StringBuilder mBuilder = new StringBuilder();
Node tmpNode = headNode;
while (tmpNode != null) {
mBuilder.append(tmpNode.val);
mBuilder.append('\0');
tmpNode = tmpNode.next;
}
mBuilder.append(" size="+size);
System.out.println(mBuilder.toString());
}
public static void main(String[] args) {
SingleLinkedList mLinkedList = new SingleLinkedList();
Node node = new Node(10);
mLinkedList.addHeadNode(node);
Node node1 = new Node(11);
mLinkedList.addHeadNode(node1);
mLinkedList.addHeadNode(new Node(12));
mLinkedList.addHeadNode(new Node(13));
mLinkedList.printAll();
mLinkedList.addNode(new Node(100), 3);
mLinkedList.printAll();
mLinkedList.removeNode(4);
mLinkedList.printAll();
mLinkedList.addHeadNode(node1);
mLinkedList.addHeadNode(node1);
mLinkedList.printAll();
node = mLinkedList.next();
System.out.println(""+(node != null?node.val:""));
mLinkedList.moveToPosition(3);
node = mLinkedList.next();
System.out.println(""+(node != null?node.val:""));
node = mLinkedList.next();
System.out.println(""+(node != null?node.val:""));
mLinkedList.moveToPosition(1);
node = mLinkedList.next();
System.out.println(""+(node != null?node.val:""));
mLinkedList.clear();
node = mLinkedList.next();
System.out.println("after clear,"+(node != null?node.val:""));
}
}
运行结果: