php 顺序线性表
<?php /*
* 线性顺序表 ,其是按照顺序在内存进行存储,出起始和结尾以外都是一一连接的(一般都是用一维数组的形式表现)
*
* GetElem: 返回线性表中第$index个数据元素
* ListLength: 返回线性表的长度
* LocateElem: 返回给定的数据元素在线性表中的位置
* 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个数据元素
*/ 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;
} /*
* 返回给定的数据元素在线性表中的位置
*/ 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;
} } ?>
php 顺序线性表的更多相关文章
- 顺序线性表之大整数求和C++
顺序线性表之大整数求和 大整数求和伪代码 1.初始化进位标志 flag=0: 2.求大整数 A 和 B 的长度: int aLength = a.GetLength(); int bLength = ...
- 顺序线性表 ---- ArrayList 源码解析及实现原理分析
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7738888.html ------------------------------------ ...
- 线性表&顺序线性表
第二章 线性表 参考文献:[数据结构(C语言版)].严蔚敏 本篇章仅为个人学习数据结构的笔记,不做任何用途. 2.1 线性结构的特点 (1). 存在唯一的一个被称为"第一个"的数据 ...
- 顺序线性表之大整数求和C++实现
顺序线性表之大整数求和 大整数求和伪代码 1.初始化进位标志 flag=0: 2.求大整数 A 和 B 的长度: int aLength = a.GetLength(); int bLength = ...
- C语言数据结构-顺序线性表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作
1.数据结构-顺序线性表的实现-C语言 #define MAXSIZE 100 //结构体定义 typedef struct { int *elem; //基地址 int length; //结构体当 ...
- 动态分配的顺序线性表的十五种操作—C语言实现
线性表 定义:是最常用的,也是最简单的数据结构,是长度为n个数据元素的有序的序列. 含有大量记录的线性表叫文件 记录:稍微复杂的线性表里,数据元素为若干个数据项组成,这时把一个数据元素叫记录 结构特点 ...
- 数据结构 - 静态顺序线性表的实行(C语言)
数据结构 - 静态顺序线性表的实行(C语言) 1 获取元素操作 对于线性表的顺序存储结构来说,如果我们要实现GetElem操作,即将线性表L中的第i个位置元素值返回,其实是非常简单的. 只要i的数值在 ...
- 数据结构算法C语言实现(一)---2.2线性表的顺序表示和实现
注意: 虽然是用C语言实现,但是考虑到使用了一个C++的特性----引用以简化代码,所以所有的代码均以cpp作为后缀,用g++编译(以后不做说明). g++版本: 一.简述 本节主要讲述线性表的顺序实 ...
- 线性表之顺序表C++实现
线性表之顺序表 一.头文件:SeqList.h //顺序线性表的头文件 #include<iostream> ; //定义顺序表SeqList的模板类 template<class ...
随机推荐
- oracle 密码默认180天过期
alter profile default limit password_life_time unlimited; alter user username identified by 'pwd';
- node install error
错误:Unexpected end of JSON input while parsing near.... 解决办法: npm cache clean --force
- 需要network lightweight filter disk 上的文件netft.sys
小米wifi在win10下安装不成功,需要network lightweight filter disk 上的文件ntflt.sys 默认路径有问题,改成下面的路径好了! 选择下面第一个路径安装成功了 ...
- ks8基础(1) etcd安装
下载安装 https://github.com/coreos/etcd/releases 在这网页,可以看到有多个版本共选择. 下载3.25 解压后, cd etcd-v3.2.5-linux-amd ...
- opencv 美白磨皮人脸检测<转>
1. 简介 这学期的计算机视觉课,我们组的课程项目为“照片自动美化”,其中我负责的模块为人脸检测与自动磨皮.功能为:用户上传一张照片,自动检测并定位出照片中的人脸,将照片中所有的人脸进行“磨皮”处理, ...
- Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder!
Eclipse 编译没问题,但是运行就出现这个报错:Unknown error: Unable to build: the file dx.jar was not loaded from the SD ...
- css中字间距调整(转)
css中字间距调整 (2014-01-02 13:51:07) 标签: 汉字 英文 间距 letter-spacing word-spacing 杂谈 分类: 网页制作 汉字间字间距设置:letter ...
- POJ 3714 分治/求平面最近点对
第一次见这种问题直接懵圈...没想到分治法这么强大,借鉴了lyd的代码: 代码如下 #include<cstdio> #include<algorithm> #include& ...
- linq 初步认识
linq to sql 类 介绍: linq如果不能用的话 重装一下vs就好了 LINQ,语言集成查询(Language Integrated Query)是一组用于c#和Visual Basic语言 ...
- 624. Maximum Distance in Arrays二重数组中的最大差值距离
[抄题]: Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers ...