Java one-way list operation detailed

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/zxman660/article/details/7786354

——————————————————————————————————————————

  1. /* First define a node class to store the domain and pointer fields of the nodes
  2. * That is, the value in the current node and the method of the back node
  3. * in C is equivalent to defining a struct type a data field and a pointer field method
  4. */
  5. Class lnode{//This is already very fixed. Set two properties using the Set function and the Get function to get these two properties
  6. private int data;
  7. private Lnode Next; //This and string should have a similar usage, the class name is used to represent the data type, which means that the next data type is also the node
  8. public void SetData (int data) {
  9. this.data = data;
  10. }
  11. public int GetData () {
  12. return This.data;
  13. }
  14. public void Setnext (Lnode next) {
  15. This.next = next;
  16. }
  17. Public Lnode GetNext () {
  18. return This.next;
  19. }
  20. }
  21. /*
  22. * Define a list of main classes, and define a variety of methods for the operation of the list
  23. */
  24. Public class Linklist {
  25. Public Lnode head; //Define a head node
  26. /*
  27. * Define a method for creating a linked list
  28. * This method is called: Tail interpolation: The newly generated node inserts the linked list from the tail
  29. */
  30. public void Createlink (int [] a) {
  31. Lnode pnew; //define pnew to represent newly generated nodes .
  32. Lnode ptail=New Lnode (); Allocating heap memory for tail nodes
  33. Head=ptail; //Initially the head node is equal to the tail.
  34. For (int i=0;i<a.length;i++) {
  35. pnew=new Lnode (); Allocating heap memory for newly generated nodes
  36. Pnew.setdata (A[i]); //Pass data value
  37. Ptail.setnext (pnew); //Set the newly created node to the successor of the Ptail
  38. Pnew.setnext (null); Set the successor node of the newly created node to null
  39. Ptail=pnew; //Move the Ptail node position so that it always points to the tail
  40. }
  41. }
  42. /*
  43. * Define a method for determining the existence of elements in a linked list
  44. */
  45. public void Seachlink (int value) {
  46. Lnode ptr;
  47. Ptr=head.getnext ();
  48. while (ptr!=null) {//search for a matching value in the case of non-empty nodes
  49. if (Value==ptr.getdata ()) {//Match succeeded Yes
  50. System.out.println ("Find data:" +ptr.getdata ());
  51. Break ; //exit loop
  52. }
  53. else{//When the current value is not the value you are looking for, find the next
  54. Ptr=ptr.getnext ();
  55. }
  56. }
  57. if (ptr==null)//list traversal completed, when not found
  58. System.out.println ("There is no data to find in the list");
  59. }
  60. /*
  61. * Define a method for deleting nodes
  62. */
  63. public void Deletelink (int value) {
  64. Lnode ptr;
  65. Lnode p;
  66. P=head;
  67. Ptr=head.getnext ();
  68. While (ptr!=null) {
  69. if (Value==ptr.getdata ()) {//Determines whether the current value in the linked list is the node to be deleted
  70. P.setnext (Ptr.getnext ()); //Set the successor node of PTR to the successor of P, that is, the PTR node is deleted in form in the list.
  71. //System.GC ();
  72. System.out.println ("Delete data" +value+ "success!"  ");
  73. Break ;
  74. }
  75. else{
  76. P=ptr; //p pointing to the PTR location
  77. Ptr=ptr.getnext (); //ptr Point to its direct successor position
  78. }
  79. }
  80. if (ptr==null)
  81. System.out.println ("There is no data in the list to delete!")  ");
  82. }
  83. /*
  84. * Methods for defining the insertion node
  85. */
  86. public void Insertlink (int pos,int value) {//two parameters, one representing the insertion position, and the other representing the inserted value
  87. Lnode ptr;
  88. Lnode pnew; //instantiation of new node
  89. Ptr=head.getnext ();
  90. While (ptr!=null) {
  91. if (Pos==ptr.getdata ()) {
  92. pnew=new Lnode ();
  93. Pnew.setdata (value);
  94. Pnew.setnext (Ptr.getnext ());
  95. Ptr.setnext (pnew); //   
  96. System.out.println ("Insert Data" +value+ "success!")  ");
  97. Break ;
  98. }
  99. else{
  100. Ptr=ptr.getnext ();
  101. }
  102. }
  103. if (ptr==null)
  104. System.out.println ("Insert data failed!  ");
  105. }
  106. /*
  107. * Define an output link table content method
  108. */
  109. public void Printlink () {
  110. Lnode ptr; //instantiation of a node
  111. Ptr=head.getnext (); //The node obtains the successor of the head node.
  112. While (ptr!=null) {
  113. System.out.print (Ptr.getdata () +"-");
  114. Ptr=ptr.getnext ();
  115. }
  116. System.out.println ("NULL");
  117. }
  118. /*
  119. * Below is a test case that creates an integer linked list with an array and outputs it
  120. */
  121. public static void Main (String args[]) {
  122. int a[]=new int [10];
  123. For (int i=0;i<a.length;i++) {
  124. A[i]=i;
  125. }
  126. linklist list=new linklist ();
  127. List.createlink (a);
  128. System.out.println ("linked list output is as follows:");
  129. List.printlink ();
  130. System.out.println ("The output of the linked list after inserting an element is as follows:");
  131. List.printlink ();
  132. }
  133. }

Java one-way list operation detailed

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.