-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathQueueUsingStacks.php
More file actions
70 lines (59 loc) · 1.23 KB
/
QueueUsingStacks.php
File metadata and controls
70 lines (59 loc) · 1.23 KB
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
<?php
class MyQueue
{
/**
* Initialize your data structure here.
*/
public $stock = [];
public $temp = [];
function __construct()
{
}
/**
* Push element x to the back of queue.
* @param Integer $x
* @return NULL
*/
function push($x)
{
while (!empty($this->stock)) {
array_unshift($this->temp, array_shift($this->stock));
}
array_unshift($this->stock, $x);
while (!empty($this->temp)) {
array_unshift($this->stock, array_shift($this->temp));
}
}
/**
* Removes the element from in front of queue and returns that element.
* @return Integer
*/
function pop()
{
return array_shift($this->stock);
}
/**
* Get the front element.
* @return Integer
*/
function peek()
{
return current($this->stock);
}
/**
* Returns whether the queue is empty.
* @return Boolean
*/
function empty()
{
return empty($this->stock);
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* $obj = MyQueue();
* $obj->push($x);
* $ret_2 = $obj->pop();
* $ret_3 = $obj->peek();
* $ret_4 = $obj->empty();
*/