当前位置: 首页 > 后端技术 > PHP

数据结构——PHP通过链表对象实现“栈”

时间:2023-03-29 15:25:07 PHP

本文展示了如何使用PHP语言实现的链表类(LinkedList),然后通过链表实现的栈(Stack)只能从中添加元素栈顶,它只能从栈顶取出元素,这是一种后进先出(LIFO),即后进先出结构。1.output_stack_by_linked_list.php这是调用打印输出结果的显示文件:push('aa');$stack->push('bb');$stack->push('cc');$stack->push('dd');$stack->push('ee');$stack->push('ff');$stack->push('gg');$stack->push('hh');echo$stack->peek();//查看栈顶元素,打印hhecho"
";echo$stack->toString();//打印堆栈数据hh->gg->ff->ee->dd->cc->bb->aa->nullecho"
";echo$stack->pop();//弹出堆栈,打印hhecho"
";echo$stack->toString();//打印栈数据gg->ff->ee->dd->cc->bb->aa->null2.StackByLinkedList类这是一个封装好的栈(Stack)。通过实例化链表类(LinkedList)实现推(push)和出栈(pop),并查看栈顶(peek):array=newLinkedList();}/***获取堆栈大小*@returnint*/publicfunctiongetSize():int{return$this->array->getSize();}/***判断栈是否为空*@returnbool*/publicfunctionisEmpty():bool{return$this->array->isEmpty();}/***将元素压入堆栈*/publicfunctionpush($e):void{$this->array->addFirst($e);}/***pop*@returnmixed*/publicfunctionpop(){return$this->array->removeFirst();}/***查看栈顶元素*@returnmixed*/publicfunctionpeek(){return$this->array->getFirst();}/***将堆栈数组转换为字符串*@returnstring*/publicfunctiontoString():string{return$this->array->toString();}}Tips:这是一个Stack类,通过继承LinkedList类实现了Stack的基本功能3.interfaceStacknull*LinkedList构造函数。*/publicfunction__construct(){$this->dummyHead=newNode(null,null);$this->size=0;}/***获取链表的大小*@returnint*/publicfunctiongetSize():int{return$this->size;}/***判断链表是否为空*@returnbool*/publicfunctionisEmpty():bool{return$this->size==0;}/***在链表的索引位置添加一个元素*@paramint$index*@param$e*/publicfunctionadd(int$index,$e):void{if($index<0||$index>$this->size){echo"索引范围错误";出口;}$prve=$this->dummyHead;对于($i=0;$i<$index;$i++){$prve=$prve->next;}//将上一个插入位置的上一个位置的下一个节点指向插入节点,插入节点的下一个节点信息指向上一个节点的下一个节点$prve->next=newNode($e,$prve->下一个);$这个->尺寸++;}/***添加一个元素到链表的开头*@param$e*/publicfunctionaddFirst($e):void{$this->add(0,$e);}/***添加一个元素到链表的末尾*@param$e*/publicfunctionaddLast($e):void{$this->add($this->size,$e);}/***获取链表索引位置的元素*@param$index*/publicfunctionget($index){if($index<0||$index>$this->size){echo“索引范围错误”;出口;}$node=$this->dummyHead;对于($i=0;$i<$index+1;$i++){$node=$node->next;}返回$node->e;}/***获取链表的第一个元素*@returnmixed*/publicfunctiongetFirst(){return$this->get(0);}/***获取链表的最后一个元素*@returnmixed*/publicfunctiongetLast(){return$this->get($this->size-1);}/***修改链表中索引位置的元素值*@param$index*@param$e*/publicfunctionupdate($index,$e){if($index<0||$index>$this->size){echo"索引范围错误";出口;}$node=$this->dummyHead;对于($i=0;$i<$index+1;$i++){$node=$node->next;}$node->e=$e;}/***判断一个元素是否存在于链表中*@param$e*@returnbool*/publicfunctioncontains($e):bool{for($node=$this->dummyHead->next;$node!=null;$node=$node->next){if($node->e==$e){返回真;}}返回真;}/***删除链表中的索引元素*@param$index*/publicfunctionremove($index){if($index<0||$index>$this->size){echo"索引范围错误”;出口;}if($this->size==0){echo"链表已经为空";出口;}$prve=$this->dummyHead;for($i=0;$i<$index;$i++){$prve=$prve->next;}$node=$prve->next;$prve->next=$node->next;$this->大小--;返回$node->e;}/***删除链表的头元素*/publicfunctionremoveFirst(){return$this->remove(0);}/***删除链表的最后一个元素*/publicfunctionremoveLast(){return$this->remove($this->size-1);}/***链表元素转换为字符串显示*@returnstring*/publicfunctiontoString():string{$str="";对于($node=$this->dummyHead->next;$node!=null;$node=$node->next){$str.=$node->e.“->”;}返回$str。“无效的”;}}classNode{public$e;//节点元素public$next;//下一个节点信息/***构造函数设置节点信息*节点构造函数。*@param$e*@param$next*/publicfunction__construct($e,$next){$this->e=$e;$this->next=$next;}}代码库:https://gitee.com/love-for-po。..扫描二维码关注爱因世贤