PHP implements the Stack data structure

Source: Internet
Author: User

Stack is a special post-first-out linear table that can only be inserted at one end) and delete (delete is generally called stack play, stack back or stack out) operations. One end that allows the insert and delete operations is called the stack top, and the other end is called the stack bottom. STACK: stores data according to the principle of "back-in-first-out". The data that comes first is pushed to the bottom of the stack, and the data that comes later is on the top of the stack. When you need to read data, data pops up from the top of the stack. When there are no elements in the stack, it is called an empty stack.

Data Structure and algorithm (PHP implementation)-Stack 1
<? Php
/**
* Data structures and algorithms (implemented in PHP)-stacks ).
*
* @ Author creative programming (TOPPHP. ORG)
* @ Copyright Copyright (c) 2013 creative programming (TOPPHP. ORG) All Rights Reserved
* @ License http://www.opensource.org/licenses/mit-license.php MIT LICENSE
* @ Version 1.0.0-Build20130607
*/
Class Stack {
/**
* Stack.
*
* @ Var array
*/
Private $ stack;
 
/**
* Stack length.
*
* @ Var integer
*/
Private $ size;
 
/**
* Constructor-Initialize data.
*/
Public function _ construct (){
$ This-> stack = array ();
$ This-> size = 0;
}
 
/**
* Stack pushing (stack loading and stack loading) operations.
*
* @ Param mixed $ data pressure stack data.
* @ Return object returns the object itself.
*/
Public function push ($ data ){
$ This-> stack [$ this-> size ++] = $ data;
 
Return $ this;
}
 
/**
* Stack rollback and stack exit operations.
*
* @ Return mixed: If the stack is empty, FALSE is returned. Otherwise, the top element of the stack is returned.
*/
Public function pop (){
If (! $ This-> isEmpty ()){
$ Top = array_splice ($ this-> stack, -- $ this-> size, 1 );
 
Return $ top [0];
}
 
Return FALSE;
}
 
/**
* Obtain the stack.
*
* @ Return array returns the stack.
*/
Public function getStack (){
Return $ this-> stack;
}
 
/**
* Obtain the top element of the stack.
*
* @ Return mixed: If the stack is empty, FALSE is returned. Otherwise, the top element of the stack is returned.
*/
Public function getTop (){
If (! $ This-> isEmpty ()){
Return $ this-> stack [$ this-> size-1];
}
 
Return FALSE;
}
 
/**
* Obtain the stack length.
*
* @ Return integer returns the length of the stack.
*/
Public function getSize (){
Return $ this-> size;
}
 
/**
* Check whether the stack is empty.
*
* @ Return boolean if the stack is empty, TRUE is returned; otherwise, FALSE is returned.
*/
Public function isEmpty (){
Return 0 ===$ this-> size;
}
}
?>

Sample Code 1
<? Php
$ Stack = new Stack ();
$ Stack-> push (1)-> push (2)-> push (3)-> push (4)-> push (5)-> push (6 );
Echo '<pre>', print_r ($ stack-> getStack (), TRUE), '</pre> ';
 
$ Stack-> pop ();
Echo '<pre>', print_r ($ stack-> getStack (), TRUE), '</pre> ';
?>

Note: PHP array functions have functions similar to Stacks: array_push (pressure stack) and array_pop (bullet stack ).

 

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.