Version 3 by way of the interior of the single-chain class, so there are two advantages, first: a package; Second: more bonding object-oriented thinking. Define a class node Node Link objectlist inside, Node is responsible for creating and performing an internal junction node related operations, objectlist responsible for performing the operation node. code show as below:
Link categories:
Import java.net.Inet4Address;
/ **
* This is a class list, written in the interior of the node class objectlist even more object-oriented thinking affixed together (by a list consisting of nodes), this class action is the operation node,
* defines the method of operating a node
* /
public class Link {
Private the node the root; // set the first node
private int count = 0; // linked list used to calculate the length of
private int index = 0; // index defined
private String [] returnArray; // define an array of linked list data for outputting
public String [] toArray () {
IF (this.root == null) {
return null;
}
this.index = 0;
this.returnArray = new new String [this.count];
this.root.toArrayNode ();
return this.returnArray;
}
// delete method
public void remove (String data) {
IF (this.contains (Data)) {
IF (data.equals (this.root.data)) {
this.root = this.root.next;
}
} the else {
this.root.next.removeNode (this.root, Data);
// this case the next node is a node of this. the root
} this.count--;
}
// modification method
public void SET (int index, String Data) {
IF (index> (-this.count. 1) || index <0) {
return;
}
this.index = 0 ;
this.root.setNode (index, Data);
}
/ **
* * acquires node information
* /
public String GET (int index) {
IF (index> (-this.count. 1) || index <0) {
null return;
}
this.index = 0;
return this.root.getNode (index);
}
/ **
** pointer node is provided a method
* /
public void setIndex (int index) {
this.index = index;
}
// the resulting list length
public int size () {
return this.count;
}
// determines whether the list is empty
public Boolean isEmpty () {
return this.count == 0;
}
// call the method containsNode node class, according to the transmission determines whether there is the data
public Boolean the contains (String data) {
iF (data == null || this.root == null) {
return to false;
}
this.root.containsNode return (Data);
}
public Link () {
}
method invocation node // class nodes increases
// increase is to be noted that a node is a node for this operation, so that a node only consider
public void the Add (String Data) {
IF (Data == null) {
return;
}
the Node the Node new new Node = (Data);
this.count ++; // count out to create the Node ++
IF (this.root == null) {
this.root = Node;
} the else {
this.root.addNode (Node);
}
}
// * ************************************************** ***************
/ **
** this is a node class, the role of this is to create a class of nodes
* /
Private the node class {
Private Data String;
Private Next the node ;
// configuration parameters have
public Node (String data) {
Data = this.data;
}
// constructor with no arguments
public the Node () {
}
/ **
* array output into the linked list data
* /
public void toArrayNode () {
Link.this.returnArray [Link.this.index ++] = this.data;
IF (this.next = null!) {
this.next.toArrayNode ();
}
}
// first time (link) previous = this.root call; = Link.root.next the this
// second previous = this.root.next call times (the Node); = Link.root.next.next.next the this
public void the removeNode (Previous the Node, String Data) {
IF (this.data.equals (Data)) {
previous.next = this.next;
the else {}
this.next.removeNode (Previous, Data); // this.next parent node is the this
}
}
/ **
* * node information setting method
* /
public void setNode (int index, String Data) {
IF (index == Link.this.index ++) {
this.data = Data;
} the else {
this.next.setNode (index, Data);
}
}
/ **
** method of acquiring node information
* /
public String the getNode (int index) {
IF (index == Link.this.index ++) {
return this.data;
} the else {
this.next.getNode return (index);
}
}
/ **
* * search method determines whether the number is present
* /
public Boolean containsNode (String Data) {
IF (data.equals (this.data)) {
return to true;
} {the else
IF (! this.next = null) {
return this.next.containsNode (Data);
} the else {
return to false;
}
}
}
/ **
* increased node
* /
// first call (Link): = Link.root the this
// second call (Node): this = Link.root.next
public void addNode(Node node) {
if(this.next==null) {
this.next=node;
}else {
this.next.addNode(node);
}
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}
LinkeText类:
/ **
*
* /
Package lianbiaoDemo03;
public class linkText {
public static void main (String [] args) {
Link Link Link new new = ();
link.add ( "gourd. 1");
link.add ( "gourd 2 ");
link.add (" gourd. 3 ");
link.add (" gourd. 4 ");
link.add (" gourd. 5 ");
// System.out.println (link.size () );
// System.out.println (link.isEmpty ());
// System.out.println (link.contains ( "gourd doll 4"));
// System.out.println (link.get (1 ));
// link.set (1, "Monkey King");
// System.out.println (link.get (1));
// link.remove ( "gourd doll 3");
// System.out.println(link.get(2));
String [] s=link.toArray();
for (int i = 0; i < s.length; i++) {
System.out.println(s[i]);
}
}
}