Home > Backend Development > PHP Problem > Data structure linear table code

Data structure linear table code

angryTom
Release: 2023-02-28 08:58:01
Original
4484 people have browsed it

Data structure linear table code

Data structure linear table code

A linear table is a finite sequence of n elements with the same data characteristics. It is the most basic and commonly used linear structure (linear lists, stacks, queues, strings and arrays are all linear structures), and it is also the basis of other data structures.

Characteristics of non-empty linear tables or linear structures:

(1) There is only one data element called "first";

( 2) There is only one data element called the "last";

(3) Except for the first one, each data element in the structure has only one predecessor;

(4) Except for the last one, each data element in the structure has only one successor;

Linear table structure sequence representation (sequential table)

Concept: use A group of storage units with consecutive addresses stores the data elements of a linear table in sequence. The linear table of this storage structure is called a sequential table.

Features: Logically adjacent data elements are also adjacent in physical order.

As long as the starting position of the linear table is determined, any data element in the linear table can be randomly accessed, so the sequential storage structure of the linear table is a random access storage structure, because the advanced The array type in the language also has random access characteristics, so we usually use arrays to describe the sequential storage structure in the data structure, and use dynamically allocated one-dimensional arrays to represent linear tables.

The following is the code to use PHP to implement the data structure linear table (sequential table):

<?php
class ArrayList{
    private $list;
    private $size;
    public function __construct()
    {
        $this->list=array();
        $this->size=0;
    }
    //初始化链表
    public function InitList(){
        $this->list=array();
        $this->size=0;
    }
    //删除链表
    public function destoryList(){
        if (isset($this->list)){
            unset($this->list);
            $this->size=0;
        }
    }
    //清空链表
    public function clearList(){
        if (isset($this->list)){
            unset($this->list);
        }
        $this->list=array();
        $this->size=0;
    }
    //判断链表是否为空
    public function emptyList(){
        if (isset($this->list)){
            if ($this->size==0){
                return true;
            }else{
                return false;
            }
        }
    }
    //链表长度
    public function lengthList(){
        if (isset($this->list)){
            return $this->size;
        }else{
            return false;
        }
    }
    //取元素
    public function getElem($i){
        if ($i<1||$i>$this->size){
            die(&#39;failed&#39;);
        }
        if (isset($this->list)&&is_array($this->list)){
            return $this->list[$i-1];
        }
    }
    //是否在链表中
    public function locateElem($e){
        if (isset($this->list)&&is_array($this->list)){
            for ($i=0;$i<$this->size;$i++){
                if ($this->list[$i]==$e){
                    return $i+1;
                }
                return 0;
            }
        }
    }
    //前驱
    public function priorElem($i){
        if ($i<1||$i>$this->size){
            die(&#39;failed&#39;);
        }
        if ($i==1){
            die(&#39;no prior&#39;);
        }
        if (isset($this->list)&&is_array($this->list)){
            return $this->list[$i-2];
        }
    }
    //后继
    public function nextElem($i){
        if ($i<1||$i>$this->size){
            die(&#39;failed&#39;);
        }
        if ($i==$this->size){
            die(&#39;no next&#39;);
        }
        if (isset($this->list)&&is_array($this->list)){
            return $this->list[$i];
        }
    }
    //插入元素
    public function insertList($i,$e){
        if ($i<1||$i>$this->size){
            die(&#39;failed&#39;);
        }
        if (isset($this->list)&&is_array($this->list)){
            if ($this->size==0){
                $this->list[0]=$e;
                $this->size++;
            }else{
                for($j=$this->size-1;$j>=$i;$j--){
                    $this->list[$j]=$this->list[$j-1];
                }
                $this->list[$i-1]=$e;
                $this->size++;
            }
        }
    }
    //删除元素
    public function deleteList($i){
        if ($i<1||$i>$this->size){
            die(&#39;failed&#39;);
        }
        if (isset($this->list)&&is_array($this->list)){
            if ($i==$this->size){
                unset($this->list[$i-1]);
            }else{
                unset($this->list[$i-1]);
                for ($j=$i;$j<$this->size;$j++){
                    $this->list[$j-1]=$this->list[$j];
                }
            }
            $this->size--;
        }
    }
    //遍历
    public function printList(){
        if (isset($this->list)&&is_array($this->list)){
            foreach ($this->list as $value) {
                echo $value.&#39; &#39;;
            }
        }
    }
}
Copy after login

For more PHP-related knowledge, please visit PHP Chinese website!

The above is the detailed content of Data structure linear table code. 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