以下将为你详细介绍使用 PHP 语言实现常见的数据结构和算法,涵盖数组、链表、栈、队列、排序算法和搜索算法等方面。
1. 数据结构
1.1 数组
在 PHP 中,数组是一种非常灵活的数据结构,既可以作为普通数组使用,也可以当作关联数组。
php
// 创建一个普通数组
$numbers = [1, 2, 3, 4, 5];
// 创建一个关联数组
$person = [
'name' => 'John',
'age' => 30,
'city' => 'New York'
];
// 访问数组元素
echo $numbers[2]; // 输出 3
echo $person['name']; // 输出 John
// 修改数组元素
$numbers[2] = 10;
$person['age'] = 31;
// 遍历数组
foreach ($numbers as $number) {
echo $number. ' ';
}
foreach ($person as $key => $value) {
echo "$key: $value <br>";
}
1.2 链表
链表是一种动态数据结构,每个节点包含数据和指向下一个节点的指针。以下是一个简单的单向链表实现:
php
class Node {
public $data;
public $next;
public function __construct($data) {
$this->data = $data;
$this->next = null;
}
}
class LinkedList {
public $head;
public function __construct() {
$this->head = null;
}
// 在链表末尾添加节点
public function append($data) {
$newNode = new Node($data);
if ($this->head === null) {
$this->head = $newNode;
} else {
$current = $this->head;
while ($current->next !== null) {
$current = $current->next;
}
$current->next = $newNode;
}
}
// 打印链表元素
public function display() {
$current = $this->head;
while ($current !== null) {
echo $current->data. ' ';
$current = $current->next;
}
}
}
// 使用示例
$list = new LinkedList();
$list->append(1);
$list->append(2);
$list->append(3);
$list->display(); // 输出 1 2 3
1.3 栈
栈是一种后进先出(LIFO)的数据结构。可以使用数组来实现栈:
php
class Stack {
private $stack;
public function __construct() {
$this->stack = [];
}
// 入栈操作
public function push($item) {
array_push($this->stack, $item);
}
// 出栈操作
public function pop() {
if ($this->isEmpty()) {
return null;
}
return array_pop($this->stack);
}
// 获取栈顶元素
public function peek() {
if ($this->isEmpty()) {
return null;
}
return end($this->stack);
}
// 判断栈是否为空
public function isEmpty() {
return empty($this->stack);
}
// 获取栈的大小
public function size() {
return count($this->stack);
}
}
// 使用示例
$stack = new Stack();
$stack->push(1);
$stack->push(2);
$stack->push(3);
echo $stack->pop(); // 输出 3
1.4 队列
队列是一种先进先出(FIFO)的数据结构。同样可以使用数组来实现队列:
php
class Queue {
private $queue;
public function __construct() {
$this->queue = [];
}
// 入队操作
public function enqueue($item) {
array_push($this->queue, $item);
}
// 出队操作
public function dequeue() {
if ($this->isEmpty()) {
return null;
}
return array_shift($this->queue);
}
// 获取队首元素
public function front() {
if ($this->isEmpty()) {
return null;
}
return $this->queue[0];
}
// 判断队列是否为空
public function isEmpty() {
return empty($this->queue);
}
// 获取队列的大小
public function size() {
return count($this->queue);
}
}
// 使用示例
$queue = new Queue();
$queue->enqueue(1);
$queue->enqueue(2);
$queue->enqueue(3);
echo $queue->dequeue(); // 输出 1
2. 算法
2.1 排序算法 - 冒泡排序
冒泡排序是一种简单的排序算法,它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来。
php
function bubbleSort($arr) {
$n = count($arr);
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++) {
if ($arr[$j] > $arr[$j + 1]) {
// 交换元素
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
return $arr;
}
$numbers = [5, 3, 8, 4, 2];
$sortedNumbers = bubbleSort($numbers);
print_r($sortedNumbers);
2.2 搜索算法 - 线性搜索
线性搜索是一种简单的搜索算法,它从数组的第一个元素开始,逐个比较元素,直到找到目标元素或遍历完整个数组。
php
function linearSearch($arr, $target) {
$n = count($arr);
for ($i = 0; $i < $n; $i++) {
if ($arr[$i] === $target) {
return $i;
}
}
return -1;
}
$numbers = [1, 3, 5, 7, 9];
$target = 5;
$index = linearSearch($numbers, $target);
echo $index; // 输出 2
这些是数据结构和算法在 PHP 中的基本实现,你可以根据具体需求对它们进行扩展和优化。