Consolidating the data structure single-link list Java implementation single-link list in addition to the footer each point has a successor node has data and subsequent pointers are composed by building the header and footer (trailing append required) two special points to implement a single-linked list of some operations, the code is as follows
Package com.shine.test.datastruct;/** * Simple linked list * * @author shine * * @param <E> */public class Linklist<e> {pri Vate Node Head, tail;private int size = 0;protected class Node<e> {E data; Node Next;} Public linklist () {head = new Node (); head.next = Null;tail = new node<e> ();} public void Insert (e e, int index) {Node NewNode = new node<e> (); newnode.data = E; Node before = head; Node temp = head.next;while (Index >= 0) {if (index = = 0) {Before.next = Newnode;newnode.next = Temp;size++;break;} before = Temp;temp = temp.next;index--;}} public void Add (e e) {node node = new Node (); node.data = E;node.next = null;if (Head.next = = null) {head.next = node;} Tail.next = Node;tail = node;size++;} Public E get (int index) {if (Index > size-1) {return null;} Node temp = head.next;while (Index >= 0) {if (index = = 0) {return (E) Temp.data;} temp = temp.next;index--;} return null;} Public E GetFirst () {return get (0);} Public E GetLast () {return get (size-1);} public void Remove (E e) {Node before = head; Node temp = head.next;while (temp! = null) {if (Temp.data.equals (e)) {before.next = Temp.next;size--;break;} before = Temp;temp = Temp.next;}} @Overridepublic String toString () {stringbuffer sb = new StringBuffer (); Node temp = head.next;while (temp! = null) {sb.append (Temp.data + "); temp = Temp.next;} return sb.tostring ();} public int GetLength () {return size;}}
Simple implementation of single-linked list Java