PHP stack and queue function instance sharing based on array implementation

Source: Internet
Author: User
This paper mainly introduces the stack and queue function based on the array implementation of PHP, and analyzes the function of PHP array-based Array_push (), Array_pop (), Array_shift () to stack and queue, stack and queue, and add Delete and other related operation skills, the need for friends can refer to, hope to help everyone.

Stacks and queues are two implementation forms of data structures and are containers that use very wide-ranging storage. Now, let's talk about the application of these two containers in PHP:

First, use an array to implement the stack:

1, the Stack container, the last stack will be the first out of the stack, that is, the so-called "advanced out" data structure.

2, in PHP, the array as a stack, you can use the array_push() function or " $array[]=$value " to complete the stack operation, using the array_pop() function to complete the stack operation.

3, stack into the stack operation is equivalent to: the data into a barrel-like (assuming the data and the bucket has the appropriate surface area, that is, just can be horizontally put in) the container, resulting in the completion of all the data into the stack, the advanced stack at the bottom.

4. Example of array-in-stack:


<?php   $mypara = Array ("Para1");   Echo (Array_push ($mypara, "Para2"));  Add a data to the Mypara array   print_r ($mypara);   $mypara 1=array ("a" = "Para11", "b" = "para12");   Echo Array_push ($mypara 1, "Para13", "para14");    Print_r ($mypara 1);  Array ([A] = Para11 [b] = para12 [0] = = para13 [2] = = para14)   $mypara 1["c"] = "PARA15";  Add Print_r in "$array []= $value" Form   ($mypara 1)//array ([A] = Para11 [b] = para12 [0] = = para13 [2] = par A14 [C] = PARA15)?>

5. Example of an array stack:


<?php   $mypara = Array ("Para1", "Para2", "Para3", "Para4");   echo Array_pop ($mypara); Return the last element to the deleted value popup, Output PHP  print_r ($mypara);//array ([0] = para1 [1]=>PARA2 [2]=>PARA3]?>

Second, use the array to implement the queue:

1, in the data structure, the queue and stack are different, follow the "FIFO" principle.

2, the queue for example to understand, like infusion needles, first into the thin tube of liquid first into the body.

3, in PHP, the array as a queue, you can use the array_push() function or " $array[]=$value " to complete the addition of data operations, the use of array_shift() functions to complete the deletion of data operations.

4. Example of array deletion queue data:


<?php   $mypara = Array ("A" = "Para1", "b" = "Para2", "c" = "para3");   echo Array_shift ($mypara);   Print_r ($mypara);? >

5, Note: PHP also provides another one from the beginning of the queue array to insert one or more elements, the function executes successfully will return the number of inserted elements, using the same format and function array_push() . That is, you can use array_unshift() functions and array_shift() functions to queue operations.

Related Article

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.