1.链表数据结构
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
2.线性表数据结构
(一)基本特点:最基本、最简单、最常用的一种数据结构
在这种结构中:(重点)
1.存在一个唯一的被称为“第一个”的数据元素;
2.存在一个唯一的被称为“最后一个”的数据元素;
3.除第一个元素外,每个元素均有唯一一个直接前驱;
4.除最后一个元素外,每个元素均有唯一一个直接后继。
(二)定义:
线性表(Linear List) :是由n(n≧0)个数据元素(结点)a1,a2, …an组成的有限序列。该序列中的所有结点具有相同的数据类型。其中数据元素的个数n称为线性表的长度。
当n=0时,称为空表。
当n>0时,将非空的线性表记作: (a1,a2,…an)
a1称为线性表的第一个(首)结点,an称为线性表的最后一个(尾)结点。
(三)线性表顺序存储 :把线性表的结点按逻辑顺序依次存放在一组地址连续的存储单元里。用这种方法存储的线性表简称顺序表。
顺序存储的线性表的特点:
1.线性表的逻辑顺序与物理顺序一致;
2.数据元素之间的关系是以元素在计算机内“物理位置相邻”来体现。
线性表中的顺序表的PHP实现
<?php /** * 顺序表基本操作 * *包括 *1.顺序表的初始化 __contruct() *2.清空顺序表 __destruct() *3.判断顺序表是否为空 isEmpty() *4.返回顺序表的长度 getLength() *5.根据下标返回顺序表中的某个元素 getElement() *6.返回顺序表中某个元素的位置 getElementPosition() *7.指定下标位置返回元素 getElemForPos() *8.根据元素位置删除顺序表中的某个元素 getDeleteEleForPos() */ header("content-type:text/html;charset=utf-8"); class OrderLinearList{ public $oll;//顺序表 /** * 顺序表初始化 * * @param mixed $oll * @return void * */ public function __construct($oll=array()){ $this->oll=$oll; } /** * 清空顺序表 *@return void */ public function __destruct(){ foreach($this->oll as $key=>$value){ unset($this->oll[$key]); } } /** * 判断顺序表是否为空 * @return boolean 为空返回true,否则返回false * */ public function isEmpty(){ if(count($this->oll) > 0){ return false; }else{ return true; } } /** * 返回顺序表的长度 * @return int * */ public function getLength(){ return count($this->oll); } /** * 返回顺序表中下标为$key的元素 * * @param mixed $key 顺序表元素的下标 * @return mixed * */ public function getElement($key){ return $this->oll[$key]; } /** * 返回顺序表中某个元素的位置 * * @param mixed $value 顺序表中某个元素的值 * @return int 从1开始,如果返回-1表示不存在该元素 * strcmp 比较两个字符串是否完全相同 区分大小写 * */ public function getElementPosition($value){ $i=0; foreach($this->oll as $val){ $i++; if(strcmp($value,$val) === 0){ return $i; } } return -1; } /** * 根据元素位置返回顺序表中的某个元素 * * @param int $position 元素位置从1开始 * @return array array('value'=>...)元素值,array('key'=>...)元素下标 */ public function getElemForPos($position){ $i=0; $len=count($this->oll); $position=(int)$position; if($position > $len || $position < 1){ return false; } foreach($this->oll as $key =>$item){ $i++; if($i == $position){ return array('value'=>$item,'key'=>$key); } } } /** * 根据元素位置删除顺序表中的某个元素 * * @param int $position 元素位置从1开始 * @return bool 成功返回true,失败返回false * */ public function getDeleteEleForPos($position){ $len=count($this->oll); $i=0; $position=(int)$position; if($position > $len || $position < 1){ return false; } foreach($this->oll as $k=>$v){ $i++; if($i == $position){ }else{ if(is_int($k)){ $oll[]=$v; }else{ $oll[$k]=$v; } } } $this->oll=$oll; if(count($this->oll) == $len){ return false; } return true; } } $oll=new OrderLinearList(array('xu'=>'徐典阳',32,"是吧",'dianyang'=>10,455)); //判断顺序表是否为空,返回false说明不为空 var_dump($oll->isEmpty()); echo "<br />"; //返回顺序表的长度 返回6 echo $oll->getLength(); echo "<br />"; //根据下标返回顺序表中的某个元素 var_dump($oll->getElement(1)); echo "<br/>"; //返回顺序表中某个元素的位置 echo $oll->getElementPosition("是吧"); echo "<br/>"; //根据元素位置返回顺序表中的某个元素 echo "------查找="; var_dump($oll->getElemForPos(2)); echo "<br />"; //根据元素位置删除顺序表中的某个元素 var_dump($oll->getDeleteEleForPos(1)); echo "<br />"; $people = array("Peter", "Joe", "Glenn", "Cleveland"); echo current($people) . "<br>"; echo next($people) . "<br>"; echo prev($people); ?> $arr = array(3,5,8,4,9,6,1,7,2); echo implode(" ",$arr)."<br/>"; //利用线性表的原理实现删除 function delete_array_element($arr,$pos){ $length = count($arr); if($pos<1 || $pos>$length){ return "删除位置出错!"; } for($i=$pos-1;$i<$length-1;$i++){ $arr[$i] = $arr[$i+1]; } array_pop($arr); return $arr; } $pos = 3; echo "<br/>除第{$pos}位置上的元素后:"; echo implode(' ',delete_array_element($arr,$pos))."<br/>";
3.顺序栈数据结构
即栈的顺序存储结构,是利用一组地址连续的存储单元依次存放自栈底到栈顶的数据元素,同时附设指针top指示栈顶元素在顺序 栈中的位置。(也类似于我们的数组)
class Stack{ //用默认值直接初始化栈了,也可用构造方法初始化栈 private $top = -1; private $maxSize = 5; private $stack = array(); //入栈 public function push($elem){ if($this->top >= $this->maxSize-1){ echo "栈已满!<br/>"; return; } $this->top++; $this->stack[$this->top] = $elem; } //出栈 public function pop(){ if($this->top == -1){ echo "栈是空的!"; return ; } $elem = $this->stack[$this->top]; unset($this->stack[$this->top]); $this->top--; return $elem; } //打印栈 public function show(){ for($i=$this->top;$i>=0;$i--){ echo $this->stack[$i]." "; } echo "<br/>"; } } $stack = new Stack(); $stack->push(3); $stack->push(5); $stack->push(8); $stack->push(7); $stack->push(9); $stack->push(2); $stack->show(); $stack->pop(); $stack->pop(); $stack->pop(); $stack->show();
4.双向队列数据结构
队列是指允许在一端(队尾)进入插入,而在另一端(队头)进行删除的线性表。Rear指针指向队尾,front指针指向队头。 队列是“先进行出”(FIFO)或“后进后出”(LILO)的线性表。 队列运算包括 (1)入队运算:从队尾插入一个元素; (2)退队运算:从队头删除一个元素。 循环队列:s=0表示队列空,s=1且front=rear表示队列满 class Deque{ private $queue = array(); public function addFirst($item){//头入队 array_unshift($this->queue,$item); } public function addLast($item){//尾入队 array_push($this->queue,$item); } public function removeFirst(){//头出队 array_shift($this->queue); } public function removeLast(){//尾出队 array_pop($this->queue); } public function show(){//打印 foreach($this->queue as $item){ echo $item." "; } echo "<br/>"; } } $deque = new Deque(); $deque->addFirst(2); $deque->addLast(3); $deque->addLast(4); $deque->addFirst(5); $deque->show();