您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP实现双向链表、栈_PHP教程

2025/10/3 12:59:16发布11次查看
php实现双向链表、栈 双向链表                                                                                       
复制代码
no=$no;
                $this->name=$name;
            }
            /**
            *插入
            */
            static public function addhero($head,$hero)
            {
                $cur = $head;
                $isexist=false;
                //判断目前这个链表是否为空
                if($cur->next==null)
                {
                    $cur->next=$hero;
                    $hero->pre=$cur;
                }
                else
                {
                    //如果不是空节点,则安排名来添加
                    //找到添加的位置            
                    while($cur->next!=null)
                    {
                        if($cur->next->no > $hero->no)
                        {//如果大于了排名,跳出
                            break;
                        }
                        else if($cur->next->no == $hero->no)
                        {//如果等于排名,则代表有这个元素了
                            $isexist=true;
                            echo
不能添加相同的编号;
                        }
                        $cur=$cur->next;
                    }
                    if(!$isexist)
                    {//如果元素不存在,执行插入操作
                        if($cur->next!=null)
                        {$hero->next=$cur->next;}
                        $hero->pre=$cur;
                        if($cur->next!=null)
                        {$hero->next->pre=$hero;}
                        $cur->next=$hero;            
                    }
                }
            }
            //遍历
            static public function showhero($head)
            {
                $cur=$head;
                while($cur->next!=null)
                {
                    echo
编号:.$cur->next->no.名字:.$cur->next->name;
                    $cur=$cur->next;
                }
            }        
            static public function delhero($head,$herono)
            {
                $cur=$head;
                $isfind=false;
                while($cur!=null)
                {
                    if($cur->no==$herono)
                    {
                        $isfind=true;
                        break;
                    }
                    //继续找
                    $cur=$cur->next;
                }
                if($isfind)
                {
                    if($cur->next!=null)
                    {$cur->next_pre=$cur->pre;}
                    $cur->pre->next=$cur->next;
                }
                else
                {echo
没有找到目标;}            
            }
        }
        $head = new hero();
        $hero1 = new hero(1,'1111');
        $hero3 = new hero(3,'3333');
        $hero2 = new hero(2,'2222');
        hero::addhero($head,$hero1);
        hero::addhero($head,$hero3);
        hero::addhero($head,$hero2);
        hero::showhero($head);
        hero::delhero($head,2);
        hero::showhero($head);
?>
复制代码
双向链表的插入操作示意图:
if($cur->next!=null)
    $hero->next=$cur->next;
$hero->pre=$cur;
 if($cur->next!=null)
    $hero->next->pre=$hero;
$cur->next=$hero;
qq截图20140706152241
删除操作示意图:
if($cur->next!=null)
    $cur->next->pre=$cur->pre;
$cur->pre->next=$cur->next;
qq截图20140706152857

复制代码
top == $this->maxsize)
            {
                echo
已经满了;
            }
            $this->top++;
            $this->stack[$this->top]=$val;
        }
public function showstack()
        {
            if($this->top==-1)
            {
                echo
栈为空!;
                return ;
            }
            for($i=$this->top;$i>-1;$i--)
            {
                echo
stack[.$i.]=.$this->stack[$i];
            }
        }
public function pop()
        {
            if($this->top==-1)
            {
                echo
栈为空!;
                return ;
            }
$val=$this->stack[$this->top];
            $this->top--;
            echo
弹出.$val;
        }
    }
$mystack = new mystack;
    $mystack->push('111');
    $mystack->push('222');
    $mystack->showstack();
    $mystack->pop();
    $mystack->pop();
?>
复制代码
栈(stack):是限制在表的一端进行插入和删除操作的线性表。又称为后进先出lifo (last in first out)或先进后出filo (first in last out)线性表。
栈在计算机的实现有多种方式:
硬堆栈:利用cpu中的某些寄存器组或类似的硬件或使用内存的特殊区域来实现。这类堆栈容量有限,但速度很快;
软堆栈:这类堆栈主要在内存中实现。堆栈容量可以达到很大。在实现方式上,又有动态方式和静态方式两种
栈顶(top):允许进行插入、删除操作的一端,又称为表尾。用栈顶指针(top)来指示栈顶元素。
栈底(bottom):是固定端,又称为表头。
空栈:当表中没有元素时称为空栈。
栈的链式存储结构称为链栈,是运算受限的单链表。其插入和删除操作只能在表头位置上进行。因此,链栈没有必要像单链表那样附加头结点,栈顶指针top就是链表的头指针。
当然,php中的数组api里面带的有push和pop函数。
http://www.bkjia.com/phpjc/845432.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/845432.htmltecharticlephp实现双向链表、栈 双向链表 复制代码 ?php //双向链表 class hero { public $pre=null;//前指针 public $no;//排名 public $name;//名字 public $next=null;//后指...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product