Detailed explanation of data structure implementation of PHP queue and stack

WBOY
Release: 2024-05-07 09:42:01
Original
369 people have browsed it

The queue follows the "first in, first out" principle and can be implemented using an array or linked list; the stack follows the "last in first out" principle and can also be implemented using an array or linked list. Specific implementation methods include: queue array implementation, queue linked list implementation, stack array implementation, and stack linked list implementation. Practical cases demonstrate the application of queues and stacks in message printing and array reversal.

PHP 队列和堆栈的数据结构实现详解

Detailed explanation of the data structure implementation of PHP queue and stack

Queue and stack are a common linear data structure. They possess unique properties and are widely used in a variety of applications. This article will introduce the data structure implementation of queues and stacks in PHP and provide practical cases.

Queue

The queue follows the "first in, first out" (FIFO) principle. The oldest inserted element in the queue will be removed first. Queues can be implemented using arrays or linked lists.

Array implementation:

class Queue
{
    private $queue = [];

    public function enqueue($item)
    {
        $this->queue[] = $item;
    }

    public function dequeue()
    {
        if (empty($this->queue)) {
            throw new Exception("Queue is empty");
        }
        return array_shift($this->queue);
    }
}
Copy after login

Linked list implementation:

class Node
{
    public $data;
    public $next;

    public function __construct($data)
    {
        $this->data = $data;
        $this->next = null;
    }
}

class Queue
{
    private $head;
    private $tail;

    public function enqueue($item)
    {
        $node = new Node($item);
        if (empty($this->head)) {
            $this->head = $node;
            $this->tail = $node;
        } else {
            $this->tail->next = $node;
            $this->tail = $node;
        }
    }

    public function dequeue()
    {
        if (empty($this->head)) {
            throw new Exception("Queue is empty");
        }
        $item = $this->head->data;
        $this->head = $this->head->next;
        if (empty($this->head)) {
            $this->tail = null;
        }
        return $item;
    }
}
Copy after login

Practical case: Use queue printing Message

$queue = new Queue();
$queue->enqueue("Hello");
$queue->enqueue("World");
while (!$queue->isEmpty()) {
    echo $queue->dequeue() . "<br>";
}
Copy after login

Stack

The stack follows the "last in, first out" (LIFO) principle. The last inserted element in the stack will be removed first. Stacks can be implemented using arrays or linked lists.

Array implementation:

class Stack
{
    private $stack = [];

    public function push($item)
    {
        $this->stack[] = $item;
    }

    public function pop()
    {
        if (empty($this->stack)) {
            throw new Exception("Stack is empty");
        }
        return array_pop($this->stack);
    }
}
Copy after login

Linked list implementation:

class Node
{
    public $data;
    public $next;

    public function __construct($data)
    {
        $this->data = $data;
        $this->next = null;
    }
}

class Stack
{
    private $top;

    public function push($item)
    {
        $node = new Node($item);
        $node->next = $this->top;
        $this->top = $node;
    }

    public function pop()
    {
        if (empty($this->top)) {
            throw new Exception("Stack is empty");
        }
        $item = $this->top->data;
        $this->top = $this->top->next;
        return $item;
    }
}
Copy after login

Practical case: Use stack reverse order An array

$stack = new Stack();
$array = [1, 2, 3, 4, 5];
foreach ($array as $item) {
    $stack->push($item);
}
$reversedArray = [];
while (!$stack->isEmpty()) {
    $reversedArray[] = $stack->pop();
}
print_r($reversedArray);
Copy after login

The above is the detailed content of Detailed explanation of data structure implementation of PHP queue and stack. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!