PHP实现线性表的顺序存储实现

<?php
    //确定编码格式
    header('content-Type: text/html; charset=utf-8');

    /*
     * 类名:SeqList
     * 功能:线性表的顺序存储实现
     * 日期:2010-09-25
     * 修改:无
     * 作者:刘士龙
     */
    class SeqList {
        /* 类的成员属性的声明 */
        private $mElem;     //存储数组元素
        private $mLength;    //存储数组下标,同时指明当前顺序表元素个数
       
        /*
         * 函数名:__construct
         * 功  能:构造函数
         * 参  数:无
         * 返回值:无
         */
        public function __construct() {
            $this->mElem = array();
            $this->mLength = 0;
        } //__construct()

        /*
         * 函数名:__desstruct
         * 功  能:析构函数
         * 参  数:无
         * 返回值:无
         */
        public function __destruct() {
            $this->mElem = array();
            $this->mLength = 0;
        } //__destruct()

        /*
         * 函数名:ClearList
         * 功  能:清空顺序表
         * 参  数:无
         * 返回值:清空后的顺序表
         */
        public function clearList() {
            //$this->elem = array();
            $this->mLength = 0;

            //返回清空后的表
            return $this;
        } //clearList()

        /*
         * 函数名:ListEmpty
         * 功  能:判断顺序表是否为空表
         * 参  数:无
         * 返回值:bool (true表示为空表)
         */
        public function ListEmpty() {
            return $this->mLength == 0;
        } //ListEmpty()

        /*
         * 函数名:ListLength
         * 功  能:求顺序表的元素个数
         * 参  数:无
         * 返回值:int
         */
        public function ListLength() {
            return $this->mLength;
        } //ListLength()

        /*
         * 函数名:GetElem
         * 功  能:取顺序表中第i个元素(位序),并赋值给元素e
         * 参  数:i(待取元素的位序),&e(取得的元素)
         * 返回值:bool(操作是否成功)
         */
        public function GetElem($i, &$e) {
            //若当前顺序表为空表或不存在第i个元素,则操作失败
            if (0 == $this->mLength || $i < 1 || $i > $this->mLength)
                return false;
            //返回获得的第i个元素
            $e = $this->mElem[$i - 1];
            return true;
        } //GetElem()

        /*
         * 函数名:LocateElem
         * 功  能:返回待查找元素e在顺序表中的位序
         * 参  数:fp(变量函数,遍历函数的函数,本质上为一个函数指针);e(待定位的元素)
         * 返回值:int(元素e在顺序表中的位序,若返回-1则表示不存在该元素)
         */
        public function LocateElem($e, $fp) {
            for ($i = 0; $i < $this->mLength; ++$i) {
                //顺序表中的每一个元素依次与$e进行$fp的比较
                if (0 == $fp($e, $this->mElem[$i])) { //满足“相等”关系
                    return 1 + $i;
                } //if
            } //for
            return -1;
        } //LocateElem()

        /*
         * 函数名:PriorElem
         * 功  能:返回待查找元素e在顺序表中的前驱元素
         * 参  数:e(待定位的元素),pre_e为元素e在顺序表中的前驱元素
         * 返回值:bool(操作是否成功)
         */
        public function PriorElem($e, &$pre_e = 0) {
            $pos = $this->LocateElem($e, 'compare'));
            if ($pos !== -1 && $pos > 1) { //存在元素e
                $pre_e = $this->mElem[$pos - 2];
                return true;
            } //if
            return false;
        } //PriorElem()

        /*
         * 函数名:NextElem
         * 功  能:返回待查找元素e在顺序表中的直接后继元素
         * 参  数:e(待定位的元素),next_e为元素e在顺序表中的直接后继元素
         * 返回值:bool(操作是否成功)
         */
        public function NextElem($e, &next_e = 0) {
            $pos = $this->LocateElem($e, 'compare'));
            if ($pos !== -1 && $pos < $this->mLength) { //存在元素e
                $pre_e = $this->mElem[$pos];
                return true;
            } //if
            return false;
        } //LocateElem()

        /*
         * 函数名:ListInsert
         * 功  能:向顺序表中插入一个元素e
         * 参  数:i(待插入元素的位序),e(待插入的元素)
         * 返回值:无
         */
        public function ListInsert($i, $e) {
            for ($pos = $this->mLength; $pos >= $i; --$pos) {
                $this->mElem[$pos] = $this->mElem[$pos-1];
            } //for
            $this->mElem[$pos] = $e;
            ++$this->mLength;
        } //ListInsert()

        /*
         * 函数名:ListDelete
         * 功  能:删除顺序表中的一个元素,并用变量e返回
         * 参  数:i(待删除元素的位序),e(返回删除的元素)
         * 返回值:bool(操作是否成功)
         */
        public function ListDelete($i, &$e) {
            /* 若没有传入$e,则初始为0 */
            if (!isset($e)) $e = 0;
            if ($this->GetElem($i, $e) == true) {
                for (; $i < $this->mLength; ++$i) {
                    $this->mElem[$i - 1] = $this->mElem[$i];
                } //for               
                --$this->mLength;
                return true;
            } //if
            return false;
        } //ListDelete()

        /*
         * 函数名:ListTraverse
         * 功  能:遍历顺序表中的每个元素
         * 参  数:fp(变量函数,遍历函数的函数,本质上为一个函数指针)
         * 返回值:无
         */
        public function ListTraverse($fp) {
            for ($i = 0; $i < $this->mLength; ++$i) {
                $fp($this->mElem[$i]);
            } //for
            echo '<br />';
        } //ListTraverse()

       
    } //class SeqList

   

    function visit($e) {
        echo "$e&nbsp;&nbsp;&nbsp;";
    } //visit

    function compare($x, $y) {
        return $x - $y;
    } //compare()
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值