Jeff Lee blog:http://www.cnblogs.com/alandre/(Mud brick Carpenter), retain the URL when reproduced! Thanks
Linked list is a normal data structure.here I show how to implements it.
Step 1. Define a structure
public class listnode{public ListNode next;public int value;public listnode (int newvalue) {Value = newvalue;}}
Step 2. Implements the functions
public class Clist{private ListNode head;private listnode tail;private listnode current;private int ListCountValue; Public Clist () {listcountvalue = 0; Head = null; Tail = null;} public void Append (int datavalue) {ListNode NewNode = new ListNode (datavalue); if (Listcountvalue = = 0) {Head = NewNode; Tail = NewNode;} Else{tail.next = NewNode; Tail = NewNode;} current = NewNode; Listcountvalue + = 1;} public void Insert (int datavalue) {ListNode NewNode = new ListNode (datavalue); if (Listcountvalue = = 0) {Append (datavalue); return;} if (current = = Tail) {tail.next = NewNode; Tail = NewNode; current = Tail; Listcountvalue + = 1;} if (current! = Head) && (current! = Tail)) {newnode.next = Current.next; Current.next = NewNode; current = NewNode; Listcountvalue + = 1;}} public void Delete () {if (Listcountvalue! = 0) {if (current = = head) {head = Current.next; current = Head; Listcountvalue-= 1;return;} Else{current = Current.next; Listcountvalue-= 1;}}} public void Printalllistnode () {current = head;for (int i = 0; i < ListCountValue; i++) {System.out.println (current.value); current = Current.next;}}}
Step 3. Test Class for testing
public class Test{public static void Main (string[] args) {Clist Clist = new Clist (); Clist. Append (n); CList. Append (CList); Insert (CList); Insert (CList); Delete (); Clist.printalllistnode ();}}
We'll see:
12226633
Singly linked list algorithm implemented by Java