PHP implements two-way linked list and stack

Source: Internet
Author: User

PHP implements two-way linked list and stack
Bidirectional linked list copy Code <? Php // bidirectional linked list class Hero {public $ pre = null; // The front pointer public $ no; // rank public $ name; // name public $ next = null; // post pointer/*** constructor, declaring the chain table header */public function _ construct ($ no = '', $ name = '') {$ this-> no = $ no; $ this-> name = $ name;}/*** insert */static public function addHero ($ head, $ hero) {$ cur = $ head; $ isExist = false; // checks whether the linked list is empty. if ($ cur-> next = null) {$ cur-> next = $ hero; $ hero-> pre = $ cur;} else {// if it is not an empty node, add a ranking. // locate the added Location while ($ cur-> next! = Null) {if ($ cur-> next-> no> $ hero-> no) {// jump out of break if the ranking is higher than the ranking ;} else if ($ cur-> next-> no ==$ hero-> no) {// if it is equal to the rank, $ isExist = true indicates that this element exists; echo "<br> the same number cannot be added";} $ cur = $ cur-> next;} if (! $ IsExist) {// if the element does not exist, execute the insert operation if ($ cur-> next! = Null) {$ hero-> next = $ cur-> next;} $ hero-> pre = $ cur; if ($ cur-> next! = Null) {$ hero-> next-> pre = $ hero;} $ cur-> next = $ hero; }}// traverse static public function showHero ($ head) {$ cur = $ head; while ($ cur-> next! = Null) {echo "<br> No :". $ cur-> next-> no. "Name :". $ 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;} // continue to find $ cur = $ cur-> next ;} if ($ isFind) {if ($ cur-> next! = Null) {$ cur-> next_pre = $ cur-> pre;} $ cur-> pre-> next = $ cur-> next ;} else {echo "<br> no target found" ;}}$ head = new Hero (); $ hero1 = new Hero (1, '20140901 '); $ hero3 = new Hero (3, '000000'); $ hero2 = new Hero (2, '000000'); Hero: addHero ($ head, $ hero1); Hero:: addHero ($ head, $ hero3); Hero: addHero ($ head, $ hero2); Hero: showHero ($ head); Hero: delHero ($ head, 2); Hero: showHero ($ head);?> Copy the code to insert a two-way linked list: if ($ cur-> next! = Null) $ hero-> next = $ cur-> next; $ hero-> pre = $ cur; if ($ cur-> next! = Null) $ hero-> next-> pre = $ hero; $ cur-> next = $ hero; QQ20140706152241 delete operation: if ($ cur-> next! = Null) $ cur-> next-> pre = $ cur-> pre; $ cur-> pre-> next = $ cur-> next; QQ20140706152857 stack copy Code <? Php class myStack {private $ top =-1; private $ maxSize = 5; private $ stack = array (); public function push ($ val) {if ($ this-> top ==$ this-> maxSize) {echo "<br> full" ;}$ this-> top ++; $ this-> stack [$ this-> top] = $ val;} public function showStack () {if ($ this-> top =-1) {echo "<br> stack is empty! "; Return;} for ($ I = $ this-> top; $ I>-1; $ I --) {echo" <br> stack [". $ I. "] = ". $ this-> stack [$ I] ;}} public function pop () {if ($ this-> top =-1) {echo "<br> stack is empty! "; Return ;}$ val = $ this-> stack [$ this-> top]; $ this-> top --; echo" <br> pop-up ". $ val ;}$ mystack = new myStack; $ mystack-> push ('20140901'); $ mystack-> push ('20160901 '); $ mystack-> showStack (); $ mystack-> pop ();?> Copy code Stack: a linear table that is limited to insert and delete operations at one end of the table. It is also known as the Last In First Out (LIFO) or the First In First Out (First In Last Out) linear table. Stack can be implemented on a computer in multiple ways: Hard Stack: using some Register Groups in the CPU or similar hardware or using special areas of memory. These stacks have limited capacity but are fast; soft stacks: these stacks are mainly implemented in memory. The stack capacity can be very large. In terms of implementation methods, there are two stack Top modes: dynamic mode and static mode: one end that allows the insert and delete operations, also known as the end of the table. Use the stack top pointer to indicate the top elements of the stack. Bottom stack (Bottom): it is a fixed end, also known as the header. Empty Stack: when there are no elements in the table, it is called an empty stack. The stack's chain storage structure is called the chain stack, which is a single-chain table with limited operations. The insert and delete operations can only be performed at the header position. Therefore, the chain stack does not need to append header nodes as a single-chain table. The top pointer of the stack is the head pointer of the chain table. Of course, the array API in php contains push and pop functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.