How to achieve the effect of stack and queue?

autoload
Release: 2023-03-08 15:40:01
Original
1453 people have browsed it

Stack and Queue

  1. StackandQueueboth belong to thedata structure

  2. StackisLIFO

  3. ##QueueisFIFO

1. Implement stack array

array_push(array input array, value should be pushed into the first value at the end of array )

array_pop(): Pop the last element from the stack.

 1 [1] => 14 [2] => 34 [3] => 89 ) ?>
Copy after login

2. Implement the array of

queue

array_shift(): Dequeue and remove the first element in the queue

array_unshift(): Insert an element at the beginning of the array

 1 [1] => 14 [2] => 34 [3] => 89 [4] => 67 ) echo "
"; array_shift($array);//将先进入队列的数组元素,出队列 print_r($array);//Array ( [0] => 14 [1] => 34 [2] => 89 [3] => 67 ) echo "
"; array_unshift($array,'66');//在队列头部插入一个元素 print_r($array);//Array ( [0] => 66 [1] => 14 [2] => 34 [3] => 89 [4] => 67 ) ?>
Copy after login

Recommended:

php tutorial,php video tutorial

The above is the detailed content of How to achieve the effect of stack and queue?. 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
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!