Data structure--java realization of one-way linked list

Source: Internet
Author: User

Node class:
1 /**2  * @authorZhengbinmac3 * An object of the Onelinknode class represents only one node in the linked list, and the logical relationship of each data element in the linear table is realized by the self-reference of the member variable next. 4  */5  Public classOnelinknode {6     //Save the value of the node7      Public intdata;8     //save references to subsequent nodes9      PublicOnelinknode Next;Ten     //construct a node with a value of K One      PublicOnelinknode (intk) { Adata =K; -Next =NULL; -     } the     //Initialize one-way linked list -      PublicOnelinknode () { -          This(0); -     } +}
One-way linked list class:
1 /**2  * @authorZhengbinmac3 * An object of the Onelink1 class represents a one-way linked list, the member variable head as the head pointer of the linked list, pointing to4 * The first node of a linked list. Head is protected (protected) and can be inherited by its subclasses. 5 * When head is NULL, it means that the list is empty and the number of elements is 0. 6  */7  Public classOnelink1 {8     //point to the first node of a list9     protectedOnelinknode head;Ten     //Create an empty one-way list One      PublicOnelink1 () { AHead =NULL; -     } -     //constructs a one-way linked list that is directed by H1 the      PublicOnelink1 (Onelinknode H1) { -Head =H1; -     } -     /** + * Determine if the linked list is empty -      */ +      Public BooleanIsEmpty () { A         returnHead = =NULL; at     } -     /** - * Set up a one-way list with n random values -      */ -      PublicONELINK1 (intN) { - Onelinknode rear,q; in         if(N > 0) { -             intK = (int) (Math.random () * 100);//generate a random number, join a linked list toHead =NewOnelinknode (k); +Rear =head; -              for(inti = 1;i < n;i++) { theK = (int) (Math.random () * 100); *Q =NewOnelinknode (k); $Rear.next =Q;Panax NotoginsengRear =Q; -             } the         } +     } A     /** the * Returns the length of the linked list +      */ -      Public intLength () { $         intn = 0; $Onelinknode p =head; -          while(P! =NULL) { -n++; thep =P.next; -         }Wuyi         returnN; the     } -     /** Wu * Output Link list -      */ About      Public voidoutput () { $          This. Output (head); -     } -     Private voidoutput (Onelinknode head) { -System.out.print ( This. GetClass (). GetName () + ":"); A          while(Head! =NULL) { + System.out.print (head.data); theHead =Head.next; -             if(Head! =NULL) { $System.out.print ("--"); the             } the         } the System.out.println (); the     } -}
Reversal of one-way linked list:
1 /**2  * @authorZhengbinmac3  */4  Public classOnelink2extendsonelink1{5      PublicOnelink2 () {6         Super();7     }8      PublicONELINK2 (intN) {9         Super(n);Ten     } One      Public voidreverse () { AOnelinknode p = This. Head, q =NULL, front =NULL; -         /* - * Here is an example of this list: the * 1->2->3->4 -          */ -          while(P! =NULL) { -Q = p.next;//p = 1, q = 2; +P.next = Front;//1.next = null; the next loop will change to: 2.next = 1; -Front = p;//front = 1; +p = q;//p = 2; A         } at          This. Head = Front;//after the loop is over, front points to the last node in the original list, -     } -      Public Static voidMain (string[] args) { -ONELINK2 H2 =NewONELINK2 (5); - h2.output (); -System.out.println ("reverse!"); in h2.reverse (); - h2.output (); to     } +}

Online Programming: http://www.nowcoder.com/profile/511012/codeBookDetail?submissionId=3672959

Data structure--java realization of one-way linked list

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.