Once we have defined the physical structure, the storage structure, we need to perform a series of logical operations on the storage structure. In this case, we’ll start with the sequential list, because it’s a very simple structure, and it’s the array we use most often. So what do we usually do with arrays?

We don’t need to think too complicated, we just need a few simple operations can be:

1. Find 2. Insert 3

Isn’t that easy? Why didn’t I go through it? How often do we have to iterate through an array?

Notice that here we are talking about the physical structure of a sequential table in terms of a data structure. Traversal operations are generally for more complex structures, such as trees, graphs, starting from one node to traverse all the paths, and so on. For the physical structure of the order table, we only need to master the above three operations and do not need to include traversal.

Another student said, in PHP, these three operations are simply too simple, there is no technical content!

Be careful not to get into a hole oh, look up we’re talking about finding the index where this value is, not just giving you a index and simply printing out a value. In addition, insert and delete we need to consider a problem, that is, after we insert or delete data in the ith position, I +1 and the data after it should also be moved accordingly? Be careful, we are inserting and deleting the content of a subscript position, not modifying and replacing the content of the subscript!!

Well, let’s get straight to the example.

insert

/** ** @param array $list * @param int $I * @param mixed $e * return bool */ function ListInsert(array &$list, int $i, $e) { $c = count($list); if ($i < 0 || $i > $c) { return false; } $j = $c - 1; $j + 1 = $j + 1; $j + 1 = $j + 1; $j + 1 = $j + 1; $j--; $list[$I] = $e; return true; }

The insert operation first determines whether the subscript is out of bounds. It then moves the data from the inserted position back one bit, and finally puts the newly added data into the specified position. Note that in this operation, our primary concern is the movement of this data location. Why do we move from the last bit of the array instead of the insertion position? If you start at the insertion position, then all of the following data will be the same data, that is, the next data in the insertion position. If you are interested, you can try it yourself.

$arr = [1, 2, 3, 4, 5, 6, 7];

ListInsert($arr, 3, 55);
print_r($arr);
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => 3
//     [3] => 55
//     [4] => 4
//     [5] => 5
//     [6] => 6
//     [7] => 7
// )

In the test code above, we inserted the data 55 at position 3 of the data. As you can see in the output, the length of the array has been increased by one digit, and everything from the index 3 has been moved back by one digit.

delete

/** ** function listDelete (array, $list, $I, $I, $I, $I, $I) &$list, int $i) { $c = count($list); if ($i < 0 || $i > $c - 1) { return false; } $j = $i; $list[$j] = $list[$j+1]; $list[$j] = $list[$j+1]; $j++; } // unset($list[$c - 1]); return true; }

After learning the above insert operation, I’m sure most students can imagine that the operation of deleting an element is exactly the reverse of insert. The first step is again to determine whether the subscript is compliant. The next step is to move the element after the subscript element specified to be deleted one step forward. In this case, we start by deleting the subscript and move the elements one bit forward, and finally remove the last bit of duplicate data, thereby subtracting the number of elements in the array by one.

$arr = [1, 2, 3, 4, 5, 6, 7];
ListDelete($arr, 5);
print_r($arr);
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => 3
//     [3] => 4
//     [4] => 5
//     [5] => 7
// )

It was also clear that the element at subscript 5 was 6. When we remove the index 5 element, the number of elements in the whole data is reduced by one, and the next element has to be moved up, that is, element 7 has to be moved to the position of 5.

To find the

Searching is simply doing a linear search, which is to compare the data one by one, to see where we need the data in the array.

** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** $e) { $c = count($list); for ($i = 0; $i < $c; $i++) { if ($list[$i] == $e) { return $i; } } return -1; }

If the data is found, we return the subscript of the current data location. And if we don’t find it at the end, we return a -1 to say we didn’t find it.

conclusion

Welcome to the world of data structure and algorithm, meaning is not surprising, surprise is not surprising, today for the first time to write so much code, but write out the feeling and we usually write is not quite the same? As with inserts and deletions, if you’re not paying attention to them, you might not really know that you should move them the other way around to get the correct result. This is the fun of learning data structures and algorithms, challenge yourself, every day is beyond!

Test code:

https://github.com/zhangyue0503/Data-structure-and-algorithm/blob/master/2. /source/2.2%20 sequential tables (arrays) related logical operations. PHP

References:

Data Structures, 2nd Ed., Weimin Yan

Data Structure, 2nd Edition, Yue Chen

“Notes on Data Structure with High Score”, 2020 edition, Tianqin postgraduate entrance examination

Search for “Hardcore Project Manager” on all media platforms