PHP中的SPl编程

 PHP   PHP中的SPl编程   阅读(166)   2019-07-09 21:37:36 

双向链表提供队列
class SplQueue1 extends SplDoublyLinkedList implements Iterator,ArrayAccess,Countable
{
//继承PHP内置接口

/**
* 在末尾插入键值
* @param mixed $value
*/
public function push($value)
{
parent::push($value); // TODO: Change the autogenerated stub
}

/**
* 反序列
* @return string|void
*/
public function serialize()
{
parent::serialize(); // TODO: Change the autogenerated stub
}

/**
* 在开头插入键值
* @param mixed $value
*/
public function unshift($value)
{
parent::unshift($value); // TODO: Change the autogenerated stub
}

/**
* 在开头删除键值
* @return mixed|void
*/
public function shift()
{
parent::shift(); // TODO: Change the autogenerated stub
}
}

$queue = new SplQueue1();
$queue->push(1);
$queue->push(2);
$queue->push(3);
$queue->shift();
print_r($queue); 内置方法操作栈

$queue = new SplQueue();
$queue->push("a");
$queue->push("b");
$queue->push("c");
var_dump($queue->pop());

双向链表

$dlist=new SplDoublyLinkedList();
//在末尾插入键值
$dlist->push('hiramariam');
$dlist->push('maaz');
$dlist->push('zafar');
//在开头插入键值
$dlist->unshift(1);
$dlist->unshift(2);
$dlist->unshift(3);
//删除开头
$dlist->shift();
print_r($dlist);
PHP中栈
$q = new SplStack();
$q[] = 1;
$q[] = 2;
$q[] = 3;
$q->push(4);
php堆
class MySimpleHeap extends SplHeap
{
public function compare($value1, $value2)
{
return ($value1 - $value2);
}
}
$obj = new MySimpleHeap();
$obj->insert(1);
$obj->insert(2);
$obj->insert(3);
foreach ($obj as $item) {
echo $item;
}










我要评论



展示评论