Special linear form: Stack

   Introduced in front of two basic data structures - arrays and lists , from a logical point of view, they are both linear structure (the structure is arranged in a line, only two longitudinal directions, nonlinear structure comprising trees, like FIG, will be mentioned later), from a storage point of view, is a sequence storage, is a chain store, have advantages and disadvantages, contiguous memory array need to apply in advance, it exceeds the limit will overflow, but know exactly the size of a small data sets , using arrays will be more efficient, random access characteristics of the array is also more convenient to read, but the insert and delete performance to be worse; the list if there is no space constraints, but requires additional space to store a pointer, insert, delete high efficiency, but does not support random access. Although PHP rarely come into contact with these, but you learn to use the C language, these words still need to know basis.
   Next we want to introduce two special linear structure, a linear structure or to meet the needs of a particular scene: stacks and queues.
   First look at the stack:
     Stack called stack, it is defined only linear table insertions and deletions at one end, and to meet the advanced out, last in first out (LIFO) characteristics. Usually allow insertion and deletion of one end of the stack, called the other end is called the bottom of the stack, the stack does not contain any data called empty stack:
     Stack supports is achieved by an array / list, often called by the array while sequentially stack, called the link stack implemented by a linked list.
     Because it is demonstrated in PHP, I try to tend to the PHP language, to make things simple, we simply demonstrate how under the following stack to achieve a simple sequence through an array in PHP:
/ * * 
 * Simple sequential stack is achieved by PHP array 
 * / 
class the SimpleStack { 

    Private  $ _stack The = [];
     Private  $ _size = 0; 

    public  function the __construct ( $ size = 10 ) 
    { 
        $ the this -> _ size = $ size ; 
    } 

    // Get the top element 
    public  function POP () 
    { 
        // empty stack 
        IF ( COUNT ( $ the this -> _ stack) == 0 ) {
             return  to false ; 
        }
        return  array_pop ( $ the this -> _stack The); 
    } 

    // push element to the top of the stack 
    public  function Push ( $ value ) 
    { 
        // full stack 
        IF ( COUNT ( $ the this -> _ Stack) == $ the this -> _size) {
             return  to false ; 
        } 
        the array_push ( $ the this -> _ stack, $ value );
         return  to true ; 
    } 

    public  function isEmpty () 
    { 
        // if the stack is empty
        return current($this->_stack) == false;
    }

    public function size()
    {
        return count($this->_size);
    }
}

$stack = new SimpleStack(15);
var_dump($stack->isEmpty());  # true
$stack->push(111);
$stack->push('长坂坡');
var_dump($stack->pop());  # 长坂坡
var_dump($stack->size());  # 1

   In the bottom of SPL PHP library also provides an implementation class SplStack stack, the stack concept is relatively simple, not complicated to understand, given below a chart to help you understand the image stack operation process:

   

   Stack in the daily development and software use, is widely used, such as our browser forward, backward function, the editor / IDE revocation, cancellation Undo function, a function of program code called recursion, four operations, etc., this is based on a stack data structure to achieve, and even the famous stackoverflow site is also taking "stack overflow" meaning needs to ask for advice.

 

 

 

 

 

 

 
 

Guess you like

Origin www.cnblogs.com/mzhaox/p/11294213.html