1. Background
Single-linked list is the most basic data structure, carefully watched for a long time finally figured out, the difference is not each part, each chain is a node object. Requires two parameter positioning: One is index, which indicates the orientation of the object. The other is the object of node.
2. Code
Node class
public class Node {protected node next; protected int data, public Node (int data) {this.data=data;} public void Display ( { System.out.print (data+ "");} }
ArrayList class
public class myArrayList {public node first;//define head node private int pos=0;//node location public myarraylist () {// This.first=null; }//Insert a header node public void Addfirstnode (int data) {Node node=new node (data); Node.next=first; First=node; }//Delete head node public node Deletefirstnode () {node tempnode=first; First=tempnode.next; return tempnode; }//Insert node at any location insert public void add after index (int index, int data) {node node = new node (data); Node current = first; Node previous = first; while (pos! = index) {previous = current; Current = current. Next pos++; } node. Next = current; Previous. Next = node; pos = 0; }//delete nodes at any location public node Deletebypos (int index) {node current = first; Node previous = first; while (pos! = index) {pos++; previous = current; Current = current. Next } if (current = = first) {first = first. Next; } else {pos = 0; Previous. Next = current. Next } return current; } public void Displayallnodes () {Node current = first; while (current = null) {Current.display (); System.out.println (); Current = current. Next } } }
The main function of the implementation:
public class Main {public static void Main (String args[]) { myarraylist ls=new myarraylist (); Ls.addfirstnode (a); Ls.addfirstnode (+); Ls.add (1, 144); Ls.add (2, +); Ls.deletebypos (1); Ls.displayallnodes (); }}
Implementation results:
16
44
15
Reference: http://blog.csdn.net/tayanxunhua/article/details/11100097
/********************************
* This article from the blog "Bo Li Garvin"
* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod
******************************************/
Java implementation of "algorithm data structure Java implementation" single-link list