This is the second day of my participation in Gwen Challenge

An array of

Array features:

  • Storage is continuous in physical space.
  • The underlying array length is immutable.
  • The array variable refers to the location of the first element in the array.

A [0],a[1] square brackets indicate the offset of the storage address. The numbers inside indicate how many positions are offset

Advantages: Good query performance. (Specify a location to query.) Disadvantages:

  • Because the space must be continuous, if the array is large, when the system has a lot of space debris, it is easy to store no more.
  • Because the length of an array is fixed, its contents are difficult to add and remove.

The list

If you want to pass a list, you have to pass the root of the list, and each node thinks it’s the root.

Linked lists are not spatially continuous. For every value stored, one reference space is expended. Advantages: As long as the memory is large enough, it can be saved, and there is no need to worry about space debris. Lists are easy to add and remove. Disadvantages: Slow query speed (refers to a location query). Each node on the linked list needs to create a reference to next, wasting some space. (When there is more data in a node, this part of the overhead has less memory impact.)

traverse

Traversal: To retrieve and view every element in a collection.

Recursive traversal of arrays:

var arr = [1.2.3.4.5.6.7.8];
function bianArr(arr,i){
    if(arr ! =null || arr.length <= 1) return;
        console.log(arr[i]);
        bainArr(arr, i + 1);   
}
bainArr(arr,0);
Copy the code

Recursive traversal of linked lists:

function Node (value){
    this.value = value;
    this.next = null;
}
var node1 = new Node(1);
var node2 = new Node(2);
var node3 = new Node(3);
var node4 = new Node(4);
var node5 = new Node(5);

node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;

function bainLink(root){
    if(root == null) return;
    console.log(root.value);
    bainLink(root.next);
}
bainLink(node1);
Copy the code