PHP implementation of data structure and algorithm queue and stack

1, Queues

1) Queue is a first in first out (FIFO) linear table. It only allows deletion at the front end of the table and insertion at the back end of the table. The end of the insertion is called the end of the queue, and the end of the deletion is called the head of the queue. That is to say, the team can only enter from the end of the team, and the team can only leave from the head of the team.

2) Generally, a queue has a front pointer and a rear pointer. When a queue does not store data, both the front pointer and the rear pointer point to the front of the queue.

3) Join operation: after the rear is moved, the data is stored in the unit pointed to by the rear, and the team is not allowed to join when the team is full, which also indicates that the front always points to the forerunner of the team head element.

4) Out of team operation: move the front backward, the element is out of the team, and cannot leave the team when the team is empty.

5) In PHP functions, array_ The push function is to add an element to the end of an array, that is, a queue operation; array_ The shift function is to delete the array header element, that is, the queue out operation.

$array =  array('a', 'b');
array_push($array, 'c'); //Join the team
array_shift($array);     //Queue out $array = array ('a ','b');
array_push($array, 'c'); //Join the team
array_shift($array);     //Out of the team

Array implementation of queue

/**
 * php Implementation of queue with array: FIFO
    1. getLength(): Get the length of the queue
    2. isEmpty(): Judge whether the queue is empty
    3. enqueue(): Join the team and add data at the end of the team.
    4. dequeue(): Leave the team, return and remove the team head data. You can't leave the team when it's empty.
    5. show(): Traverse the queue and output
    6. clear(): Clear queue
 */
class Queue {
    // Queue array
    public $dataStore = array();

    // Get the length of the queue
    public function getLength() {
        return count($this->dataStore);
    }
    // Judge whether the queue is empty
    public function isEmpty() {
        return $this->getLength() === 0;
    }
    // Join the team and add data at the end of the team.
    public function enqueue($element) {
        $this->dataStore[] = $element;
        // array_push($this->dataStore, $element);
    }
    // Leave the team, return and remove the team head data. You can't leave the team when it's empty.
    public function dequeue() {
        if (!$this->isEmpty()) {
            return array_shift($this->dataStore);
        }
        return false;
    }
    // Traverse the queue and output
    public function show() {
        if (!$this->isEmpty()) {
            for ($i = 0; $i < $this->getLength(); $i++) {
                echo $this->dataStore[$i] . PHP_EOL;
            }
        } else {
            return "empty";
        }
    }
    // Clear queue
    public function clearQueue() {
        unset($this->dataStore);
        // $this->dataStore = array();
    }
}
// test
$q = new Queue();
$q->enqueue('a');
$q->enqueue('b');
$q->enqueue('c');
echo 'The length of the queue is:' . $q->getLength();
echo "</br>";
echo 'The queue is:';
$q->show();
echo "</br>";
$q->dequeue();
echo "</br>";
echo "a Out of the queue:";
$q->show();
$q->clearQueue();
echo "After clearing the queue, the queue is" . $q->show();

Implementation of linked list in queue

When creating a chained queue, you need to define two structures, one for describing nodes and one for describing queues.

<?php
/**
 * php Realization of queue with linked list: FIFO
    1. isEmpty(): Judge whether the queue is empty
    2. enqueue(): Join the team and add data at the end of the team.
    3. dequeue(): Leave the team, return and remove the team head data. You can't leave the team when it's empty.
    4. clear(): Clear queue
    5. show(): Show elements in queue
 */
// Node class
class Node {
    public $data;   // Node data
    public $next;   // Next node

    public function __construct($data) {
        $this->data = $data;
        $this->next = NULL;
    }
}
// Queue class
class Queue {
    private $header;        // Head node

    function __construct($data) {
        $this->header = new Node($data);
    }
    // Judge whether the queue is empty
    public function isEmpty() {
        if ($this->header->next !== null) { // Not empty
            return false;
        }
        return true;
    }
    // Join the team and add data at the end of the team.
    public function enqueue($element) {
        $newNode = new Node($element);
        $current = $this->header;
        if ($current->next == null) { // Only the head node
            $this->header->next = $newNode;
        } else { // Traverse to the last element at the end of the team
            while ($current->next != null) {
                $current = $current->next;
            }
            $current->next = $newNode;
        }
        $newNode->next = null;
    }
    // Leave the team, return and remove the team head data. You can't leave the team when it's empty.
    public function dequeue() {
        if ($this->isEmpty()) { // Queue is empty
            return false;
        }
        // The header node has no practical significance. The queue head node is the node that the header points to.
        $current = $this->header;
        $current->next = $current->next->next;
    }
    // Clear queue
    public function clear() {
        $this->header = null;
    }
    // Show elements in queue
    public function show() {
        $current = $this->header;
        if ($this->isEmpty()) {
            echo "Empty!";
        }
        while ($current->next != null) {
            echo $current->next->data . PHP_EOL;
            $current = $current->next;
        }
    }
}
// test
$q = new Queue('header');
$q->enqueue('a');
$q->enqueue('b');
$q->enqueue('c');
echo "The queue is:";
$q->show();
echo "</br>";
echo "a Out of the queue:";
$q->dequeue();
$q->show();
echo "</br>";
$q->clear();
echo "After clearing the queue, the queue is";
$q->show();




