Stack of data structures-sequential storage structure (PHP code implementation)

Source: Internet
Author: User

<?php/**    1. destroystack ():  stack destruction     2. clearstack ():  stack to empty stack     3. stackempty ():  determine if stack is empty     4.  Stacklength ():  returns the length of the stack     5. gettop ():  Gets the element     6 of the top of the stack.  push ():  insert a new stack top element     7. pop ():  Delete the top element of the stack     8.  Stacktraverse ():  traverses the stack element  *///except for the push and pop methods, the rest of the methods are basically the same as the code implementations of the previous linear table's sequential storage class sqstack{     public  $SQARR;//Here is an array     private   $top for storing stack elements;  //represents the top of the stack, Corresponds to the last element of the array subscript     public function __construct ($SQARR) {          $this->sqarr= $SqArr;         $this->top= Count ($SQARR) destruction of -1;    }    //stacks     public  Function destroystack () {&NBsp;        $this->sqarr=null;          $this->top=-1;    }    //the stack to empty stack     public  function clearstack () {         $this->sqarr=array ();          $this->top=-1;    }     //determine if the stack is empty     public function stackempty () {         if ($this->top==-1) {             return  ' Null ';        }else{             return  ' No null ';         }    }    //returns the length of the stack     public  Function stacklength () { &Nbsp;      if (Is_null ($this->sqarr)) {             return null;        }         return  $this->top+1;    }     //gets the top element of the stack     public function gettop () {         if ($this->top==-1) {             return  ' ERROR ';        }         return  $this->sqarr[$this->top];    }     Inserts a new stack top element     public function push ($elem) {          $this->top++;         $this->sqarr[$this->top]=$ Elem;    }    //Delete Stack top element     public function pop () {         if ($this->top==-1) {             return  ' ERROR ';        }          $p = $this->sqarr[$this->top];         unset ($this->sqarr[$this->top]);         //Note, this sentence can or may not, if this statement exists, The top element of this stack is actually deleted in the stack memory, but if there is no such statement, the top pointer does not point to the top element of the stack. In addition, this statement has no corresponding implementation of the following traversal method is also different, there is no corresponding to the Stacktraverse () method, there is a corresponding StackTraverse2 () method.                  $this top--;        return  $p;    }     //Traversal stack element     public function stacktraverse () {          if (Is_null ($this->sqarr)) {            return  null;        }        $ Arr=array ();         for ($i =0; $i <= $this->top; $i +) {              $arr []= $this->sqarr[$i];         }        return  $arr;     }    public function stacktraverse2 () {         return  $this->sqarr;    }}


This article is from the "Everything Possible" blog, please be sure to keep this source http://noican.blog.51cto.com/4081966/1599657

Stack of data structures-sequential storage structure (PHP code implementation)

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.