<?php
/**
* 单链表的实现
*
*/
//线性表接口
interface LList{
//function isEmpty();
//function theLength();
//function get($index);
//function set($index,$value);
//function insert($index,$value);
//初步实现添加操作
function add($value);
//function remove($index);
}
//单链表数据结点类
class Node{
public $data;
public $next;
function __construct($data,$next=null){
$this->data = $data;
$this->next = $next;
}
}
class SinglyLinkedList implements LList{
protected $head;
function __construct(){
$this->head = null;
}
function add($value){
if ($this->head == null){
$node = new Node($value);
//实现头插入
$node->next = $this->head;
$this->head = $node;
}else{
$j=1;
$p = $this->head;
while ($p->next != null){
$j++;
$p = $p->next;
}
$q = new Node($value);
$q->next = $p->next;
$p->next = $q;
}
}
线性单链表的实现(php)
最新推荐文章于 2021-03-11 08:07:52 发布