Using java to implement a single-chain table (cainiao issue), java single-chain
Package code; class Node {Node next; int data; public Node (int data) {this. data = data ;}} class LinkList {Node first; // public LinkList () {this. first = null;} public void addNode (Node no) {no. next = first; first = no; // Add} public void delectNode () {Node n = first. next; first = null; first = n; // Delete in the header} // Delete the specified position public int Number () {int count = 1; // check the number of elements Node nd = first; while (nd. next! = Null) {nd = nd. next; count ++;} return count;} public void delectExact (int n) {// delete a specified location if (n> 1) {int count = 1; node de = first; while (count <n-1) {de = de. next; count ++;} de. next = de. next. next;} else first = first. next;} public void addExact (int n, Node nd) {if (n> 1) // Add the specified position {int count = 1; Node de = first; while (count <n-1) {de = de. next; count ++;} nd. next = de. next; de. next = nd;} else first = first. next ;} Public int findNode (int n) {int count = 1; // find the location where a number corresponds to Node de = first; while (de. data! = N) {de = de. next; count ++; if (de = null) {return-1 ;}} return count ;}public void print () {Node no = first; // print all while (no! = Null) {System. out. println (no. data); no = no. next ;}} public class TextNode {public static void main (String [] args) {LinkList ll = new LinkList (); ll. addNode (new Node (12); ll. addNode (new Node (15); ll. addNode (new Node (18); ll. addNode (new Node (19); ll. addNode (new Node (20);/* System. out. println (ll. first. data); ll. delectNode (); System. out. println (ll. first. data); */System. out. println (ll. number (); ll. delectExact (3); ll. addExact (3, new Node (100); System. out. println (ll. number (); // ll. print (); System. out. println (ll. findNode (112 ));}}
A newbie is a beginner. Please forgive me for any mistakes.