1、定义节点
public class Node {
// 数据
private int value;
// 下一节点
private Node next;
public Node(){}
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
public Node(int value){
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
2、utils方法
public class NodeUtils {
private NodeUtils(){}
/**
* 从当前位置输出链表
* @param node 链表开始输出的位置
* @return
*/
public static String toString(Node node) {
StringBuilder sb = new StringBuilder();
while (node != null) {
sb.append(node.getValue());
node = node.getNext();
if (node == null){
sb.append(" -> end");
}else{
sb.append(" -> ");
}
}
return sb.toString();
}
/**
* 在链表中间插入元素
* @param head 头指针
* @param data 插入的数据
* @param index 插入的位置
* @return
*/
public static Node addNode(Node head,int data,int index){
Node newNode = new Node(data);
if (head == null) {
head = newNode;
head.setNext(null);
}
if (index == 0){
head = headAddNode(head,data);
}
else{
Node preNode = head;
for (int i=0;i<index-1;i++){
preNode = preNode.getNext();
}
Node curNode = preNode.getNext();
newNode.setNext(curNode);
preNode.setNext(newNode);
}
return head;
}
/**
* 在链表的开头插入元素
* @param head 头指针
* @param data 插入的数据
*/
public static Node headAddNode(Node head, int data) {
Node newNode = new Node();
newNode.setValue(data);
newNode.setNext(head);
return newNode;
}
/**
* 在链表的结尾插入数据
* @param head 链表的头指针
* @param data 插入的数据
*/
public static void tailAddNode(Node head, int data) {
Node node = head;
Node pre = null;
while (node != null) {
pre = node;
node = node.getNext();
}
Node newNode = new Node(data, null);
pre.setNext(newNode);
}
/**
* 根据data来修改节点
* @param head 链表头指针
* @param oldData 旧值
* @param newData 新值
*/
public static void updateNodeByValue(Node head,int oldData,int newData){
Node node = head;
while (node != null) {
if (oldData == node.getValue()) {
node.setValue(newData);
}
node = node.getNext();
}
}
/**
* 根据位置来修改节点值
* @param head 链表头指针
* @param index 修改位置
* @param data 新值
*/
public static void updateNodeByIndex(Node head,int index,int data){
Node node = head;
for (int i = 0; i < index; i++) {
node = node.getNext();
}
node.setValue(data);
}
}
3、调用例子
public class Lianbiao {
public static void main(String[] args) {
//单链表
// 头节点
Node head = new Node();
head.setValue(-1);
head.setNext(null);
// 第一个节点
Node firstNode = new Node();
firstNode.setValue(4);
firstNode.setNext(null);
head.setNext(firstNode);
// 第二个节点
Node secondNode = new Node();
secondNode.setValue(10);
secondNode.setNext(null);
firstNode.setNext(secondNode);
head = NodeUtils.headAddNode(head, 1);
head = NodeUtils.addNode(head, 199, 1);
NodeUtils.tailAddNode(head,1888);
//输出链表
System.out.println(NodeUtils.toString(head));
NodeUtils.updateNodeByIndex(head,3,100);
System.out.println(NodeUtils.toString(head));
NodeUtils.updateNodeByValue(head,1888,1234);
System.out.println(NodeUtils.toString(head));
}
}
输出结果:
1 -> 199 -> -1 -> 4 -> 10 -> 1888 -> end
1 -> 199 -> -1 -> 100 -> 10 -> 1888 -> end
1 -> 199 -> -1 -> 100 -> 10 -> 1234 -> end