Passed
Push — development ( 91cbfb...a0df82 )
by Dylan David
02:26
created

Queue::dequeue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace Ptypes;
4
5
use Countable;
6
use Ptypes\DLNode;
7
use Ptypes\DoublyLinkedList;
8
9
/**
10
 * Queue
11
 * Is a First In First Out (FIFO) data structure.
12
 *
13
 * Functions: enqueue, dequeue, peek, and size
14
 */
15
class Queue implements Countable
16
{
17
	/**
18
	 * Is the doubly linked list which the queue wraps around.
19
	 */
20
	private $list;
21
	
22
	public function __construct()
23
	{
24
		 $this->list = new DoublyLinkedList();
25
	}
26
	
27
	/**
28
	 * Enqueue
29
	 * Inserts the element at the tail of the queue.
30
	 *
31
	 * @param $object
32
	 */
33
	public function enqueue($object)
34
	{
35
		$this->list->insert_beginning(new DLNode($object));
36
		return $this; //allows chaining
37
	}
38
	
39
	/**
40
	 * Dequeue
41
	 * Removes the element at the head of the queue.
42
	 */
43
	public function dequeue()
44
	{
45
		if($this->list->size() == 0) {return $this;}
46
		$this->list->remove($this->list->lastNode);
47
		return $this; //allows chaining
48
	}
49
	
50
	/**
51
	 * Peek
52
	 * Returns the element at the head of the queue.
53
	 *
54
	 * @return object
55
	 */
56
	public function peek()
57
	{
58
		return $this->list->lastNode->data;
59
	}
60
	
61
	/**
62
	 * Size
63
	 * Gets the size of the queue.
64
	 *
65
	 * @return int
66
	 */
67
	public function size()
68
	{
69
		return $this->list->size();
70
	}
71
	
72
	/**
73
	 * is_empty
74
	 * Determines if the queue is empty or not.
75
	 *
76
	 * @return boolean
77
	 */
78
	public function is_empty()
79
	{
80
		return ($this->list->size() == 0) ? true : false;
81
	}
82
	
83
	//Overriding functions & magic methods below
84
85
	/**
86
	 * Count, same functionality as Size.
87
	 * Overrides the default count function call on this class.
88
	 *
89
	 * @return int
90
	 */
91
	public function count()
92
	{
93
		return $this->list->size();
94
	}
95
}