One, the node of the linked list
/*
* Chain node, equivalent to a compartment.
*/
public class Node {
Data fields
public Long data;
Pointer field
Public Node Next;
Public Node (Long value) {
This.data = value;
}
/**
* Display method
*/
public void display () {
System.out.print (Data + "");
}
}
Second, simple linked list
/*
* Linked list, equivalent to train
*/
public class Linklist {
Head Knot Point
Private Node first;
Public linklist () {
first = null;
}
/**
* Insert a node and insert it after the head node.
*/
public void Insertfirst (Long value) {
Node node = new node (value);
Node.next = First;
First = node;
}
/**
* Delete a node and delete it after the first node.
*/
Public Node Deletefirst () {
Node tmp = First;
first = Tmp.next;
return TMP;
}
/**
* Display method
*/
public void display () {
Node current = first;
while (current! = null) {
Current.display ();
current = Current.next;
}
System.out.println ();
}
/**
* Find method
*/
Public Node find (Long value) {
Node current = first;
while (current.data! = value) {
if (Current.next = = null) {
return null;
}
current = Current.next;
}
return current;
}
/**
* Delete method, according to the data field to delete
*/
Public Node Delete (Long value) {
Node current = first;
Node previous = first;
while (current.data! = value) {
if (Current.next = = null) {
return null;
}
previous = current;
current = Current.next;
}
if (current = = first) {
first = First.next;
} else {
Previous.next = Current.next;
}
return current;
}
}
Test:
public class Testlinklist {
public static void Main (string[] args) {
linklist linklist = new linklist ();
Linklist.insertfirst (34);
Linklist.insertfirst (23);
Linklist.insertfirst (12);
Linklist.insertfirst (0);
Linklist.insertfirst (-1);
Linklist.display ();
//
Linklist.deletefirst ();
Linklist.display ();
//
Node node = linklist.find (23);
Node.display ();
Node Node1 = linklist.delete (0);
Node1.display ();
System.out.println ();
Linklist.display ();
}
}
This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1696290
Lapping data structures and algorithms-04 linked list