LinkedList: bidirectional linked list and implementation.

Source: Internet
Author: User

LinkedList: bidirectional linked list and implementation.

So-called two-way linked list:

  

(From this figure, we can see the deep work of the old man)

A linked list is composed of node connections.

Each node is composed of three parts: the previous node, the elements of the current node, and the next node.

When there is only one node in the linked list, the node points to the previous node is empty, and the next node is empty.

When multiple nodes exist, the previous node of the first node is empty, and the next node of the last node is empty.

For example, the next node of node A points to Node B, and the last node of Node B points to node.

 

Don't talk about it... because my expression skills are not very strong... Let's go directly to the code...

1 public class mydomainlist {2/* first Node */3 private Node first; 4/* last Node */5 private Node last; 6/* size */7 private int size; 8/** 9 * Get the size of this linked list (number of elements) 10 * @ return 11 */12 public int size () {13 return size; 14} 15/** 16 * This method is from the response list. copied from the node (index) method, slightly modified 17 * to return the 18 * @ return 19 */20 private Node node (int index) at the subscript of the pointer) for example, 23 * size = 6; 24 * size> 1 = 3 25 * If ind If ex is less than 3, find the last 26 from the first one * If index is greater than 3, the following code finds the first 27 * from the last one. 28 */29 if (index <(size> 1) {30 Node x = first; 31 for (int I = 0; I <index; I ++) 32 x = x. next; 33 return x; 34} else {35 Node x = last; 36 for (int I = size-1; I> index; I --) 37 x = x. prev; 38 return x; 39} 40} 41/** 42 * add a node 43 * @ param obj add element 44 */45 public void add (Object obj) {46 Node temp = new Node ();// New node 47/* New node element assignment */48 temp. element = obj; 49 50 if (first = null) {// if the first node is empty, no node 51 // since this node is the first node, therefore, the prev and next of the node are empty. Therefore, you do not need to assign a value of 52 //. Similarly, this newly inserted node is the first node and the last one is 53 first = temp; 54 last = temp; 55} else {// otherwise, it means this node is not empty. 56 // The prev of the new node is the last node 57 temp before the node is inserted. prev = last; 58 // while the next of the last node before insertion is the new node 59 //, a chain is formed: The next Of a is B, the last one of B is a, and the next one of a is B ...... 60 last. next = temp; 61 // At the end, the new node is the last node with 62 last = temp; 63} 64 // insert success size ++; 65 size ++; 66} 67/** 68 * add a node at the specified position 69 * @ param index 70 * @ param obj 71 */72 public void add (int index, Object obj) {73 Node temp = node (index); // The resulting Node 74 Node newNode = new Node (); // new Node 75 newNode. element = obj; 76 77 if (temp! = Null) {// if the specified Node is not empty, 78 // obtain the last Node of temp, 79 Node tempPrev = temp. prev; 80 81 82 // The next node of tempPrev is assigned newNode 83 tempPrev. next = newNode; 84 // At the same time, the previous node of newNode is assigned tempPrev 85 newNode. prev = tempPrev; 86 87 88 // then the next node of newNode is the specified temp node 89 newNode at the beginning. next = temp; 90 // The previous node of temp is assigned newNode 91 // so that a new element 92 temp is inserted before the specified element. prev = newNode; 93} 94 size ++; 95} 96/** 97 * Delete 98 * @ param index 99 */10 0 public void remove (int index) {101/* 102 * Delete... 103 * The next node with three elements of a B c 104 * a is B. The next node is c105 * c. The last node is B. The last node is a106 *-- 107 * For example, if you delete b108 *, you need to connect a and c. 109*110 * after the connection is completed, the next Node of 111 * a is c112 * c. The last node is a113 * 114 */115 116 Node temp = node (index ); // obtain the 117 if (temp! = Null) {118/* 119 120 // obtain the 121 Node tempPrev = temp on the last Node of temp. prev; 122 // get temp's next Node 123 Node tempNext = temp. next; 124 // The next node of tempPrev is tempNext125 tempPrev. next = tempNext; 126 // The Last node of tempNext is tempPrev127 tempNext. prev = tempPrev; 128 129 */130 131 // The next node of the previous temp node is the next node of temp 132 temp. prev. next = temp. next; 133 // The Last node of the next node of temp is the 134 temp of the previous node of temp. next. prev = temp. prev; 135} 136 size --; 137} 138/** 139 * obtain the index of the element 140 * @ param index 141 * @ return142 */143 public Object get (int index) based on the subscript) {144 return node (index ). element; // obtain the element of the specified node 145} 146/* optional */147 public static void main (String [] args) {148 mydomainlist list = new mydomainlist (); 149 list. add ("a"); 150 list. add ("B"); 151 list. add (1, "B"); 152 list. remove (1); 153 System. out. println (list. get (1); 154 System. out. println ("Current linked list size:" + list. size ()); 155} 156} 157/** 158 * Node class 159 */160 class Node {161/* 162 * indicates the last Node 163 *. Therefore, the Node type 164 */165 Node prev is used.; 166/* indicates the next Node */167 Node next; 168/* elements of the current Node */169 Object element; 170 171 public Node () {172} 173 174 public Node (Node prev, Node next, Object element) {175 this. prev = prev; 176 this. next = next; 177 this. element = element; 178} 179 180}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.