Link table simple example

xiaoxiao2021-03-06  19

Package test;

Import java.util. *;

public class Tiger9 {static class LinkedList {private Node header; private int size; public LinkedList () {header = new Node (null, null); size = 0;} public void add (Object o) {Node n = new Node ( o, header; // Key step. Create a new Node object, its next variable will point to the HEADER object to add it. HEADER = n; // enables the newly created Node object a new Header object.

SIZE ;} public object remove () {Object o = header.object; header = header.next; size-; return;} public object get (int index) {if (index <0 || index> size) { Throw new arrayindexoutofboundsexception ("index =" index);} node n = header; for (int i = 0; i

[DDDD, CCCC, BBBB, AAAA] 4i = 0, Object = DDDI = 1, Object = CCCCI = 2, Object = Bbbbi = 3, Object = AAAAREMOVE: DDDD, [CCCC, BBBB, AAAA] Remove: CCCC, [BBBB , AAAA] REMOVE: BBBB, [AAAA] Remove: aaaa, []

in conclusion:

1. For the linked list, it is very easy to add and delete elements. In contrast, every element is added or deleted in a linear table, you have to create a new Size array and copy the original element to the new array. Second, on the other hand, in the linear table, since the array is used as a warehouse of the access element, it is very easy to implement according to the index. Relatively, in the linked list, it must be experienced frequent traversal experience to index to the corresponding position.

Third, the corresponding data structure should be selected according to the specific conditions. such as:

If you only need to quickly add an element and sequentially access the element through the traversal method, you do not involve an index operation, and it is recommended to use a list.

If it is necessary to frequently access elements according to the index, although it is insufficient in addition to the addition and deletion, the performance of the linear table will still be better than the linked list.

转载请注明原文地址:https://www.9cbs.com/read-46483.html

New Post(0)