Home Backend Development PHP Tutorial PHP implements the sequential storage structure of linear tables

PHP implements the sequential storage structure of linear tables

Jul 30, 2016 pm 01:31 PM
gt length list this

<code><span><span>class</span><span>SqList</span>
{</span><span>public</span><span>$elem</span>;
    <span>public</span><span>$length</span>;
    <span>public</span><span>$size</span>;
}

<span><span>class</span><span>Linear</span>
{</span><span>const</span> LIST_INIT_SIZE = <span>10</span>;
    <span>const</span> LIST_INCREMENT = <span>5</span>;
    <span>private</span><span>$list</span> = <span>null</span>;

    <span>/**
     * 构造一个空的线性表
     */</span><span>public</span><span><span>function</span><span>initList</span><span>()</span>
    {</span><span>$this</span>-><span>list</span> = <span>new</span> SqList();
        <span>$this</span>-><span>list</span>->elem = <span>array</span>();
        <span>$this</span>-><span>list</span>->length = <span>0</span>;
        <span>$this</span>-><span>list</span>->size = <span>self</span>::LIST_INIT_SIZE;
        <span>return</span><span>true</span>;
    }

    <span>/**
     * 销毁线性表
     */</span><span>public</span><span><span>function</span><span>destoryList</span><span>()</span>
    {</span><span>if</span> (is_object(<span>$this</span>-><span>list</span>)) {
            <span>$this</span>-><span>list</span> = <span>null</span>;
        }
    }

    <span>/**
     * 是否为空表
     */</span><span>public</span><span><span>function</span><span>listEmpty</span><span>()</span>
    {</span><span>if</span> (is_object(<span>$this</span>-><span>list</span>)) {
            <span>return</span><span>$this</span>-><span>list</span>->length == <span>0</span> ? <span>true</span> : <span>false</span>;
        }
    }

    <span>/**
     * 返回元素个数
     */</span><span>public</span><span><span>function</span><span>listLength</span><span>()</span>
    {</span><span>if</span> (is_object(<span>$this</span>-><span>list</span>)) {
            <span>return</span><span>$this</span>-><span>list</span>->length;
        }
    }

    <span>/**
     * 获取指定位置的元素
     */</span><span>public</span><span><span>function</span><span>getElem</span><span>(<span>$i</span>)</span>
    {</span><span>if</span> (<span>$i</span> < <span>1</span> || <span>$i</span> > <span>$this</span>-><span>list</span>->length + <span>1</span>) {
            <span>return</span><span>false</span>;
        }
        <span>return</span><span>$this</span>-><span>list</span>->elem[<span>$i</span>-<span>1</span>];
    }

    <span>/**
     * 在指定位置插入元素
     */</span><span>public</span><span><span>function</span><span>listInsert</span><span>(<span>$i</span>, <span>$e</span>)</span>
    {</span><span>if</span> (<span>$i</span> < <span>1</span> || <span>$i</span> > <span>$this</span>-><span>list</span>->length + <span>1</span>) {
            <span>return</span><span>false</span>;
        }
        <span>if</span> (<span>$this</span>-><span>list</span>->length >= <span>$this</span>-><span>list</span>->size) {
            <span>$this</span>-><span>list</span>->size += <span>self</span>::LIST_INCREMENT;
        }
        <span>for</span> (<span>$j</span> = <span>$this</span>-><span>list</span>->length; <span>$j</span> >= <span>$i</span>; <span>$j</span>--) {
            <span>$this</span>-><span>list</span>->elem[<span>$j</span>] = <span>$this</span>-><span>list</span>->elem[<span>$j</span>-<span>1</span>];
        }
        <span>$this</span>-><span>list</span>->elem[<span>$i</span>-<span>1</span>] = <span>$e</span>;
        <span>$this</span>-><span>list</span>->length++;
    }

    <span>/**
     * 删除指定位置数据元素
     */</span><span>public</span><span><span>function</span><span>listDelete</span><span>(<span>$i</span>)</span>
    {</span><span>if</span> (<span>$i</span> < <span>1</span> || <span>$i</span> > <span>$this</span>-><span>list</span>->length) {
            <span>return</span><span>false</span>;
        }
        <span>$data</span> = <span>$this</span>-><span>list</span>->elem[<span>$i</span>-<span>1</span>];
        <span>for</span> (<span>$j</span> = <span>$i</span> -<span>1</span>; <span>$j</span> < <span>$this</span>-><span>list</span>->length -<span>1</span>; <span>$j</span>++) {
            <span>$this</span>-><span>list</span>->elem[<span>$j</span>] = <span>$this</span>-><span>list</span>->elem[<span>$j</span>+<span>1</span>];
        }
        <span>unset</span>(<span>$this</span>-><span>list</span>->elem[<span>$this</span>-><span>list</span>->length-<span>1</span>]);
        <span>$this</span>-><span>list</span>->length--;
        <span>return</span><span>$data</span>;
    }
}</code>
Copy after login

Copyright Statement: This article is the original article of the blogger and may not be reproduced without the permission of the blogger.

The above introduces the sequential storage structure of linear tables implemented in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to implement Redis List operation in php How to implement Redis List operation in php May 26, 2023 am 11:51 AM

List operation //Insert a value from the head of the list. $ret=$redis->lPush('city','guangzhou');//Insert a value from the end of the list. $ret=$redis->rPush('city','guangzhou');//Get the elements in the specified range of the list. 0 represents the first element of the list, -1 represents the last element, and -2 represents the penultimate element. $ret=$redis->l

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

How to convert JSONArray to List in Java How to convert JSONArray to List in Java May 04, 2023 pm 05:25 PM

1: JSONArray to ListJSONArray string to List//Initialize JSONArrayJSONArrayarray=newJSONArray();array.add(0,"a");array.add(1,"b");array.add(2,"c") ;Listlist=JSONObject.parseArray(array.toJSONString(),String.class);System.out.println(list.to

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

How to convert list to numpy How to convert list to numpy Nov 22, 2023 am 11:29 AM

Method to convert list to numpy: 1. Use the numpy.array() function. The first parameter of the function is a list object, which can be a one-dimensional or multi-dimensional list; 2. Use the numpy.asarray() function, which will try its best to Use the data type of the input list; 3. Use the numpy.reshape() function to convert the one-dimensional list into a multi-dimensional NumPy array; 4. Use the numpy.fromiter() function, the first parameter of the function is an iterable object.

See all articles