packagecom.test.collection;/*** Custom Implementation linklist * 1. Two-way linked list * Implementation principle: the underlying package node object (each node has 3 Parts: 1. The position of the last node, 2. Current node contents, 3. Position of the next Node) * * 2. Query * linklist compared to A Rraylist query efficiency is low: * because the linklist underlying element is not an array, cannot be obtained directly through the index, you need to traverse the index node object from the beginning or from the Tail. * ArrayList can be obtained directly from the Index. * 3. delete, Insert * linklist is more efficient than ArrayList insert, Delete * Linklist directly before and after the link, link to the new object, and ArrayList after the insertion of the subsequent elements need to be a whole shift *@authorChenx **/ public classMylinklist {PrivateNode first; PrivateNode last; public intsize; public voidAdd (Object obj) {node node=NewNode (); if(first = =NULL) {node.prev=NULL; Node.element=obj; Node.next=NULL; first=node; last=node; }Else{ //default directly append to lastnode.prev=last ; Node.element=obj; Node.next=NULL; Last.next=node; last=node; } size++; } /*** Because it is not an array and cannot be obtained directly from the index, it is necessary to traverse the index node object from the beginning or from the tail * therefore, it is slower than ArrayList query *@paramIndex *@return */ publicObject Get (intIndex) {rangecheck (index);//Subscript out of bounds checkNode Temp=node (index);//Get current node returntemp.element; } /*** The linked list is removed, directly interrupted by the link, linked to the new object, * and ArrayList after the deletion of the subsequent elements need to be shifted overall, * therefore: linklist compared to arraylist delete faster *@paramIndex *@paramobj*/ public voidRemoveintIndex) {rangecheck (index);//Subscript out of bounds checkNode Temp=node (index);//Get current nodeNode up=temp.prev; Node down=temp.next; if(up==NULL) { first=down ; }Else{up.next=down ; } if(down==NULL) { last=up ; }Else{down.prev=up ; } size--; } //Get nodeNode Node (intIndex) {//implementing a similar index effect through node traversalNode temp=first ; if(first! =NULL){ if(index < (size >>1) ) {temp=first ; for(inti=0;i<index;i++) {temp=temp.next; } }Else{temp=last ; for(inti=size;i>index;i--) {temp=temp.prev; } } } returntemp; } /*** Linked list inserted, directly interrupted before and after the link, link to the new object; * and the ArrayList insert requires a whole shift of the subsequent elements, * therefore: the linklist phase is faster than ArrayList insertion * @paramIndex *@paramobj*/ public voidAddintIndex,object Obj) {Node Temp=node (index);//Get current nodeNode newnode=NewNode (); Newnode.element=obj; if(temp!=NULL) {Node up=temp.prev; Up.next=newNode; Newnode.prev=up ; Newnode.next=temp; Temp.prev=newNode; Size++; } } public intsize () {returnsize; } //Subscript out of bounds check Private voidRangecheck (intIndex) { if(index<0 | | index>Size) { Try { Throw NewException (); } Catch(Exception E) {e.printstacktrace (); } } } public Static voidmain (string[] Args) {mylinklist mylinklist=Newmylinklist (); Mylinklist.add ("aaa"); Mylinklist.add ("bbb"); Mylinklist.add ("ccc"); Mylinklist.remove (1); for(inti=0;i<mylinklist.size;i++) {System.out.println (mylinklist.get (i)); } }}
Node.java
package com.test.collection; public class node { node prev; Object element; Node next; public node () { } public node (node prev, Object element, node Next) { Super(); This. prev = prev; this. element = element; This. next = next;} }
Java Collection Chapter Ii: linklist