顺序表添加与删除元素以及 php实现顺序表实例

时间:2023-07-27 22:40:47
顺序表添加与删除元素以及 php实现顺序表实例

对顺序表的操作,添加与删除元素。

增加元素

如下图所示  对顺序列表 Li [1328,693,2529,254]  添加一个元素 111 ,有三种方式:

顺序表添加与删除元素以及 php实现顺序表实例

a)尾部端插入元素,时间复杂度O(1);    保证了原始顺序列表的顺序。

b)非保序的加入元素(不常见),时间复杂度O(1);   打乱了原始顺序列表的顺序。

c)保需的元素插入,时间复杂度O(n);    保证了原始顺序列表的顺序。

删除元素

如下图所示  对顺序列表 Li [1328,693,2529,254]  删除元素 ,有三种方式:

顺序表添加与删除元素以及 php实现顺序表实例

a)删除表尾元素,时间复杂度O(1);

b)非保序的元素删除,时间复杂度O(1);

c)保序的元素删除,时间复杂度O(n);  比如上图删除1号元素。

list存储数据,允许不同类型的数据作为元素:如,Li [ 121, 'hello', 3.14, 1000 ], 因此List采用元素外置的方式来做。

元素外置的方式:可以理解为,列表list中,存储的是各个元素的地址,各个元素是外置到list外部, 每个元素的地址对应外部的元素。如下图:

顺序表添加与删除元素以及 php实现顺序表实例

支持顺序表存储区扩充的,称作动态顺序表

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";