Transferred from: http://blog.csdn.net/zxman660/article/details/7786354
——————————————————————————————————————————
- /* First define a node class to store the domain and pointer fields of the nodes
- * That is, the value in the current node and the method of the back node
- * in C is equivalent to defining a struct type a data field and a pointer field method
- */
- Class lnode{//This is already very fixed. Set two properties using the Set function and the Get function to get these two properties
- private int data;
- 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
- public void SetData (int data) {
- this.data = data;
- }
- public int GetData () {
- return This.data;
- }
- public void Setnext (Lnode next) {
- This.next = next;
- }
- Public Lnode GetNext () {
- return This.next;
- }
- }
- /*
- * Define a list of main classes, and define a variety of methods for the operation of the list
- */
- Public class Linklist {
- Public Lnode head; //Define a head node
- /*
- * Define a method for creating a linked list
- * This method is called: Tail interpolation: The newly generated node inserts the linked list from the tail
- */
- public void Createlink (int [] a) {
- Lnode pnew; //define pnew to represent newly generated nodes .
- Lnode ptail=New Lnode (); Allocating heap memory for tail nodes
- Head=ptail; //Initially the head node is equal to the tail.
- For (int i=0;i<a.length;i++) {
- pnew=new Lnode (); Allocating heap memory for newly generated nodes
- Pnew.setdata (A[i]); //Pass data value
- Ptail.setnext (pnew); //Set the newly created node to the successor of the Ptail
- Pnew.setnext (null); Set the successor node of the newly created node to null
- Ptail=pnew; //Move the Ptail node position so that it always points to the tail
- }
- }
- /*
- * Define a method for determining the existence of elements in a linked list
- */
- public void Seachlink (int value) {
- Lnode ptr;
- Ptr=head.getnext ();
- while (ptr!=null) {//search for a matching value in the case of non-empty nodes
- if (Value==ptr.getdata ()) {//Match succeeded Yes
- System.out.println ("Find data:" +ptr.getdata ());
- Break ; //exit loop
- }
- else{//When the current value is not the value you are looking for, find the next
- Ptr=ptr.getnext ();
- }
- }
- if (ptr==null)//list traversal completed, when not found
- System.out.println ("There is no data to find in the list");
- }
- /*
- * Define a method for deleting nodes
- */
- public void Deletelink (int value) {
- Lnode ptr;
- Lnode p;
- P=head;
- Ptr=head.getnext ();
- While (ptr!=null) {
- if (Value==ptr.getdata ()) {//Determines whether the current value in the linked list is the node to be deleted
- 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.
- //System.GC ();
- System.out.println ("Delete data" +value+ "success!" ");
- Break ;
- }
- else{
- P=ptr; //p pointing to the PTR location
- Ptr=ptr.getnext (); //ptr Point to its direct successor position
- }
- }
- if (ptr==null)
- System.out.println ("There is no data in the list to delete!") ");
- }
- /*
- * Methods for defining the insertion node
- */
- public void Insertlink (int pos,int value) {//two parameters, one representing the insertion position, and the other representing the inserted value
- Lnode ptr;
- Lnode pnew; //instantiation of new node
- Ptr=head.getnext ();
- While (ptr!=null) {
- if (Pos==ptr.getdata ()) {
- pnew=new Lnode ();
- Pnew.setdata (value);
- Pnew.setnext (Ptr.getnext ());
- Ptr.setnext (pnew); //
- System.out.println ("Insert Data" +value+ "success!") ");
- Break ;
- }
- else{
- Ptr=ptr.getnext ();
- }
- }
- if (ptr==null)
- System.out.println ("Insert data failed! ");
- }
- /*
- * Define an output link table content method
- */
- public void Printlink () {
- Lnode ptr; //instantiation of a node
- Ptr=head.getnext (); //The node obtains the successor of the head node.
- While (ptr!=null) {
- System.out.print (Ptr.getdata () +"-");
- Ptr=ptr.getnext ();
- }
- System.out.println ("NULL");
- }
- /*
- * Below is a test case that creates an integer linked list with an array and outputs it
- */
- public static void Main (String args[]) {
- int a[]=new int [10];
- For (int i=0;i<a.length;i++) {
- A[i]=i;
- }
- linklist list=new linklist ();
- List.createlink (a);
- System.out.println ("linked list output is as follows:");
- List.printlink ();
- System.out.println ("The output of the linked list after inserting an element is as follows:");
- List.printlink ();
- }
- }
Java one-way list operation detailed