import java.util.Arrays;
public class MyLinkList {
static private Node head = null;
/**
* 头部增加元素
*/
public void insertHead(String value){
Node node = new Node(value);
if (head == null){
head = node;
} else {
// 如果头不为空,则将当前节点的下一个节点设置为原本的头节点,并将头节点设置为当前节点
node.nextNode = head;
head = node;
}
}
/**
* 向list尾部增加元素
*/
public void insert(String value){
Node node = new Node(value);
if (head == null){
insertHead(value);
} else {
Node cur = head;
while (cur.nextNode != null){
cur = cur.nextNode;
}
// 遍历得到最后一个元素
cur.nextNode = node;
}
}
/**
* 为第N个位置增加元素,n从0开始
* @param value
*/
public void insetNth(int n, String value){
Node node = new Node(value);
// 如果是头元素,则插入头
if (n == 0){
insertHead(value);
return;
}
// 不是头元素
Node cur = head.nextNode;
int nth = 1;
while(cur != null){
if (nth == n - 1){
// 已找到指定位置的前一位置,将指定位置的元素设置为当前节点的下一元素,将原本此位置设置为当前节点
node.nextNode = cur.nextNode;
cur.nextNode = node;
break;
}
cur = cur.nextNode;
nth ++;
}
}
/**
* 删除头节点
*/
public void deleteHead(){
if (head != null && head.nextNode != null){
head = head.nextNode;
} else {
head = null;
}
}
/**
* 删除指定位置的节点,n从0开始
* @param n
*/
public void deleteNth(int n){
if (n == 0){
deleteHead();
} else {
int nth = 0;
Node cur = head;
while (cur.nextNode != null){
if (nth == n - 1){
cur.nextNode = cur.nextNode.nextNode;
return;
}
cur = cur.nextNode;
nth ++;
}
}
}
/**
* 删除最后一个节点
*/
public void deleteTail(){
if (head != null && head.nextNode != null){
Node cur = head;
Node nextNode;
while (cur.nextNode != null){
nextNode = cur.nextNode;
// 遍历得到了最后的尾节点
if(nextNode.nextNode == null) {
cur.nextNode = null;
return;
}
cur = nextNode;
}
} else if (head != null){
// 原本就只有头节点
head = null;
}
}
/**
* 删除所有值为value的元素
*/
public void deleteValue(String value){
if(head != null && head.nextNode != null){
Node cur = head;
while(cur.nextNode != null){
// 如果当前值的下一个节点的值与要删除的值相同,则将当前节点的下一节点设置为下一节点的下一节点
if(cur.nextNode.value == value){
cur.nextNode = cur.nextNode.nextNode;
}
cur = cur.nextNode;
}
// 上面的while循环没有遍历头节点
if(head.value == value){
deleteHead();
}
}
}
/**
* 更新头节点
* @param value
*/
public void updateHead(String value){
if(head != null){
head.value = value;
}
}
/**
* 更新第n位置节点的值
* @param n
* @param value
*/
public void updateNth(int n, String value){
if(n == 0){
updateHead(value);
} else {
Node cur = head;
int nth = 0;
while (cur.nextNode != null){
cur = cur.nextNode;
nth ++;
if (nth == n){
cur.value = value;
return;
}
}
}
}
/**
* 更新所有值为value的元素
* @param originalValue
* @param newValue
*/
public void updateValue(String originalValue, String newValue){
if(head != null && head.nextNode != null){
Node cur = head;
while (cur.nextNode != null){
if(cur.value == originalValue){
cur.value = newValue;
}
cur = cur.nextNode;
}
// 上面的遍历不包含尾节点
if(cur.nextNode == null){
if(cur.value == originalValue){
cur.value = newValue;
}
}
}
}
/**
* 查询头节点的值
* @return
*/
public String queryHead(){
if (head != null){
return head.value;
}
return "no head";
}
/**
* 查询第n个节点的值
* @param n
* @return
*/
public String queryNth(int n){
if(n == 0){
return queryHead();
} else {
if (head != null && head.nextNode != null){
Node cur = head;
int nth = 0;
while(cur.nextNode != null){
if (nth == n){
return cur.value;
}
cur = cur.nextNode;
nth ++;
}
}
}
return "n > LinkList.length";
}
/**
* 查询所有值为value的节点位置
* @param value
*/
public int[] queryValue(String value){
int [] index = new int[5];
if (head != null && head.nextNode != null){
Node cur = head;
int nth = 0;
int point = 0;
while(cur.nextNode != null){
if (value.equals(cur.value)){
index[point ++] = nth;
}
cur = cur.nextNode;
nth ++;
}
// 以上遍历不包含尾节点
if (cur.nextNode == null){
if (value.equals(cur.value)){
index[point ++] = nth;
}
}
}
return index;
}
@Override
public String toString(){
StringBuffer strBuffer = new StringBuffer();
if (head != null){
Node cur = head;
int nth = 0;
while (cur.nextNode != null){
strBuffer.append("[").append(nth).append("]:").append(cur.value).append(",");
cur = cur.nextNode;
nth ++;
}
// 最后一个元素
if (cur.nextNode == null){
strBuffer.append("[").append(nth).append("]:").append(cur.value);
}
return strBuffer.toString();
} else {
return "Empty LinkList";
}
}
public static void main(String[] args) {
MyLinkList myLinkList = new MyLinkList();
myLinkList.insert("Zero");
myLinkList.insert("One");
myLinkList.insert("Two");
myLinkList.insert("Three");
myLinkList.insert("Four");
System.out.println(myLinkList.toString());
myLinkList.insertHead("NewHead");
System.out.println(myLinkList.toString());
myLinkList.insetNth(4,"Nth");
System.out.println(myLinkList.toString());
myLinkList.deleteHead();
System.out.println(myLinkList.toString());
myLinkList.deleteNth(3);
System.out.println(myLinkList.toString());
myLinkList.deleteNth(4); // 使用deleteNth()方法删除尾部
System.out.println(myLinkList.toString());
myLinkList.deleteTail();
System.out.println(myLinkList.toString());
myLinkList.deleteTail();
System.out.println(myLinkList.toString());
myLinkList.deleteTail();
System.out.println(myLinkList.toString());
myLinkList.deleteTail();
System.out.println(myLinkList.toString());
// 重新载入,测试deleteValue方法
myLinkList.insert("Zero");
myLinkList.insert("One");
myLinkList.insert("Two");
myLinkList.insert("Zero");
myLinkList.insert("Four");
System.out.println(myLinkList.toString());
myLinkList.deleteValue("Zero");
System.out.println(myLinkList.toString());
myLinkList.updateHead("NewHead");
System.out.println(myLinkList.toString());
myLinkList.updateNth(1,"NewOne");
System.out.println(myLinkList.toString());
myLinkList.updateNth(2,"NewTail");
System.out.println(myLinkList.toString());
// 重新载入值,用来测试updateValue
myLinkList.insert("Two");
myLinkList.insert("Three");
myLinkList.insert("Four");
myLinkList.insert("Two");
myLinkList.insert("Three");
System.out.println(myLinkList.toString());
myLinkList.updateValue("Two","NewTwo");
System.out.println(myLinkList.toString());
System.out.println("LinkList's Head:" + myLinkList.queryHead());
System.out.println("LinkList's 2th:" + myLinkList.queryNth(2));
System.out.println("LinkList's index of 'NewTwo':" + Arrays.toString(myLinkList.queryValue("NewTwo")));
System.out.println("LinkList's index of 'Three':" + Arrays.toString(myLinkList.queryValue("Three")));
}
}
class Node{
String value = null;
Node nextNode = null;
public Node(String value) {
this.value = value;
this.nextNode = null;
}
}
结果:
[0]:Zero,[1]:One,[2]:Two,[3]:Three,[4]:Four
[0]:NewHead,[1]:Zero,[2]:One,[3]:Two,[4]:Three,[5]:Four
[0]:NewHead,[1]:Zero,[2]:One,[3]:Two,[4]:Nth,[5]:Three,[6]:Four
[0]:Zero,[1]:One,[2]:Two,[3]:Nth,[4]:Three,[5]:Four
[0]:Zero,[1]:One,[2]:Two,[3]:Three,[4]:Four
[0]:Zero,[1]:One,[2]:Two,[3]:Three
[0]:Zero,[1]:One,[2]:Two
[0]:Zero,[1]:One
[0]:Zero
Empty LinkList
[0]:Zero,[1]:One,[2]:Two,[3]:Zero,[4]:Four
[0]:One,[1]:Two,[2]:Four
[0]:NewHead,[1]:Two,[2]:Four
[0]:NewHead,[1]:NewOne,[2]:Four
[0]:NewHead,[1]:NewOne,[2]:NewTail
[0]:NewHead,[1]:NewOne,[2]:NewTail,[3]:Two,[4]:Three,[5]:Four,[6]:Two,[7]:Three
[0]:NewHead,[1]:NewOne,[2]:NewTail,[3]:NewTwo,[4]:Three,[5]:Four,[6]:NewTwo,[7]:Three
LinkList's Head:NewHead
LinkList's 2th:NewTail
LinkList's index of 'NewTwo':[3, 6, 0, 0, 0]
LinkList's index of 'Three':[4, 7, 0, 0, 0]