PHP implements the Queue Data Structure

Source: Internet
Author: User
A Queue is a special first-in-first-out linear table that can only be deleted at the front end (usually called a Queue) and inserted at the backend (usually called a Queue ). The end of the delete operation is called the head of the team, and the end of the insert operation is called the end of the team. A queue organizes data according to the principle of first-in-first-out or second-out. When no elements exist in the queue,

A Queue is a special first-in-first-out linear table that can only be deleted at the front end (usually called a Queue) and inserted at the backend (usually called a Queue ). The end of the delete operation is called the head of the team, and the end of the insert operation is called the end of the team. A queue organizes data according to the principle of first-in-first-out or second-out. When no elements exist in the queue,

A Queue is a special first-in-first-out linear table that can only be deleted at the front end (usually called a Queue) and inserted at the backend (usually called a Queue ). The end of the delete operation is called the head of the team, and the end of the insert operation is called the end of the team. A queue organizes data according to the principle of first-in-first-out or second-out. When there are no elements in the queue, it is called an empty queue.

Data Structure and algorithm (implemented in PHP)-Queue)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

/**

* Data structures and algorithms (implemented in PHP)-Queue ).

*

* @ Author creative programming (TOPPHP. ORG)

* @ Copyright Copyright (c) 2013 creative programming (TOPPHP. ORG) All Rights Reserved

* @license http://www.opensource.org/licenses/mit-license.php MIT LICENSE

* @version 1.0.0 - Build20130607

*/

class Queue {

/**

* Queue.

*

* @var array

*/

private$queue;

/**

* Queue length.

*

* @var integer

*/

private$size;

/**

* Constructor-Initialize data.

*/

publicfunction __construct() {

$this->queue =array();

$this->size = 0;

}

/**

* Queue operations.

*

* @ Param mixed $ data queue data.

* @ Return object returns the object itself.

*/

publicfunction enqueue($data) {

$this->queue[$this->size++] =$data;

return$this;

}

/**

* Team-out operations.

*

* @ Return mixed: FALSE is returned when the queue is empty. Otherwise, the Header element of the queue is returned.

*/

publicfunction dequeue() {

if(!$this->isEmpty()) {

--$this->size;

$front= array_splice($this->queue, 0, 1);

return$front[0];

}

returnFALSE;

}

/**

* Get the queue.

*

* @ Return array returns the entire queue.

*/

publicfunction getQueue() {

return$this->queue;

}

/**

* Obtain the element of the team header.

*

* @ Return mixed: FALSE is returned when the queue is empty. Otherwise, the Header element of the queue is returned.

*/

publicfunction getFront() {

if(!$this->isEmpty()) {

return$this->queue[0];

}

returnFALSE;

}

/**

* Get the queue length.

*

* @ Return integer returns the length of the queue.

*/

publicfunction getSize() {

return$this->size;

}

/**

* Check whether the queue is empty.

*

* @ Return boolean if the queue is empty, TRUE is returned; otherwise, FALSE is returned.

*/

publicfunction isEmpty() {

return0 === $this->size;

}

}

?>

Sample Code

1

2

3

4

5

6

7

8

$queue = new Queue();

$queue->enqueue(1)->enqueue(2)->enqueue(3)->enqueue(4)->enqueue(5)->enqueue(6);

echo '

', print_r($queue->getQueue(), TRUE),'
';

$queue->dequeue();

echo '

', print_r($queue->getQueue(), TRUE),'
';

?>

Note: PHP array functions such as queue functions exist: array_unshift (queuing) and array_shift (queuing ).

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.