PHP Data Structure 5: implementation of the PHP stack and basic operations of the stack

Source: Internet
Author: User
Stack and queue are two widely used data structures. they all come from linear table data structures and are linear tables with limited operations.

Stack and queue are two widely used data structures. they all come from linear table data structures and are linear tables with limited operations.

Stack

Stack can be implemented on a computer in multiple ways:

Hard stack: use some Register groups in the CPU or similar hardware or special areas using memory. This type of stack has limited capacity, but is fast;

Soft stack: this type of stack is mainly implemented in memory. The stack capacity can be very large. In terms of implementation methods, there are two dynamic and static modes.

1. definition:
Stack: a linear table that is limited to insert and delete operations at one end of the table. It is also known as the Last In First Out (LIFO) or the First In First Out (First In Last Out) linear table.

Top: The end of a table that allows insert or delete operations. Use the stack top pointer to indicate the top elements of the stack.

Bottom stack (Bottom): It is a fixed end, also known as the header.

Empty stack: when there are no elements in the table, it is called an empty stack.

2. sequential storage representation of stacks

3. stack chain storage representation
The Stack's chain storage structure is called the chain stack, which is a single-chain table with limited operations. The insert and delete operations can only be performed at the header position. Therefore, the chain stack does not need to append header nodes as a single-chain table. The top pointer of the stack is the head pointer of the chain table. The following are the basic operations on the stack implemented by PHP. they are only created by myself and are for your reference only!

 

 MElem = null; $ this-> mNext = null;} class StackLinked {// the header "pointer" pointing to the stack top Element public $ mNext; public static $ mLength; /*** initialize stack ** @ return void */public function _ construct () {$ this-> mNext = null; self ::$ mLength = 0 ;} /*** determine whether the stack is empty. ** @ return boolean true is returned if the stack is empty. otherwise, false is returned */public function getIsEmpty () {if ($ this-> mNext = null) {return true;} else {return false ;}} /*** get all elements out of the stack ** @ return array returns all elements in the stack */public function getAllPop Stack () {$ e = array (); if ($ this-> getIsEmpty () {} else {while ($ this-> mNext! = Null) {$ e [] = $ this-> mNext-> mElem; $ this-> mNext = $ this-> mNext ;}} self :: $ mLength = 0; return $ e;}/*** return the number of elements in the stack ** @ return int */public static function getLength () {return self :: $ mLength;}/*** element stack ** @ param mixed $ e value of elements in the stack * @ return void **/public function getPushStack ($ e) {$ newLn = new LNode (); $ newLn-> mElem = $ e; $ newLn-> mNext = $ this-> mNext; $ this-> mNext = & $ newLn; self: $ mLength ++;}/*** element output stack ** @ param LNode $ e protection The variable * @ return boolean for storing the elements of the stack returns true if the stack is successfully output; otherwise, false is returned. **/public function getPopStack (& $ e) {if ($ this-> getIsEmpty () {return false;} $ p = $ this-> mNext; $ e = $ p-> mElem; $ this-> mNext = $ p-> mNext; self: $ mLength --;} /*** only returns all elements in the stack ** @ return an array composed of all elements in the array stack */public function getAllElem () {$ sldata = array (); if ($ this-> getIsEmpty () {} else {$ p = $ this-> mNext; while ($ p! = Null) {$ sldata [] = $ p-> mElem; $ p = $ p-> mNext;} return $ sldata ;}} /*** return the number of elements in the stack ** @ param mixed $ e value of the element to be searched * @ return int **/public function getCountForElem ($ e) {$ allelem = $ this-> getAllElem (); $ count = 0; foreach ($ allelem as $ value) {if ($ e = $ value) {$ count ++ ;}}return $ count ;}}

 

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.