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