順序表添加與刪除元素以及 php實現順序表實例

對順序表的操做,添加與刪除元素。php

增長元素數組

以下圖所示  對順序列表 Li [1328,693,2529,254]  添加一個元素 111 ,有三種方式:this

 

 

a)尾部端插入元素,時間複雜度O(1);    保證了原始順序列表的順序。spa

b)非保序的加入元素(不常見),時間複雜度O(1);   打亂了原始順序列表的順序。3d

c)保需的元素插入,時間複雜度O(n);    保證了原始順序列表的順序。code

刪除元素blog

以下圖所示  對順序列表 Li [1328,693,2529,254]  刪除元素 ,有三種方式:內存

 

 

a)刪除表尾元素,時間複雜度O(1);get

b)非保序的元素刪除,時間複雜度O(1);io

c)保序的元素刪除,時間複雜度O(n);  好比上圖刪除1號元素。

 

list存儲數據,容許不一樣類型的數據做爲元素:如,Li [ 121, 'hello', 3.14, 1000 ], 所以List採用元素外置的方式來作。

元素外置的方式:能夠理解爲,列表list中,存儲的是各個元素的地址,各個元素是外置到list外部, 每一個元素的地址對應外部的元素。以下圖:

 支持順序表存儲區擴充的,稱做動態順序表

PHP代碼實現動態的順序表:

  1 <?php
  2 /**
  3  * Class Sequence
  4  * desc:順序表的實現:
  5  * 順序表:在內存中元素是順序存儲的,除了首部元素和尾部元素,其他元素是一一緊湊相連的;通常用一維數組表示。
  6  * 特色:順序表元素是外置的,順序表中只存元素的地址;順序表中的元素多是多種數據類型類型的元素
  7  * getElem 獲取元素的位置
  8  * getListLen 獲取順序表的長度
  9  * getPriorElem 獲取前一個元素
 10  * getNextElem 獲取後一個元素
 11  * InsertList 在第index個位置插入元素elem,返回新的順序表
 12  * DeleteList 在第index個位置刪除elem,返回新的順序表
 13  */
 14 
 15 class Sequence 16 { 17 public $seqArr; 18 public $length; 19 public function __construct($arr) 20  { 21 $this->seqArr = $arr; 22 $this->length = count($arr); 23  } 24 25 //獲取順序表List中給定元素 26 public function getElem($index) 27  { 28 if($index<0 || $index==0 || $index>$this->length) 29  { 30 return "Error!"; 31  } 32 return $this->seqArr[$index]; 33  } 34 35 //獲取順序表List的長度 36 public function getListLen($arr) 37  { 38 return $this->length; 39  } 40 41 //獲取順序表List中給定元素位置 42 public function getLocateElem($elem) 43  { 44 if(in_array($elem,$this->seqArr)) 45  { 46 for ($i=0;$i<$this->length;$i++) 47  { 48 if($this->seqArr[$i] == $elem) 49  { 50 return $i+1; 51  } 52  } 53  } 54 else 55  { 56 return "Error! elem not in Sequence!"; 57  } 58  } 59 60 //獲取給定元素在順序表List中的上一個元素 61 public function getPriorElem($elem) 62  { 63 if(in_array($elem,$this->seqArr)) 64  { 65 for ($i=0;$i<$this->length;$i++) 66  { 67 if($this->seqArr[$i] == $elem && $i==0) 68  { 69 return "Error: This is first Elem in Sequence!"; 70  } 71 elseif($this->seqArr[$i] == $elem) 72  { 73 return $this->seqArr[$i-1]; 74  } 75  } 76  } 77 else 78  { 79 return "Error!"; 80  } 81  } 82 83 //獲取給定元素在順序表List中的下一個元素 84 public function getNextElem($elem) 85  { 86 if(in_array($elem,$this->seqArr)) 87  { 88 for ($i=0;$i<$this->length;$i++) 89  { 90 if($this->seqArr[$i] == $elem && $i==($this->length-1)) 91  { 92 return "Error: This is final Elem in Sequence!"; 93  } 94 elseif($this->seqArr[$i] == $elem) 95  { 96 return $this->seqArr[$i+1]; 97  } 98  } 99  } 100 else 101  { 102 return "Error! elem not in Sequence!"; 103  } 104 105  } 106 107 //順序表第index 中新增一個元素elem,返回新的順序表 108 public function InsertList($index,$elem) 109  { 110 if($this->length == 0 || $index > $this->length || $index <0) 111  { 112 return "Error !"; 113  } 114 for ($i=($this->length-1);$i>$index-1;$i--) 115  { 116 $this->seqArr[$i+1] = $this->seqArr[$i]; 117  } 118 $this->seqArr[$index] = $elem; 119 $this->length = $this->length+1; 120 return $this->seqArr; 121  } 122 123 //順序表刪除第index中一個元素elem,返回新的順序表 124 public function DeleteList($index) 125  { 126 if($this->length == 0 || $index > $this->length || $index <0) 127  { 128 return "Error !"; 129  } 130 unset($this->seqArr[$index]); 131 $this->length--; 132 return $this->seqArr; 133  } 134 } 135 136 $arr = [6,'好','你',9,3.14]; 137 $se = new Sequence($arr); 138 //var_dump($se->getElem(1)); echo"\r\n"; 139 //var_dump($se->getListLen($arr)); echo"\r\n"; 140 //var_dump($se->getLocateElem(9)); echo"\r\n"; 141 //var_dump($se->getPriorElem(9)); echo"\r\n"; 142 //var_dump($se->getNextElem(9)); echo"\r\n"; 143 //print_r($se->InsertList(1,81)); echo"\r\n"; 144 print_r($se->DeleteList(3)); echo"\r\n";
相關文章
相關標籤/搜索