php實現(xiàn)的順序線性表示例
本文實例講述了php實現(xiàn)的順序線性表。分享給大家供大家參考,具體如下:
<?php
/*
* 線性順序表 ,其是按照順序在內(nèi)存進行存儲,出起始和結(jié)尾以外都是一一連接的(一般都是用一維數(shù)組的形式表現(xiàn))
*
* GetElem: 返回線性表中第$index個數(shù)據(jù)元素
* ListLength: 返回線性表的長度
* LocateElem: 返回給定的數(shù)據(jù)元素在線性表中的位置
* PriorElem: 返回指定元素的前一個元素
* NextElem: 返回指定元素的后一個元素
* ListInsert: 在第index的位置插入元素elem
* ListDelete: 刪除第index位置的元素elem
*/
class Sequence {
public $seqArr;
public $length;
public function __construct($arr) {
$this->seqArr = $arr;
$this->length = count($arr);
}
/*
* 返回線性表中第$index個數(shù)據(jù)元素
*/
public function GetElem($index) {
if (($this->length) == 0 || $index < 0 || ($index > $this->length)) {
return "Error";
}
return $this->seqArr[$index - 1];
}
/*
* 返回線性表的長度
*
*/
public function ListLength() {
return $this->length;
}
/*
* 返回給定的數(shù)據(jù)元素在線性表中的位置
*/
public function LocateElem($elem) {
for ($i = 0; $i < ($this->length); $i++) {
if (($this->seqArr[$i]) == $elem) {
return $i + 1;
}
}
}
/*
* PriorElem: 返回指定元素的前一個元素
*/
public function PriorElem($elem) {
for ($i = 0; $i < ($this->length); $i++) {
if (($this->seqArr[$i]) == $elem) {
if ($i == 0) {
return "Error (is null) ";
} else {
return $this->seqArr[$i - 1];
}
}
}
}
/*
* NextElem: 返回指定元素的后一個元素
*/
public function NextElem($elem) {
for ($i = 0; $i < ($this->length); $i++) {
if (($this->seqArr[$i]) == $elem) {
return $this->seqArr[$i + 1];
}
}
}
/*
* ListInsert: 在第index的位置插入元素elem
*/
public function ListInsert($index, $elem) {
if (($this->length) == 0 || $index < 0 || $index > ($this->length)) {
return "Error";
}
for ($i = $index; $i < ($this->length); $i++) {
$this->seqArr[$i + 1] = $this->seqArr[$i];
}
$this->seqArr[$index] = $elem;
$this->length = $this->length + 1;
return $this->seqArr;
}
/*
* ListDelete: 刪除第index位置的元素
*/
public function ListDelete($index) {
if (($this->length) == 0 || $index < 0 || $index > ($this->length - 1)) {
return "Error";
}
unset($this->seqArr[$index]);
$this->length--;
return $this->seqArr;
}
}
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學(xué)運算技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php從memcache讀取數(shù)據(jù)再批量寫入mysql的方法
這篇文章主要介紹了php從memcache讀取數(shù)據(jù)再批量寫入mysql的方法,可利用memcache緩解服務(wù)器讀寫壓力,并實現(xiàn)數(shù)據(jù)庫數(shù)據(jù)的寫入操作,非常具有實用價值,需要的朋友可以參考下2014-12-12
PHP調(diào)用存儲過程返回值不一致問題的解決方法分析
這篇文章主要介紹了PHP調(diào)用存儲過程返回值不一致問題的解決方法,結(jié)合實例形式分析了存儲過程調(diào)用返回值不一致的原因與解決方法,需要的朋友可以參考下2016-04-04
PHP高級對象構(gòu)建 多個構(gòu)造函數(shù)的使用
構(gòu)建對象是PHP面向?qū)ο缶幊淘O(shè)計中的一個重要主題。在最簡單的情況下,普通構(gòu)造函數(shù)就夠用了,但如果要開展更為復(fù)雜的設(shè)計,那么構(gòu)造函數(shù)可能會變的難以管理2012-02-02
phpmailer簡單發(fā)送郵件的方法(附phpmailer源碼下載)
這篇文章主要介紹了phpmailer簡單發(fā)送郵件的方法,提供了phpmailer的源碼與相應(yīng)的設(shè)置、使用方法,需要的朋友可以參考下2016-06-06
php定時計劃任務(wù)與fsockopen持續(xù)進程實例
本文介紹了php中定時計劃任務(wù)的實現(xiàn)代碼,以及php持續(xù)進程fsockopen的用法,需要的朋友可以參考下2014-05-05
PHP執(zhí)行Curl時報錯提示CURL ERROR: Recv failure: Connection reset by
這篇文章主要介紹了PHP執(zhí)行Curl時報錯提示CURL ERROR: Recv failure: Connection reset by peer的解決方法,需要的朋友可以參考下2014-06-06

