Analysis and Development of linked list problems in mobile development
I. Basic Introduction to one-way linked list
A linked list is a data structure at the same level as an array. For example, the implementation principle of ArrayList in Java is array. The implementation principle of tranquility list is linked list. The linked list is inefficient at loop times, but it has obvious advantages in insertion and deletion.
Compared with Arrays: The chain table has unlimited length and does not need to move data items during insertion or deletion. Therefore, although the time complexity of some operations is the same as that of arrays, the actual efficiency is much higher than the array.
The disadvantage is random access, which requires traversing and searching, and cannot directly find specific data items through subscript as an array.
Next we will introduce one-way linked list:
A one-way linked list is a linear table, which is actually composed of nodes. A linked list has an indefinite number of nodes. The data stored in the memory is discontinuous. The data stored in the memory is scattered in the memory. Each node can only know the storage location of the next node. A one-way linked list composed of N nodes. Each Node records the Node data and the next Node. Only one Head is exposed. All the operations on the linked list are carried out directly or indirectly through its header node.
The leftmost node is the Head node, but the order of adding nodes is from right to left. The new node will be used as the new node. The reference of the first added node to the next node can be blank. References are objects that reference the next node rather than the next node. Because of the constant reference, the header node can operate on all nodes.
Describes the storage of one-way linked lists. Storage is scattered. Every node only needs to record the next node, and all data strings are collected to form a one-way linked list.
A Node is composed of an object to be stored and a reference to the next Node. That is to say, the node has two members:Storage objects and references to the next node. The following figure shows a detailed description:
Unidirectional linked list
Package ch04;/** chain Node, equivalent to carriage */public class Node {// data domain public long data; // pointer domain public Node next; public Node (long value) {this. data = value;}/*** display Method */public void display () {System. out. print (data + "") ;}} package ch04;/** linked list, equivalent to train */public class LinkList {// head Node, head private Node first; public LinkList () {first = null;}/*** insert a node after the header node * all nodes contain two fields, data field and pointer field */public void insertFi Rst (long value) {Node node = new Node (value); node. next = first; first = node;}/*** delete a Node and delete it after the header Node */public node deleteFirst () {Node tmp = first; first = tmp. next; return tmp;}/*** display Method */public void display () {Node current = first; while (current! = Null) {current. display (); current = current. next;} System. out. println ();}/*** Query Method */public Node find (long value) {Node current = first; while (current. data! = Value) {if (current. next = null) {return null;} current = current. next;} return current;}/*** delete method, which is used to delete */public Node delete (long value) {Node current = first; Node previous = first; while (current. data! = Value) {if (current. next = null) {return null;} previous = current; current = current. next;} if (current = first) {first = first. next;} else {previous. next = current. next;} return current ;}}
Double-ended linked list
Double-ended linked list:The double-ended linked list is very similar to the traditional linked list. It only adds an Attribute-reference to the last link node.
As shown in: As there is a direct reference to the last link node, the double-ended linked list is more convenient than the traditional linked list in some aspects. For example, you can insert a chain node at the end. The double-ended linked list can be directly operated.
However, the traditional linked list can only find the last link node through the next node loop, so the double-ended linked list is suitable for manufacturing queues.
Package ch04;/** chain Node, equivalent to carriage */public class Node {// data domain public long data; // pointer domain public Node next; public Node (long value) {this. data = value;}/*** display Method */public void display () {System. out. print (data + "") ;}} package ch05; import ch04.Node;/** double-ended linked list */public class FirstLastLinkList {// header Node private Node first; // End Node private Node last; public FirstLastLinkList () {first = null; last = null ;}/ * ** Insert a Node and insert it after the header node */public void insertFirst (long value) {Node = new node (value); if (isEmpty ()) {last = node;} node. next = first; first = node;}/*** insert a node from the End node */public void insertLast (long value) {Node node = new Node (value); if (isEmpty () {first = node;} else {last. next = node;} last = node;}/*** delete a Node. Delete the Node */public node deleteFirst () {node tmp = first; if (fi Rst. next = null) {last = null;} first = tmp. next; return tmp;}/*** display Method */public void display () {Node current = first; while (current! = Null) {current. display (); current = current. next;} System. out. println ();}/*** Query Method */public Node find (long value) {Node current = first; while (current. data! = Value) {if (current. next = null) {return null;} current = current. next;} return current;}/*** delete method, which is used to delete */public Node delete (long value) {Node current = first; Node previous = first; while (current. data! = Value) {if (current. next = null) {return null;} previous = current; current = current. next;} if (current = first) {first = first. next;} else {previous. next = current. next;} return current;}/*** determines whether it is null */public boolean isEmpty () {return (first = null );}}
Two-way linked list
Two-way linked list(Double linked list): In each node of a single form, set a pointer field pointing to the front node. Therefore, the nodes in the two-way linked list have two pointer fields, one pointing to the precursor and the other pointing to the successor.
Compared with a single-chain table, a two-way linked list has a pointer field, which consumes a little more space. However, it has good symmetry, making operations on the front and back nodes of a node easier, it can improve the algorithm's time performance, in exchange for space.
Package ch05;/** two-way linked list */public class DoubleLinkList {// head Node private Node first; // End Node private Node last; public DoubleLinkList () {first = null; last = null;}/*** insert a Node after the header node */public void insertFirst (long value) {Node = new node (value ); if (isEmpty () {last = node;} else {first. previous = node;} node. next = first; first = node;}/*** insert a node from the End node */public void insertLast (l Ong value) {Node node = new Node (value); if (isEmpty () {first = node;} else {last. next = node; node. previous = last;} last = node;}/*** delete a Node and delete it after the header Node */public node deleteFirst () {Node tmp = first; if (first. next = null) {last = null;} else {first. next. previous = null;} first = tmp. next; return tmp;}/*** delete a Node from the end */public Node deleteLast () {Node tmp = last; if (first. next = = Null) {first = null;} else {last. previous. next = null;} last = last. previous; return last;}/*** display Method */public void display () {Node current = first; while (current! = Null) {current. display (); current = current. next;} System. out. println ();}/*** Query Method */public Node find (long value) {Node current = first; while (current. data! = Value) {if (current. next = null) {return null;} current = current. next;} return current;}/*** delete method, which is used to delete */public Node delete (long value) {Node current = first; while (current. data! = Value) {if (current. next = null) {return null;} current = current. next;} if (current = first) {first = first. next;} else {current. previous. next = current. next;} return current;}/*** determines whether it is null */public boolean isEmpty () {return (first = null );}}