Using java to implement a single-chain table (cainiao issue), java single-chain

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.