PHP數據結構練習筆記--隊列

 1 <?php
 2     //隊列類
 3 class MyQueue{
 4     //變量:數組
 5     private $arr;
 6     
 7     //構造函數
 8     function __construct()
 9     {
10         $this->arr=array();
11         echo "初始化:";
12     }
13     
14     //析構函數
15     function __destruct()
16     {
17         unset($this->arr);
18         echo "釋放資源";
19     }
20     
21     //獲取隊頭元素
22     function GetHead()
23     {
24         if(count($this->arr)!=0)
25         {
26             return $this->arr[0];
27         }
28         else{
29             return false;
30         }
31     }
32     
33     //入隊列操做
34     function EnQueue($data)
35     {
36         array_push($this->arr,$data);
37     }
38     
39     //出隊列操做
40     function DeQueue()
41     {
42         if(count($this->arr)!=0)
43         {    
44             array_splice($this->arr,0,1);
45         }
46         else{
47             return false;
48         }
49     }
50     
51     //遍歷整個隊列
52     function QueueTraverse()
53     {
54         print_r($this->arr);
55     }
56     
57     //清空隊列
58     function ClearQueue()
59     {
60         unset($this->arr);
61         $this->arr=array();
62     }
63     
64     //判斷隊列是否爲空
65     function QueueEmpty()
66     {
67         if(count($this->arr)==0)
68         {
69             return true;
70         }
71         else{
72             return false;
73         }
74     }
75     
76     //獲取隊列長度
77     function QueueLength()
78     {
79         return count($this->arr);
80     }
81 }
82     
83 ?>

與棧類徹底同樣,除了進隊列方法的實現。php

相關文章
相關標籤/搜索