A doubly linked list is also called a doubly linked list, which is a list of two pointers in each data node, pointing directly to successive and direct precursors respectively. So, starting from any node in a doubly linked list, it is easy to access its predecessor and successor nodes. The query, from the first node, continues to point to the next node to get its own target node. Delete, insert the same, and finally modify the relationship between the target node, the following is the simulation of the implementation of the process:
Package test; Public classMylinkedlist<e> { //Initialize the node class first Private Static classNode<e>{E element;//node DataNode<E> Pre;//Previous NodeNode<E> Next;//Next Node Information PublicNode (E element,node<e> next,node<e>pre) { This. Element =element; This. Pre =Pre; This. Next =Next; } } Private intSize//the size of the linked list PrivateNode<e> first;//first Node PrivateNode<e> last;//last node /** * Add the default to the end of the list * @param e*/ Public voidAdd (e e) {addatlast (e); } /** * add element to specified position * @param e * @param index*/ Public voidAdd (e E,intindex) { //Check for cross-border firstCheckrangeforadd (index); if(index = = size) {//when the tail is addedAddatlast (e); }Else{Node<E> Curnode =node (index); Addbeforenode (E, Curnode); } } /** * @param index * for elements * @return*/ PublicEGet(intindex) { //Check for cross-border firstCheckrange (index); returnnode (index). element; } /** * Find the subscript of an element * @param elements * @return*/ Public intindexOf (Object Element) {Node<E> cursor =First ; intCount =0; while(NULL!=cursor) { if(NULL!=Element) { if(Element.equals (cursor.element)) {returncount; } }Else{ if(NULL= = Element) {//consider the empty case of the element being searched for returncount; }} Cursor=Cursor.next; Count++; } return-1; } /** * Delete element according to Subscript is, deal with the two-way relationship of the linked list * @param index * @return*/ PrivateE Deletelink (intindex) {Node<E> node =node (index); E element=node.element; Node<E> Prenode =Node.pre; Node<E> NextNode =Node.next; if(NULL= = Prenode) {//When a node is deleted as the first nodeFirst =NextNode; }Else{Prenode.next=NextNode; Node.next=NULL; } if(NextNode = =NULL) {//when the last node is deletedLast =Prenode; }Else{Nextnode.pre=Prenode; Node.pre=NULL; } size--; Node.element=NULL; returnelement; } /** * Delete element according to index * @param index * @return*/ PublicE Remove (intindex) { //Check if array subscript is out of boundsCheckrange (index); returnDeletelink (index); } /** * @param to delete * @return*/ Publicboolean remove (Object o) {intindex =indexOf (o); if(Index <0){ return false; } deletelink (index); return true; } /** * Check if it is out of bounds * @param index*/ Private voidCheckrange (intindex) { if(Index >= Size | | Index <0) { Throw NewIndexoutofboundsexception ("specify index to exceed bounds"); } } /** * Check if it is out of bounds * @param index*/ Private voidCheckrangeforadd (intindex) { if(Index > Size | | Index <0) { Throw NewIndexoutofboundsexception ("specify index to exceed bounds"); } } /** * Add a new element at the end of the list * @param e*/ Private voidAddatlast (e e) {Node<E> Oldlast =Last ; //construct a new nodenode<e> node =NewNode<e> (E,NULL, last); Last=node; if(NULL= = Oldlast) {//when a new element is the first elementFirst =node; }Else{//when a new element is not the first elementOldlast.next =node; } size++; } /** * Add a new element in front of the specified element, maintaining a bidirectional address * @param e * @param curnode*/ Private voidAddbeforenode (E e,node<e>Curnode) {Node<E> Prenode =Curnode.pre; Node<E> NewNode =NewNode<e>(E, Curnode, Prenode); if(NULL= = Prenode) {//when inserted before the first nodeFirst =NewNode; }Else{//Before the first node, you need to maintain the next point of the previous nodePrenode.next =NewNode; } curnode.pre=NewNode; Size++; } /** * To find elements based on index, you can only find them from scratch or start from the tail * @param index * @return*/ PrivateNode<e> node (intindex) {Node<E>node; //using the binary search method, compare the value of index with SIZE/2 to determine whether to start from the beginning or to find it from the tail. if(Index < (size >>1)) {//find it from the beginningnode =First ; for(inti =0; I < index; i++) {node=Node.next; } }Else{//start at the end .node =Last ; for(inti = size-1; i > Index; i--) {node=Node.pre; } } returnnode; } /** * The length of the linked list * @return*/ Public intsize () {return This. Size; } }
Simulation implementation of Java LinkedList