实现带头结点单链表的增删改查

/** * 结点类 */ public class Node { int data; Node next = null; Node tail = null; Node pre = null; public Node(int data) { this.data = https://www.it610.com/article/data; }public Node(int data, Node next) { this.data = data; this.next = next; }@Override public String toString() { return"Node{" + "data="https://www.it610.com/article/+ data +'}'; } }

【实现带头结点单链表的增删改查】实现带头结点的单链表增删改查
public class SingleLinkedList { static Node head = new Node(-1); private static void addToNode(int data, int newData) { Node pre = getPre(data); Node node = new Node(newData); node.next = pre.next; pre.next = node; }private static Node getPre(int data) { Node p = head.next; Node pre = head; while (p != null) { if (p.data =https://www.it610.com/article/= data) { return pre; } pre = p; p = p.next; } return null; }private static void removeData(int data) { Node pre = getPre(data); Node p = getPos(data); pre.next = p.next; }private static void changeData(int data, int newData) { Node pos = getPos(data); pos.data = newData; }//查询data结点 private static Node getPos(int data) { Node p = head.next; while (p != null) { if (p.data == data) { return p; } p = p.next; } return null; }//增加头结点 private static void addFirstNode(int data) { Node node = new Node(data); node.next = head.next; head.next = node; }//遍历 private static void printAllNodes(Node head) { Node p = head.next; while (p != null) { System.out.print(p.data +" "); p = p.next; } }}

    推荐阅读