資料結構之線性表——順序儲存結構(php代碼實現)

來源:互聯網
上載者:User

標籤:資料結構 線性表 php

<?php/** * * 線性表:即零個或多個資料元素的有限序列。 * 線性表的資料結構:即資料元素依此儲存在一段地址連續的儲存單元內。在進階語言中就表現為數組。 * * 1. DestroyList: 銷毀順序線性表 * 2. ClearList: 將線性表重設為空白 * 3. ListEmpty: 判斷線性表是否為空白 * 4. ListLength: 返回線性表的長度 * 5. GetElem: 返回線性表中第$index個資料元素 * 6. LocateElem: 返回給定的資料元素線上性表中的位置 * 7. PriorElem: 返回指定元素的前一個元素 * 8. NextElem: 返回指定元素的後一個元素 * 9. ListInsert: 在第index的位置插入元素elem * 10. ListDelete: 刪除第index位置的元素elem * */class SeqStoreList {    public $SqArr;    public static  $length;    public function __construct($SqArr){        $this->SqArr=$SqArr;        self::$length=count($SqArr);    }        //銷毀順序線性表    public  function DestroyList(){        $this->SqArr=null;        self::$length=0;    }    //將線性表重設為空白    public  function ClearList(){        $this->SqArr=array();        self::$length=0;    }        //判斷線性表是否為空白    public  function ListEmpty(){        if(self::$length==0){            return ‘Is null‘;        }else{            return ‘Not null‘;        }    }    //返回線性表的長度    public function ListLength(){        return self::$length;    }    //返回線性表中第$index個資料元素    public function GetElem($index){        if(self::$length==0 || $index<1 || $index>self::$length){            return ‘ERROR‘;        }        return $this->SqArr[$index-1];    }    //返回給定的資料元素線上性表中的位置    public function LocateElem($elem){        for($i=0;$i<self::$length;$i++){            if($this->SqArr[$i] == $elem){                break;            }        }        if($i>=self::$length){            return ‘ERROR‘;        }        return $i+1;    }    //返回指定元素的前一個元素    public function PriorElem($cur_elem){        for($i=0;$i<self::$length;$i++){            if($this->SqArr[$i] == $cur_elem){                break;            }        }        if($i==0 || $i>=self::$length){            return ‘ERROR‘;        }        return $this->SqArr[$i-1];    }    //返回指定元素的後一個元素    public function NextElem($cur_elem){        for($i=0;$i<self::$length;$i++){            if($this->SqArr[$i] == $cur_elem){                break;            }        }        if($i>=self::$length-1){            return ‘ERROR‘;        }        return $this->SqArr[$i+1];    }    //在第index的位置插入元素elem    public function ListInsert($index,$elem){        if($index<1 || $index>self::$length+1){            return ‘ERROR‘;        }        if($index<=self::$length){            for($i=self::$length-1;$i>=$index-1;$i--){                $this->SqArr[$i+1]=$this->SqArr[$i];            }        }        $this->SqArr[$index-1]=$elem;        self::$length++;        return ‘ok‘;    }    //ListDelete: 刪除第index位置的元素elem    public function ListDelete($index){        if($index<1 || $index>self::$length+1){            return ‘ERROR‘;        }        if($index<self::$length){            for($i=$index;$i<self::$length;$i++){                $this->SqArr[$i-1]=$this->SqArr[$i];            }        }        self::$length--;        return $this->SqArr[$index-1];    }}


本文出自 “一切皆有可能” 部落格,請務必保留此出處http://noican.blog.51cto.com/4081966/1598290

資料結構之線性表——順序儲存結構(php代碼實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.