PHP implements the Stack data structure. Stack is a special post-first-out linear table that can only be inserted at one end) and delete (delete is generally referred to as the pop-up Stack and the back-to-Stack (Stack). It is a special post-in-first-out linear table, it can only be inserted at one end (insert is generally referred to as the pressure stack, into the stack or into the stack) and delete (delete is generally referred to as the elastic stack, back stack or Out Stack) operations, one end that allows 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
/**
* 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
$ Stack = new Stack ();
$ Stack-> push (1)-> push (2)-> push (3)-> push (4)-> push (5)-> push (6 );
Echo'
', print_r($stack->getStack(), TRUE), '
';
$ Stack-> pop ();
Echo'
', print_r($stack->getStack(), TRUE), '
';
?>
Note: PHP array functions have functions similar to stacks: array_push (pressure stack) and array_pop (bullet stack ).
A sort (Stack) is a special post-first-out linear table that can only be inserted at one end (insert is generally called a pressure Stack, Stack-in or Stack-in) and delete (delete is generally called pop-up stack, back stack...