diff --git a/codes/php/chapter_array_and_linkedlist/array.php b/codes/php/chapter_array_and_linkedlist/array.php new file mode 100644 index 000000000..b5f1f9340 --- /dev/null +++ b/codes/php/chapter_array_and_linkedlist/array.php @@ -0,0 +1,108 @@ + $index; $i--) { + $nums[$i] = $nums[$i - 1]; + } + // 将 num 赋给 index 处的元素 + $nums[$index] = $num; +} + +/* 删除索引 index 处的元素 */ +function remove(&$nums, $index) +{ + // 把索引 index 之后的所有元素向前移动一位 + for ($i = $index; $i < count($nums) - 1; $i++) { + $nums[$i] = $nums[$i + 1]; + } +} + +/* 遍历数组 */ +function traverse($nums) +{ + $count = 0; + // 通过索引遍历数组 + for ($i = 0; $i < count($nums); $i++) { + $count += $nums[$i]; + } + // 直接遍历数组元素 + foreach ($nums as $num) { + $count += $num; + } +} + +/* 在数组中查找指定元素 */ +function find($nums, $target) +{ + for ($i = 0; $i < count($nums); $i++) { + if ($nums[$i] === $target) { + return $i; + } + } + return -1; +} + +/* Driver Code */ +/* 初始化数组 */ +$arr = array_fill(0, 5, 0); +echo '数组 arr = [' . join(', ', $arr) . ']' . PHP_EOL; +$nums = [1, 3, 2, 5, 4]; +echo '数组 nums = [' . join(', ', $nums) . ']' . PHP_EOL; + +/* 随机访问 */ +$randomNum = randomAccess($nums); +echo "在 nums 中获取随机元素 {$randomNum}" . PHP_EOL; + +/* 长度扩展 */ +$nums = extend($nums, 3); +echo '将数组长度扩展至 8 ,得到 nums = [' . join(', ', $nums) . ']' . PHP_EOL; + +/* 插入元素 */ +insert($nums, 6, 3); +echo '在索引 3 处插入数字 6 ,得到 nums = [' . join(', ', $nums) . ']' . PHP_EOL; + +/* 删除元素 */ +remove($nums, 2); +echo '删除索引 2 处的元素,得到 nums = [' . join(', ', $nums) . ']' . PHP_EOL; + +/* 遍历数组 */ +traverse($nums); + +/* 查找元素 */ +$index = find($nums, 3); +echo "在 nums 中查找元素 3 ,得到索引 = {$index}" . PHP_EOL; diff --git a/codes/php/chapter_array_and_linkedlist/linked_list.php b/codes/php/chapter_array_and_linkedlist/linked_list.php new file mode 100644 index 000000000..1af73a6b5 --- /dev/null +++ b/codes/php/chapter_array_and_linkedlist/linked_list.php @@ -0,0 +1,90 @@ +next; + $P->next = $n1; + $n0->next = $P; +} + +/* 删除链表的节点 n0 之后的首个节点 */ +function remove($n0) +{ + if ($n0->next === null) { + return; + } + // n0 -> P -> n1 + $P = $n0->next; + $n1 = $P->next; + $n0->next = $n1; +} + +/* 访问链表中索引为 index 的节点 */ +function access($head, $index) +{ + for ($i = 0; $i < $index; $i++) { + if ($head === null) { + return null; + } + $head = $head->next; + } + return $head; +} + +/* 在链表中查找值为 target 的首个节点 */ +function find($head, $target) +{ + $index = 0; + while ($head !== null) { + if ($head->val === $target) { + return $index; + } + $head = $head->next; + $index++; + } + return -1; +} + +/* Driver Code */ +/* 初始化链表 */ +// 初始化各个节点 +$n0 = new ListNode(1); +$n1 = new ListNode(3); +$n2 = new ListNode(2); +$n3 = new ListNode(5); +$n4 = new ListNode(4); +// 构建节点之间的引用 +$n0->next = $n1; +$n1->next = $n2; +$n2->next = $n3; +$n3->next = $n4; +echo '初始化的链表为' . PHP_EOL; +PrintUtil::printLinkedList($n0); + +/* 插入节点 */ +insert($n0, new ListNode(0)); +echo '插入节点后的链表为' . PHP_EOL; +PrintUtil::printLinkedList($n0); + +/* 删除节点 */ +remove($n0); +echo '删除节点后的链表为' . PHP_EOL; +PrintUtil::printLinkedList($n0); + +/* 访问节点 */ +$node = access($n0, 3); +echo "链表中索引 3 处的节点的值 = {$node->val}" . PHP_EOL; + +/* 查找节点 */ +$index = find($n0, 2); +echo "链表中值为 2 的节点的索引 = {$index}" . PHP_EOL; diff --git a/codes/php/chapter_array_and_linkedlist/list.php b/codes/php/chapter_array_and_linkedlist/list.php new file mode 100644 index 000000000..209e2df30 --- /dev/null +++ b/codes/php/chapter_array_and_linkedlist/list.php @@ -0,0 +1,58 @@ +arr = array_fill(0, $this->capacity, 0); + } + + /* 获取列表长度(当前元素数量) */ + public function size() + { + return $this->size; + } + + /* 获取列表容量 */ + public function capacity() + { + return $this->capacity; + } + + /* 访问元素 */ + public function get($index) + { + // 索引如果越界,则抛出异常,下同 + if ($index < 0 || $index >= $this->size) { + throw new Exception('索引越界'); + } + return $this->arr[$index]; + } + + /* 更新元素 */ + public function set($index, $num) + { + if ($index < 0 || $index >= $this->size) { + throw new Exception('索引越界'); + } + $this->arr[$index] = $num; + } + + /* 在尾部添加元素 */ + public function add($num) + { + // 元素数量超出容量时,触发扩容机制 + if ($this->size === $this->capacity()) { + $this->extendCapacity(); + } + $this->arr[$this->size] = $num; + // 更新元素数量 + $this->size++; + } + + /* 在中间插入元素 */ + public function insert($index, $num) + { + if ($index < 0 || $index >= $this->size) { + throw new Exception('索引越界'); + } + // 元素数量超出容量时,触发扩容机制 + if ($this->size === $this->capacity()) { + $this->extendCapacity(); + } + // 将索引 index 以及之后的元素都向后移动一位 + for ($j = $this->size - 1; $j >= $index; $j--) { + $this->arr[$j + 1] = $this->arr[$j]; + } + $this->arr[$index] = $num; + // 更新元素数量 + $this->size++; + } + + /* 删除元素 */ + public function remove($index) + { + if ($index < 0 || $index >= $this->size) { + throw new Exception('索引越界'); + } + $num = $this->arr[$index]; + // 将索引 index 之后的元素都向前移动一位 + for ($j = $index; $j < $this->size - 1; $j++) { + $this->arr[$j] = $this->arr[$j + 1]; + } + // 更新元素数量 + $this->size--; + // 返回被删除的元素 + return $num; + } + + /* 列表扩容 */ + public function extendCapacity() + { + // 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组 + $this->arr = array_pad($this->arr, $this->capacity() * $this->extendRatio, 0); + // 更新列表容量 + $this->capacity = count($this->arr); + } + + /* 将列表转换为数组 */ + public function toArray() + { + $size = $this->size(); + // 仅转换有效长度范围内的列表元素 + $arr = []; + for ($i = 0; $i < $size; $i++) { + $arr[$i] = $this->get($i); + } + return $arr; + } +} + +/* Driver Code */ +/* 初始化列表 */ +$nums = new MyList(); +/* 在尾部添加元素 */ +$nums->add(1); +$nums->add(3); +$nums->add(2); +$nums->add(5); +$nums->add(4); +echo "列表 nums = [" . join(', ', $nums->toArray()) . "] ,容量 = {$nums->capacity()} ,长度 = {$nums->size()}" . PHP_EOL; + +/* 在中间插入元素 */ +$nums->insert(3, 6); +echo '在索引 3 处插入数字 6 ,得到 nums = [' . join(', ', $nums->toArray()) . ']' . PHP_EOL; + +/* 删除元素 */ +$nums->remove(3); +echo '删除索引 3 处的元素,得到 nums = [' . join(', ', $nums->toArray()) . ']' . PHP_EOL; + +/* 访问元素 */ +$num = $nums->get(1); +echo "访问索引 1 处的元素,得到 num = {$num}" . PHP_EOL; + +/* 更新元素 */ +$nums->set(1, 0); +echo '将索引 1 处的元素更新为 0 ,得到 nums = [' . join(', ', $nums->toArray()) . ']' . PHP_EOL; + +/* 测试扩容机制 */ +for ($i = 0; $i < 10; $i++) { + // 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制 + $nums->add($i); +} +echo "扩容后的列表 nums = [" . join(', ', $nums->toArray()) . "] ,容量 = {$nums->capacity()} ,长度 = {$nums->size()}" . PHP_EOL;