2, Stack
1) Stack is a LIFO table. The insertion and deletion operations can only enter the table in one position. This position is at the end of the table, which is called Top. The other end is called Bottom, which is also called header.
2) The basic operations of the stack are push and pop, which means the stack in and stack out, equivalent to the insertion and deletion operations. When storing data, the first data entered is pushed to the bottom of the stack, and the second data entered is on the top of the stack; when reading data, data pops up from the top of the stack.


3) In PHP functions, array_ The push function is to add an element to the end of an array, that is, the stack operation; array_pop function is to delete the tail element of array, that is, stack operation.

$array =  array('a', 'b');
array_push($array, 'c'); //Push 
array_pop($array);       //Out of the stack


Array implementation of stack
The maximum capacity of the stack must be estimated in advance when selecting array to represent the stack content. Because once an array is created, its size cannot be changed, and setting an array too large may waste a lot of memory, and setting too small may overflow.

<?php
/**
 * php Implement stack with array: LIFO
    1. getLength(): Get the length of the stack
    2. push(): In the stack, add data at the top level.
    3. pop(): Stack, return and remove the top-level data.
    4. getTop(): Returns the value of the topmost data, but does not remove it
    5. clearStack(): Empty stack
    6. show(): Traversal stack element
 */
class Stack {
    // Using array to realize stack structure
    public $stack = array();

    // Get the length of the stack
    public function getLength() {
        return count($this->stack);
    }
    // In the stack, add data at the top level.
    public function push($element) {
        $this->stack[] = $element;
    }
    // Stack, return and remove the top-level data.
    public function pop() {
        if ($this->getLength() > 0) {
            return array_pop($this->stack);
        }
    }
    // Returns the value of the topmost data, but does not remove it
    public function getTop() {
        $top = $this->getLength() - 1;
        return $this->stack[$top];
    }
    // Empty stack
    public function clearStack() {
        unset($this->stack);
        // $this->stack = array();
    }
    // Traversal stack element
    public function show() {
        if ($this->getLength() > 0) {
            for ($i = 0; $i < $this->getLength(); $i++) {
                echo $this->stack[$i] . PHP_EOL;
            }
        }
        echo "Empty!";
    }
}
// test
$s = new Stack();
$s->push('a');
$s->push('b');
$s->push('c');
echo "The stack is:";
$s->show();
echo "</br>";
echo 'The element at the top of the stack is' . $s->getTop();
echo "</br>";
echo 'The length of the stack is:' . $s->getLength();
echo "</br>";
$s->pop();
echo "Pop up c,The stack is:";
$s->show();
echo "</br>";
echo "Empty the stack. The stack is:";
$s->clearStack();
$s->show();




Chain list implementation of stack

<?php
/**
 * php Implement stack with array: LIFO
    1. isEmpty(): Determine whether the queue is empty.
    2. push(): To insert a new top node
    3. pop(): Out of stack, delete top element
    4. clear(): Empty stack
    5. show(): Traversal stack element
 */
// Node class
class Node {
    public $data;   // Node data
    public $next;   // Next node

    public function __construct($data) {
        $this->data = $data;
        $this->next = NULL;
    }
}
class Stack {
    private $header;        // Head node

    function __construct($data) {
        $this->header = new Node($data);
    }

    // Judge whether the stack is empty
    public function isEmpty() {
        if ($this->header->next !== null) { // Not empty
            return false;
        }
        return true;
    }
    // To insert a new top node
    public function push($element) {
        $newNode = new Node($element);
        $current = $this->header;
        if ($current->next == null) { // Only the head node
            $this->header->next = $newNode;
        } else { // Traverse to the last element at the end of the stack
            while ($current->next != null) {
                $current = $current->next;
            }
            $current->next = $newNode;
        }
        $newNode->next = null;
    }
    // Out of stack, delete top element
    public function pop() {
        if ($this->isEmpty()) { // Stack is empty
            return false;
        }
        $current = $this->header;
        while ($current->next->next != null) {
            $current = $current->next;
        }
        $current->next = null;
    }
    // Empty stack
    public function clear() {
        $this->header = null;
    }
    // Show elements in stack
    public function show() {
        $current = $this->header;
        if ($this->isEmpty()) {
            echo "Empty!";
        }
        while ($current->next != null) {
            echo $current->next->data . PHP_EOL;
            $current = $current->next;
        }
    }
}
// test
$s = new Stack('header');
$s->push('a');
$s->push('b');
$s->push('c');
echo "The stack is:";
$s->show();
echo "</br>";
$s->pop();
echo "Pop up c,The stack is:";
$s->show();
echo "</br>";
echo "Empty the stack. The stack is:";
$s->clear();
$s->show();

I hope that the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced. There is no sense of direction when they write too much business code. I don't know where to start to improve. I have collated some information about this, including but not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoo For advanced advanced dry goods of multiple knowledge points, such as Le, Swoft, Kafka, Mysql optimization, shell script, Docker, microservice, Nginx, etc., you can share them for free, Need to stamp here for free

Tags: Programming PHP Laravel Redis kafka

Posted on Tue, 19 May 2020 04:14:52 -0400 by mgmoses