This article mainly introduces the basic stack of PHP data structure, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
Stacks and queues are linear structures like the doubly linked list, the basis of actual PHP data structure mentioned before.
The stack follows the last-in-first-out principle (LIFO). This means that the stack has only one outlet for pushing elements and popping elements. When we perform push or pop operations, we must pay attention to whether the stack is full or whether the stack is empty.
Without further ado, let’s look directly at the common operations we perform on the stack.
push
pop
interface StackInterface { public function push(string $item); public function pop(); public function top(); public function isEmpty(); }
class ArrStack implements StackInterface { private $stack; private $limit; public function __construct(int $limit = 20) { $this->limit = $limit; $this->stack = []; } public function __get($val) { return $this->$val; } public function push(string $data = null) { if (count($this->stack) < $this->limit) { array_push($this->stack, $data); } else { throw new \OverflowException('stack is overflow'); } } public function pop() { if ($this->isEmpty()) { throw new \UnderflowException('stack is empty'); } else { return array_pop($this->stack); } } public function isEmpty() { return empty($this->stack); } public function top() { return end($this->stack); }
class LinkedListStack implements StackInterface { private $stack; private $limit; public function __construct(int $limit) { $this->limit = $limit; $this->stack = new LinkedList(); } public function top() { return $this->stack->getNthNode($this->stack->getSize() - 1)->data; } public function isEmpty() { return $this->stack->getSize() === 0; } public function pop() { if ($this->isEmpty()) { throw new \UnderflowException('stack is empty'); } else { $lastItem = $this->top(); $this->stack->deleteLast(); return $lastItem; } } public function push(string $item) { if ($this->stack->getSize() < $this->limit) { $this->stack->insert($item); } else { throw new \OverflowException('stack is overflow'); } }
"8 * (9 -2) + { (4 * 5) / ( 2 * 2) }
"5 * 8 * 9 / ( 3 * 2 ) )"
"[{ (2 * 7) + ( 15 - 3) ]"
class ExpressionChecker { //$expressions[] = "8 * (9 -2) + { (4 * 5) / ( 2 * 2) }"; //$expressions[] = "5 * 8 * 9 / ( 3 * 2 ) )"; //$expressions[] = "[{ (2 * 7) + ( 15 - 3) ]"; public function check(string $expression): bool { $stack = new \SplStack(); foreach (str_split($expression) as $item) { switch ($item) { case '{': case '[': case '(': $stack->push($item); break; case '}': case ']': case ')': if ($stack->isEmpty()) return false; $last = $stack->pop(); if ( $item == '{' && $last != '}' || $item == '(' && $last != ')' || $item == '[' && $last != ']' ) return false; break; } } if ($stack->isEmpty()) { return true; } return false; } }
php redis locking and unlocking
PHP method of operating Beanstalkd and parameter comments
The above is the detailed content of PHP data structure basic stack. For more information, please follow other related articles on the PHP Chinese website!