A linked list is a non-sequential, non-sequential storage structure on a physical storage unit, and the logical order of the data nodes is achieved by the order of the pointers in the linked list.
Linked list----Java implementation:
1 PackageCom.mianshi.easy;2 3 Public classlinklist {4 5 //the node class is an inner class6 Private Static classNode {7Object data;//Data8Node Next;//a reference to the next data9 PublicNode (Object data) {Ten This. data =data; One This. Next =NULL; A } - } - the /** - * This member variable head is designed to facilitate the programming of the linked list interface method, for example: - * Determine if the list is empty, just determine if the head is equal to null; - * To get the number of nodes in the list, simply go down from head until the last node's next is null, etc.*/ + Node head; - + Publiclinklist () { AHead =NULL; at } - - Public voidClear () {//Empty List -Head =NULL; - } - in Public voidPrintall () {//traverse the Print List: Print all the data -Node p =head; to while(P! =NULL){ +System.out.print (p.data+ ""); -p =P.next; the } * } $ Panax Notoginseng Public BooleanIsEmpty () {//determine if the linked list is empty - returnHead = =NULL; the } + A Public intLength () {//Get the list length (number of nodes) theNode p =head; + intsum = 0; - while(P! =NULL){ $sum++; $p =P.next; - } - returnsum; the } - Wuyi //inserts an element at the specified position, starting with 0 the Public voidInsert (Object data,intposition) { - Wu if(Position < 0 | | position >Length ()) { - Throw NewIndexoutofboundsexception ("Access out of Bounds exception"); About } $Node NewNode =NewNode (data); - if(Position = = 0){ -Newnode.next =head; -Head =NewNode; A}Else if(Position >= Length ()-1){ +Get (Length ()-1). Next =NewNode; the}Else{ -Newnode.next =get (position); $Get (position-1). Next =NewNode; the } the } the the //Gets the node at the specified location - PublicNode Get (intposition) { in the if(Position < 0| | Position >=Length ()) { the Throw NewIndexoutofboundsexception ("Access out of Bounds exception"); About } the if(Position = = 0){ the returnhead; the } +Node p =head; - for(inti=0; i<position; i++){ thep =P.next;Bayi } the returnp; the } - - the //Main method, verifying the functionality of the list class the Public Static voidMain (string[] args) { the thelinklist LL =Newlinklist (); -Ll.insert (10, 0); theLl.insert (20, 1); theLl.insert (30, 0); theLl.insert (40, 1);94Ll.insert (50, 4); the Ll.printall (); the the System.out.println ();98 //when you access an out-of-bounds location, an exception is thrown and the program does not have try{}catch{}finally{} processing About //The program jumps out of the exception and does not run the following section -System.out.println (Ll.get (5));101 102 //This part of the code is no longer running103Ll.insert (90, 0);104Ll.insert (80, 1); the Ll.printall ();106 }107}
Results:
"main" java.lang.IndexOutOfBoundsException: Access out of bounds exception at Com.mianshi.easy.LinkList.get ( Linklist.java:() at Com.mianshi.easy.LinkList.main (linklist.java:100)
The key point of a list is that its next pointer (or reference) to each node, whether it is an insert, delete, or modify operation, is to change the next point of a node or nodes.
Implementing a linked list in Java