@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | public $next = null, $prev = null; //A reference to the next and previous node |
| 13 | 13 | public $data = null; //Data or a reference to data |
| 14 | 14 | |
| 15 | - public function __construct($data=null) |
|
| 15 | + public function __construct($data = null) |
|
| 16 | 16 | { |
| 17 | 17 | $this->data = $data; |
| 18 | 18 | } |
@@ -143,23 +143,23 @@ discard block |
||
| 143 | 143 | |
| 144 | 144 | public function get($index) |
| 145 | 145 | { |
| 146 | - if($index < 0 || $index > $this->size-1) |
|
| 146 | + if ($index < 0 || $index > $this->size-1) |
|
| 147 | 147 | { |
| 148 | 148 | throw new IndexOutOfBounds("The given index is out of bounds!") |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - if($index == 0) |
|
| 151 | + if ($index == 0) |
|
| 152 | 152 | { |
| 153 | 153 | return $this->firstNode; |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - if($index == $this->size-1) |
|
| 156 | + if ($index == $this->size-1) |
|
| 157 | 157 | { |
| 158 | 158 | return $this->lastNode; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | $node = $this->firstNode; |
| 162 | - for($i = 1; $i <= $index; $i++) |
|
| 162 | + for ($i = 1; $i <= $index; $i++) |
|
| 163 | 163 | { |
| 164 | 164 | $node = $node->next; |
| 165 | 165 | } |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | public $next = null, $prev = null; //A reference to the next and previous node |
| 13 | 13 | public $data = null; //Data or a reference to data |
| 14 | 14 | |
| 15 | - public function __construct($data=null) |
|
| 15 | + public function __construct($data=null) |
|
| 16 | 16 | { |
| 17 | 17 | $this->data = $data; |
| 18 | 18 | } |
@@ -24,13 +24,19 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | private $size = 0; |
| 26 | 26 | |
| 27 | - public function __construct() {} |
|
| 27 | + public function __construct() |
|
| 28 | + { |
|
| 29 | +} |
|
| 28 | 30 | |
| 29 | 31 | public function insert_after($node, $newNode) |
| 30 | 32 | { |
| 31 | 33 | //type checks |
| 32 | - if (get_class($node) != "DLNode") {throw new InvalidArgument("Expected a DLNode, got: " . get_class($node)); } |
|
| 33 | - if (get_class($newNode) != "DLNode") {throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 34 | + if (get_class($node) != "DLNode") |
|
| 35 | + { |
|
| 36 | +throw new InvalidArgument("Expected a DLNode, got: " . get_class($node)); } |
|
| 37 | + if (get_class($newNode) != "DLNode") |
|
| 38 | + { |
|
| 39 | +throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 34 | 40 | |
| 35 | 41 | $newNode->prev = $node; |
| 36 | 42 | if ($node->next == null) |
@@ -50,8 +56,12 @@ discard block |
||
| 50 | 56 | public function insert_before($node, $newNode) |
| 51 | 57 | { |
| 52 | 58 | //type checks |
| 53 | - if (get_class($node) != "DLNode") {throw new InvalidArgument("Expected a DLNode, got: " . get_class($node)); } |
|
| 54 | - if (get_class($newNode) != "DLNode") {throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 59 | + if (get_class($node) != "DLNode") |
|
| 60 | + { |
|
| 61 | +throw new InvalidArgument("Expected a DLNode, got: " . get_class($node)); } |
|
| 62 | + if (get_class($newNode) != "DLNode") |
|
| 63 | + { |
|
| 64 | +throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 55 | 65 | |
| 56 | 66 | $newNode->next = $node; |
| 57 | 67 | if ($node->prev == null) |
@@ -69,9 +79,11 @@ discard block |
||
| 69 | 79 | } |
| 70 | 80 | |
| 71 | 81 | public function insert_beginning($newNode) |
| 72 | - { |
|
| 82 | + { |
|
| 73 | 83 | //type checks |
| 74 | - if (get_class($newNode) != "DLNode") {throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 84 | + if (get_class($newNode) != "DLNode") |
|
| 85 | + { |
|
| 86 | +throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 75 | 87 | |
| 76 | 88 | if ($this->firstNode == null) |
| 77 | 89 | { |
@@ -103,13 +115,17 @@ discard block |
||
| 103 | 115 | public function contains($node) |
| 104 | 116 | { |
| 105 | 117 | //check this so we could possibly avoid list traversal |
| 106 | - if ($node == $this->firstNode || $node == $this->lastNode) {return true; } |
|
| 118 | + if ($node == $this->firstNode || $node == $this->lastNode) |
|
| 119 | + { |
|
| 120 | +return true; } |
|
| 107 | 121 | |
| 108 | 122 | //traverse the list and try to find the node |
| 109 | 123 | $n = $this->firstNode->next; //we can start one node ahead as we already checked the firstNode |
| 110 | 124 | while ($n != null) |
| 111 | 125 | { |
| 112 | - if ($n == $node) {return true; } |
|
| 126 | + if ($n == $node) |
|
| 127 | + { |
|
| 128 | +return true; } |
|
| 113 | 129 | $n = $n->next; |
| 114 | 130 | } |
| 115 | 131 | |
@@ -119,7 +135,9 @@ discard block |
||
| 119 | 135 | public function remove($node) |
| 120 | 136 | { |
| 121 | 137 | //check if the node exists |
| 122 | - if ($this->contains($node) == false) { throw new InvalidArgument("The node given does not exist in this list!"); } |
|
| 138 | + if ($this->contains($node) == false) |
|
| 139 | + { |
|
| 140 | +throw new InvalidArgument("The node given does not exist in this list!"); } |
|
| 123 | 141 | |
| 124 | 142 | if ($node->prev == null) |
| 125 | 143 | { |
@@ -6,13 +6,13 @@ |
||
| 6 | 6 | |
| 7 | 7 | class IndexOutOfBounds extends Exception |
| 8 | 8 | { |
| 9 | - public function __construct($message="Index is Out Of Bounds!", $code = 4, Exception $previous = null) |
|
| 9 | + public function __construct($message="Index is Out Of Bounds!", $code = 4, Exception $previous = null) |
|
| 10 | 10 | { |
| 11 | - parent::__construct($message, $code, $previous); |
|
| 12 | - } |
|
| 11 | + parent::__construct($message, $code, $previous); |
|
| 12 | + } |
|
| 13 | 13 | |
| 14 | - public function __toString() |
|
| 14 | + public function __toString() |
|
| 15 | 15 | { |
| 16 | - return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; |
|
| 17 | - } |
|
| 16 | + return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; |
|
| 17 | + } |
|
| 18 | 18 | } |
| 19 | 19 | \ No newline at end of file |
@@ -6,7 +6,7 @@ |
||
| 6 | 6 | |
| 7 | 7 | class IndexOutOfBounds extends Exception |
| 8 | 8 | { |
| 9 | - public function __construct($message="Index is Out Of Bounds!", $code = 4, Exception $previous = null) |
|
| 9 | + public function __construct($message = "Index is Out Of Bounds!", $code = 4, Exception $previous = null) |
|
| 10 | 10 | { |
| 11 | 11 | parent::__construct($message, $code, $previous); |
| 12 | 12 | } |
@@ -6,13 +6,13 @@ |
||
| 6 | 6 | |
| 7 | 7 | class IndexOutOfBounds extends Exception |
| 8 | 8 | { |
| 9 | - public function __construct($message="Index is Out Of Bounds!", $code = 4, Exception $previous = null) |
|
| 10 | - { |
|
| 9 | + public function __construct($message="Index is Out Of Bounds!", $code = 4, Exception $previous = null) |
|
| 10 | + { |
|
| 11 | 11 | parent::__construct($message, $code, $previous); |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | - public function __toString() |
|
| 15 | - { |
|
| 14 | + public function __toString() |
|
| 15 | + { |
|
| 16 | 16 | return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; |
| 17 | 17 | } |
| 18 | 18 | } |
| 19 | 19 | \ No newline at end of file |