[PHP data structure] linear table? Sequence table? Linked list? Don’t be silly and confused

Time:2021-10-21

Follow all textbooks and books related to various data structures. Let’s start with linear tables. Today’s article is more conceptual, which is a summary of a knowledge point with a linear table.

As mentioned above, physical structure is used to determine how data is stored. Other data structures (trees, graphs) and algorithms are basically based on such a physical structure. In other words, the physical structure is the foundation of the data structure. Here, we first introduce two physical structures, which are also the cornerstone of our future study of data structures. They are sequential list and linked list.

Sequence table

Do not pull complex definitions or mathematical expressions. Our most intuitive understanding is that the sequence table is an array.

Is it very simple? Yes, in the world of PHP or C, we define the sequential table as an array, and the same nouns also include: sequential storage, sequential structure, etc. As long as you see this noun, you can immediately think of an array. Of course, in Java, we can also regard collections such as list as sequential storage structures. However, it should be noted that Java’s HashMap is an array defined in the form of key value pairs in PHP. They are hash tables. Formally, they are not sequential. Here, we must remember that sequential storage structures such as array subscript increment are sequential storage structures.

Sequential tables generally occupy contiguous memory space. Not only in logic, but also in physical space.

Linked list

A linked list is generally defined as a structure in C, including data and a pointer to the next linked list. It is not sequential. Although the logic is sequential (pointed by the pointer), it can be separated in physics. In other words, the linked list does not occupy continuous memory space and does not need to be given a length like an array during initialization.

In PHP, we don’t have the syntax form of structure, so we directly use classes to represent the linked list structure.

class Node{
    public $data;
    public Node $next;
}

This is a simple linked list structure. We can call it a “node”. The data we want to save is stored in data, which can be of any type. And next is our next linked list node. If we need to simply traverse the linked list, we can directly call next until it is empty.

while($n->next){
    $n = $n->next;
    echo $n->data, PHP_EOL;
}

https://imgs.developpaper.com/imgs/1575433-20210718130202385-616110986.jpg

The above figure is about the logical state of the linked list and its traversal direction. The specific linked list operation related functions will be explained in the following articles.

There are many forms of linked lists. What we defined above is a simple one-way linked list. We can also define a two-way linked list (add a prev to point to the previous node), a circular linked list (the last next refers to the first node) and a two-way circular linked list (the last next refers to the first node, and the prev of the first points to the last node). We will also explain these contents one by one in the following articles.

Linear table

After understanding the sequential list and linked list, let’s finally talk about linear list.

In fact, the logical data structure of “sequence table” is implemented by the two physical structures of sequence table and linked list in the default state.

Sequence table: a finite sequence consisting of n (n > 0) elements with the same data characteristics(Yan Weimin version)

Note several key points:

  • Limited: array length, linked list memory size

  • Sequence: logical order (array is logical and physical order, linked list is logical order and physical disorder)

  • The data characteristics are the same: it is not obvious in PHP. Arrays in C or Java are of fixed type, and an initial length should also be given

Why is linear table so important? Let’s think about mysql, a relational database that stores data row by row. Isn’t a data table a linear table? In particular, as PHP programmers, we deal with arrays (the data read out by the database is generally put in the data) every day (of course, we may use hashes more). In other words, when we are developing, we are exposed to this thing every day. Do you say it is important or not.

The data structures such as trees and graphs are not linear tables. In reality, they belong to the category of nonlinear tables. A great feature of linear tables is that they only have front and back relationships. Whether they are linked lists or arrays, they all follow this front and back relationship. However, in trees and graphs, in addition to front and back, there are more complex relationships such as up, down, left and right. Although their basic expression is still the use of arrays and linked lists, from a strict point of view, or from the perspective of examination and interview, they really do not belong to linear structure, but should be divided into nonlinear structure.

summary

Today’s article is the basis for learning the basics of data structures. Of course, if possible, it’s best to look at how the C structure defines arrays and linked lists. PHP has solved too many problems at the bottom, so we can’t use these original syntax structures. Being able to learn data structures in C is a more recommended form.

In the next article, we will introduce the linear table related logical operations of sequential tables (arrays).

reference material:

Data structure, Second Edition, Yan Weimin

Data structure, Second Edition, Chen Yue

High score notes on data structure, 2020 edition, tianqin postgraduate entrance examination

===============

Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532