> php教程 > php手册 > 본문

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

WBOY
풀어 주다: 2016-06-06 19:54:05
원래의
1366명이 탐색했습니다.

队列(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 '<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">'</pre><div class="contentsignin">로그인 후 복사</div></div><div class="contentsignin">로그인 후 복사</div></div>, print_r($queue->getQueue(), TRUE), '';

 

$queue->dequeue();

echo '<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">'</pre><div class="contentsignin">로그인 후 복사</div></div><div class="contentsignin">로그인 후 복사</div></div>, print_r($queue->getQueue(), TRUE), '';

?>

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

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!