A circular link list of Java programming

Source: Internet
Author: User

As usual, a little nonsense, willing to give birth to a gentle and delicate heart, so the girlfriend may be a look, a smile, I know how to do, but the heart and appearance as rough, this life is fortunate to know her, may the world lovers eventually become dependents.

The loop list, as the name suggests, loops, so build a linear table after the tail pointer. Next=first, as usual, first build a struct (similar to the C language structure, Java is a class):

 class   person{ protected   person next;  protected  int   data;  public  person (int   data) { this . Data = data;  public  void   display () {System.out.println ( data /*  *next is a referenced person class, The memory is not allocated on the heap (I'm not sure, the code guesses)  */ 

We then write a list class that operates on the memory space of the person object, similar to the tool class that writes a list:

First of all, you must first establish a head pointer to the first node:

protected person first;

This first class is unreferenced, and when you use an object to refer to this first, be sure to note the consistency of the object, with a constructor below:

 Public link () {        thisnull;    }

The head node is created, and when the object references the tool class, the head node is defined, so here's a way to create a circular list:

 Public voidFun () {Scanner s=NewScanner (system.in); System.out.print ("Please enter a list length, enter as a pure number:"); intValue =Integer.parseint (S.nextline ()); Person P=NULL; First=NewPerson (n--);//It must be the first one to be built, because it starts with a definition.p =First ;  while(n-->0) {P.next=NewPerson (n+1); P=P.next; } P.next= First;

The name of this class is link, see the construction method, the list of tool class is finished.

Here's a code reference, the code is simple:

 Public class Huan {    publicstaticvoid  main (string[] args) {        // TODO auto-generated Method stub        New link ();        L.fun ();         = L.first;          while (p!=null) {            p.display ();             = P.next;}}    }

A circular link list of Java programming

Related Article

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.