Home > Backend Development > PHP Problem > What is the difference between linked list and array in php

What is the difference between linked list and array in php

青灯夜游
Release: 2023-03-11 10:42:02
Original
1695 people have browsed it

Difference: 1. Arrays allocate memory statically, and linked lists dynamically allocate memory; 2. Arrays are continuous in memory, and linked lists are not continuous; 3. Array elements are in the stack area, and linked list elements are in the heap area; 4. Array positioning The time complexity of elements is O(1), and that of linked lists is O(n); 5. The time complexity of inserting or deleting elements from arrays is O(n), and that of linked lists is O(1).

What is the difference between linked list and array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

What is a linked list

The linked list is a non-continuous and non-sequential storage structure on the physical storage unit. The logical order of the data elements is realized through the pointer link order in the linked list.

The linked list consists of a series of nodes (each element in the linked list is called a node), and the nodes can be dynamically generated at runtime.

Each node includes two parts: one is the data field that stores data elements, and the other is the pointer field that stores the address of the next node.

What is an array

An array is a collection of data that organizes a series of data to form an operable whole. Arrays in PHP are complex but more flexible than arrays in many other high-level languages.

Array An array is an ordered set of variables, where each value is called an element. Each element is distinguished by a special identifier called a key (also called a subscript).

Each entity in the array contains two items, namely key and value. The corresponding array elements can be obtained by key value. These keys can be numeric keys or association keys. If a variable is a container that stores a single value, then an array is a container that stores multiple values.

Comparison of linked lists and arrays in php

  • ##Arrays are continuous in memory, while linked lists are not continuous;

  • Due to different storage methods, arrays allocate memory statically, linked lists dynamically allocate memory, array elements are in the stack area, and linked list elements are in the heap area;

  • Due to The array is continuous in memory, we can use subscripts to locate, the time complexity is O(1), and the time complexity of locating elements in the linked list is O(n);

  • But due to the continuity of the array The time complexity of inserting or deleting elements in a sexual array is O(n), and the time complexity of a linked list is O(1).

To summarize, the difference between arrays and linked lists is as follows

1. Arrays allocate memory statically, and linked lists allocate memory dynamically

2. The array is continuous in the memory, but the linked list is not continuous

3. The array elements are in the stack area, and the linked list elements are in the heap area

4. The array is positioned using subscripts, and the time complexity is O( 1), the time complexity of locating elements in a linked list is O(n);

5. The time complexity of inserting or deleting elements in an array is O(n), and the time complexity of a linked list is O(1).

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What is the difference between linked list and array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template