php数据结构课程---4、栈(是什么)
一、总结
一句话总结:
栈(stack),它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。
1、栈的链表实现?
定义node,设置属性data,prev
定义stack类,设置栈顶top,size
编写出栈push(),pop()函数
返回栈所有数据函数getAll()
<?php
class Node{
public $data = null;
public $prev = null;
public function __construct($data){
$this->data = $data;
}
}
class stack{
private $top = null;
public $size = 0;
public function push($data)
{
if($this->top == null){
$this->top = new Node($data);
}else{
$n = new Node($data);
$n->prev = $this->top;
$this->top = $n;
}
$this->size++;
}
public function pop()
{
$data = $this->top->data;
$this->top = $this->top->prev;
$this->size--;
return $data;
}
public function getAll(){
$result = [];
while($this->top){
$result[]=$this->top->data;
$this->top = $this->top->prev;
}
return $result;
}
}
// $s = new stack();
// $s->push("aaaa");
// $s->push("bbb");
// $s->push("dddddd");
// $s->push("cc");
// $s->push("eeee");
// echo '<pre>';
// echo $s->pop();
// var_dump($s->getAll());
2、数组模拟栈?
array_push() array_pop()
//数组模拟栈
$stack = [];
array_push($stack,"aaa");
array_push($stack,"bbb");
array_push($stack,"ccc");
array_push($stack,"ddd");
array_push($stack,"eee");
echo array_pop($stack);
echo array_pop($stack);
3、括号匹配应用(比如[[()]()])?
算法思路:如果是左边的括号,就将对应的右括号押进栈,如果是右边的括号,判断栈里面是否有这个括号,没有说明不匹配
<?php
$stack = new SplStack();
$str = "[[[]](){}]";
echo $str."</br>";
$symbols =["("=>")","["=>"]","{"=>"}"];
// $keys =["(","[","{"];
// $values = [")","]","}"];
$result = "匹配";
for ($i=0; $i < strlen($str) ; $i++) {
if(in_array($str[$i], array_keys($symbols))){
$stack->push($symbols[$str[$i]]);
}else if(in_array($str[$i], array_values(($symbols)))){
if(!$stack->isEmpty()){
if($str[$i] != $stack->pop()){
$result = "不匹配!";
break;
}
}else{
$result = "不匹配!";
break;
}
}
}
if($stack->isEmpty()){
echo $result;
}else{
echo "不匹配!!";
}
// $stack = new SplStack();
// $str = "[[()][]";
// // $str = "[()][]]";
// echo $str."</br>";
// for ($i=0; $i < strlen($str); $i++) {
// if ($str[$i] == "[") {
// $stack->push("]");
// }elseif ($str[$i] == "(") {
// $stack->push(")");
// }elseif ($str[$i] == ")") {
// if(!$stack->isEmpty()){
// $pop = $stack->pop();
// if($pop != ")"){
// echo "not match!";exit;
// }
// }else{
// echo "not match!";exit;
// }
// }elseif ($str[$i] == "]") {
// if(!$stack->isEmpty()){
// $pop = $stack->pop();
// if($pop != "]"){
// echo "not match!";exit;
// }
// }else{
// echo "not match!";exit;
// }
// }
// }
// if($stack->isEmpty()){
// echo "匹配!";
// }else{
// echo "不匹配!!";
// }
4、八皇后的栈实现(不在同一条横线,竖线,左斜线,右斜线上)?
已选取的元素存入栈
判断左右斜线:$i和$j的差值
<?php
class Queen{
// 多少皇后
public $n;
// 记录多少种摆法
public $count;
//栈
public $stack;
public function __construct($n=8){
$this->n=$n;
$this->stack = new SplStack();
}
//摆放n皇后
public function place($n=8){
//结束条件,找到一个解
if($this->stack->count() == $this->n ){
$this->count++;
$this->map($this->stack);
return;
}
//摆放第n个皇后,探索水平方向值
for ($i=0; $i < $this->n ; $i++) {
//探索水平位置,值为$i
$this->stack->push($i);
if($this->check()){
$this->place($n-1);
}
//探索水平位置下个值
$this->stack->pop();
}
}
public function check(){
for ($i=1; $i < $this->stack->count() ; $i++) {
if($this->stack[$i] == $this->stack[0] || abs($this->stack[$i]-$this->stack[0]) == $i){
return false;
}
}
return true;
}
public function map(){
echo "<table>";
for ($i=0; $i < $this->n; $i++) {
echo "<tr>";
for ($j=0; $j < $this->n; $j++) {
echo "<td>";
if($this->stack[$i]==$j){
echo "Q";
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}
$n=8;
$q = new Queen($n);
$q->place($n);
echo "<table><tr><td class='count'>一共有 $q->count 个摆法。</td></tr></table>";
?>
<style>
table{
margin:15px 15px;
float:left;
}
td{
width: 15px;
height: 15px;
border:1px solid red;
}
.count{
width:150px;
}
</style>
5、用php敲算法问题的好处是什么?
结果可以以网页的形式美观的打印出来
二、内容在总结中