Implement a linked list using java, and implement a linked list using java
A linked list is a non-sequential storage structure of physical storage units. The logical sequence of data nodes is achieved through the pointer connection sequence in the linked list.
Linked List ---- Java Implementation:
1 package com. mianshi. easy; 2 3 public class LinkList {4 5 // The Node class is internal class 6 private static class Node {7 Object data; // data 8 Node next; // reference 9 public Node (Object data) {10 this. data = data; 11 this. next = null; 12} 13} 14 15/** 16 * This member variable head is used to facilitate the compilation of the linked list interface method. For example, 17 * determines whether the linked list is empty, you only need to determine whether the head is equal to null. 18 * to obtain the number of nodes in the linked list, you only need to go down from the head until the next of the last Node is null */19 Node head; 20 21 public LinkList () {22 head = Null; 23} 24 25 public void clear () {// clear linked list 26 head = null; 27} 28 29 public void printAll () {// print the linked list through traversal: print all data 30 Node p = head; 31 while (p! = Null) {32 System. out. print (p. data + ""); 33 p = p. next; 34} 35} 36 37 public boolean isEmpty () {// judge whether the linked list is empty 38 return head = null; 39} 40 41 public int length () {// obtain the chain table length (number of nodes) 42 Node p = head; 43 int sum = 0; 44 while (p! = Null) {45 sum ++; 46 p = p. next; 47} 48 return sum; 49} 50 51 // insert the element at the specified position, subscript starting from 0 52 public void insert (Object data, int position) {53 54 if (position <0 | position> length () {55 throw new IndexOutOfBoundsException ("Access out-of-bounds exception "); 56} 57 Node newNode = new Node (data); 58 if (position = 0) {59 newNode. next = head; 60 head = newNode; 61} else if (position> = length ()-1) {62 get (length ()-1 ). next = newNode; 63} else {64 newNode. next = get (position); 65 get (position-1 ). next = newNode; 66} 67} 68 69 // obtain the Node 70 public Node get (int position) at the specified position) {71 72 if (position <0 | position> = length () {73 throw new IndexOutOfBoundsException ("Access out-of-bounds exception"); 74} 75 if (position = 0) {76 return head; 77} 78 Node p = head; 79 for (int I = 0; I <position; I ++) {80 p = p. next; 81} 82 return p; 83} 84 85 86 // main method, verify the function of the linked list class 87 public static void main (String [] args) {88 89 LinkList ll = new LinkList (); 90 ll. insert (10, 0); 91 ll. insert (20, 1); 92 ll. insert (30, 0); 93 ll. insert (40, 1); 94 ll. insert (50, 4); 95 ll. printAll (); 96 97 System. out. println (); 98 // when the access is out of the range, an exception is thrown, the program does not have try {} catch {} finally {} to process 99 // the program will jump out of the exception and will not run the following part 100 System. out. println (ll. get (5); 101 102 // This part of Code no longer runs 103 ll. insert (90, 0); 104 ll. insert (80, 1); 105 ll. printAll (); 106} 107}
Result:
30 40 10 20 50 Exception in thread "main" java. lang. indexOutOfBoundsException: Access out-of-bounds exception at com. mianshi. easy. linkList. get (LinkList. java: 73) at com. mianshi. easy. linkList. main (LinkList. java: 100)
The key point of a linked list is its next pointer (or reference) for each node. Whether it is an insert, delete, or modify operation, it changes the next point of a node or some nodes.