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