PHP implements a simple sequential stack

Source: Internet
Author: User

Stacks and Arrays: PHP arrays also have methods for pressing in and out of Array_push and Array_shift. is the array the stack data structure? Or the array can be completed, why the stack

Answer: Go to all parts of the country can use legs to walk, why choose a train plane? The array is the leg, and the stack is the train plane. First of all, the second stack allows us to focus on more core business needs.


Simple sequential Stack Implementation

class data{   private  $data;     public function __ Construct ($data) {         $this->data= $data;             }    public function getdata () {         return  $this->data;   }} class  Stack{    private  $size;    private  $top;//identification of top of stack      private  $stack =array ();     public function __construct ($ Size) {        return  $this->init_stack ($size);     }    public function init_stack ($size) {          $this->size= $size;         $this->top=-1 ;//The position element of the top of the stack     }     public function empty_stack () {         if ($this->top==-1)  return 1;        else             return 0;    }     public function full_stack () {         if ($this->top< ($this->size-1))  return 0;         else            return 1;     }//into the stack//originally intended to directly into the data object, and then tested as if there is a small problem, that is, single-case problem, it seems to need to create a new/*** each time @param   $data  class  Member of the data  ***/    public function push_stack ($data) {         if ($this->full_stack ())  echo  "full-stack";         else             $this->stack[++ $this->top] = new  Data ($data);     }    //out Stack     public function  pop_stack () {        if ($this->empty_stack ())  echo  " Stack empty no data! ";         else             unset ($this->stack[$this->top--]);     }    // Read stack top element     public function top_stack () {         return  $this->empty_stack ()? " Stack empty no data ": $this->stack[$this->top]->getdata ();         /*return   $this->empty_stack ()? " Stack empty no data! ": $this->stack[$this->top]->getdata ();  */    }}//test//$data 1 =new  Data (' hello '); $data 1= ' mini '; $stock  =nEw stack (5);  $stock->push_stack (' Jack '), $stock->push_stack ($data 1); $stock->push_stack ($ DATA1); $stock->push_stack ($data 1); $stock->empty_stack (); $stock->pop_stack (); $stock->pop_stack (); $ Stock->pop_stack ();//$stock->pop_stack ();////$stock->push_stack (");//$stock->echo  $stock- >top_stack ();? >

There is no re-writing about the circular linked list and the doubly linked list. Sequential storage is better understood.

Supplemental software applications for stacks: Word,ps, browser-used undo or rewind

The supplemental stack has all the characteristics of a linear table: it can be supplemented with several functions: Destroystack (Destroy) Clearstack (empty) stacklength (length). All functions are just a walkthrough, familiar with the idea, the next time to write any business logic, in line with the need to directly use the idea

PHP implements a simple sequential 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.