Java Analog Single-link list operations

Source: Internet
Author: User

Node class

public class ListNode {
int data;//Data
ListNode next;//Hands
public ListNode (int x) {data=x;}
public int GetData () {
return data;
}
public void SetData (int data) {
This.data = data;
}
Public ListNode GetNext () {
return next;
}
public void Setnext (ListNode next) {
This.next = Next;
}

}

Node Operation:
public class Listnodeutil {//linked list

public static ListNode createheadaddnodelist (int[] dataarray) {//head interpolation insert data
ListNode headnode=new listnode (0);//Create a head node
Headnode.setnext (NULL);
for (int i=0;i<dataarray.length;i++) {
ListNode newnode=new ListNode (dataarray[i]);//assignment data, creating a new node
Newnode.setnext (Headnode.getnext ());//The point of the previous node is assigned to the next node
Headnode.setnext (NewNode);//head node points to tail nodes
}
return headnode;
}
public static ListNode createendaddnodelist (int[] dataarray) {//head interpolation insert data
ListNode headnode=new listnode (0);//Create a head node
Headnode.setnext (NULL);
ListNode pointnode=headnode;//pointer node always points to tail node
for (int i=0;i<dataarray.length;i++) {
ListNode newnode=new ListNode (Dataarray[i]);
Newnode.setnext (Pointnode.getnext ());
Pointnode.setnext (NewNode);
Pointnode=newnode;
}
return headnode;
}

public static void Printheadaddnodelist (ListNode start) {

  if(start!=null){      System.out.println(start.getData());      start=start.getNext();      printHeadAddNodeList(start);  }

}

public static void Deletenodebyindex (ListNode startnode,int start,int end) {//delete node start value input 0
if (startnode==null) {return;}
if (start<end) {//swipe down from top
start++;
Startnode=startnode.getnext ();
Deletenodebyindex (startnode,start,end);//Recursive delete
}else{
Startnode.setdata (Startnode.getnext (). GetData ());
Startnode.setnext (Startnode.getnext (). GetNext ());
}
}

public static void Putnode (ListNode startnode,listnode datanode,int start,int end) {//Parameter 1 start node parameter 2 data node

  if(startNode==null){return;}  if(start<end){      start++;      startNode=startNode.getNext();      putNode(startNode,dataNode,start,end);  }else{      dataNode.setNext(startNode.getNext());      startNode.setNext(dataNode);  }

}

}

Java simulates single-linked list operations

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.