Heim  >  Artikel  >  php教程  >  PHP实现队列(Queue)数据结构

PHP实现队列(Queue)数据结构

WBOY
WBOYOriginal
2016-06-06 19:54:051364Durchsuche

队列(Queue),是一种特殊的先进先出线性表,其只能在前端进行删除操作(一般称为出队),在后端进行插入操作(一般称为入队)。进行删除操作的端称为队头,进行插入操作的端称为队尾。队列,是按照先进先出或后进后出的原则组织数据。当队列中没有元素时,

队列(Queue),是一种特殊的先进先出线性表,其只能在前端进行删除操作(一般称为出队),在后端进行插入操作(一般称为入队)。进行删除操作的端称为队头,进行插入操作的端称为队尾。队列,是按照先进先出或后进后出的原则组织数据。当队列中没有元素时,称为空队列。

数据结构与算法(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

<?php

/**

 * 数据结构与算法(PHP实现) - 队列(Queue)。

 *

 * @author 创想编程(TOPPHP.ORG)

 * @copyright Copyright (c) 2013 创想编程(TOPPHP.ORG) All Rights Reserved

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

 * @version 1.0.0 - Build20130607

 */

class Queue {

  /**

   * 队列。

   *

   * @var array

   */

  private $queue;

 

  /**

   * 队列的长度。

   *

   * @var integer

   */

  private $size;

 

  /**

   * 构造方法 - 初始化数据。

   */

  public function __construct() {

    $this->queue = array();

    $this->size = 0;

  }

 

  /**

   * 入队操作。

   *

   * @param mixed $data 入队数据。

   * @return object 返回对象本身。

   */

  public function enqueue($data) {

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

 

    return $this;

  }

 

  /**

   * 出队操作。

   *

   * @return mixed 空队列时返回FALSE,否则返回队头元素。

   */

  public function dequeue() {

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

      --$this->size;

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

 

      return $front[0];

    }

 

    return FALSE;

  }

 

  /**

   * 获取队列。

   *

   * @return array 返回整个队列。

   */

  public function getQueue() {

    return $this->queue;

  }

 

  /**

   * 获取队头元素。

   *

   * @return mixed 空队列时返回FALSE,否则返回队头元素。

   */

  public function getFront() {

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

      return $this->queue[0];

    }

 

    return FALSE;

  }

 

  /**

   * 获取队列的长度。

   *

   * @return integer 返回队列的长度。

   */

  public function getSize() {

    return $this->size;

  }

 

  /**

   * 检测队列是否为空。

   *

   * @return boolean 空队列则返回TRUE,否则返回FALSE。

   */

  public function isEmpty() {

    return 0 === $this->size;

  }

}

?>

示例代码

1

2

3

4

5

6

7

8

<?php

$queue = new Queue();

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

echo '<pre class="brush:php;toolbar:false">'</pre>, print_r($queue->getQueue(), TRUE), '';

 

$queue->dequeue();

echo '<pre class="brush:php;toolbar:false">'</pre>, print_r($queue->getQueue(), TRUE), '';

?>

说明:PHP数组函数已有类似队列的功能函数存在:array_unshift(入队)和、array_shift(出队)。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP中日期加减方法Nächster Artikel:vim php语法检查