@@ -12,7 +12,9 @@ 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() {} |
|
| 15 | + public function __construct() |
|
| 16 | + { |
|
| 17 | +} |
|
| 16 | 18 | } |
| 17 | 19 | |
| 18 | 20 | class DoublyLinkedList implements Countable |
@@ -21,13 +23,19 @@ discard block |
||
| 21 | 23 | |
| 22 | 24 | private $size = 0; |
| 23 | 25 | |
| 24 | - public function __construct() {} |
|
| 26 | + public function __construct() |
|
| 27 | + { |
|
| 28 | +} |
|
| 25 | 29 | |
| 26 | 30 | public function insert_after($node, $newNode) |
| 27 | 31 | { |
| 28 | 32 | //type checks |
| 29 | - if (get_class($node) != "DLNode") {throw new InvalidArgument("Expected a DLNode, got: " . get_class($node)); } |
|
| 30 | - if (get_class($newNode) != "DLNode") {throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 33 | + if (get_class($node) != "DLNode") |
|
| 34 | + { |
|
| 35 | +throw new InvalidArgument("Expected a DLNode, got: " . get_class($node)); } |
|
| 36 | + if (get_class($newNode) != "DLNode") |
|
| 37 | + { |
|
| 38 | +throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 31 | 39 | |
| 32 | 40 | $newNode->prev = $node; |
| 33 | 41 | if ($node->next == null) |
@@ -47,8 +55,12 @@ discard block |
||
| 47 | 55 | public function insert_before($node, $newNode) |
| 48 | 56 | { |
| 49 | 57 | //type checks |
| 50 | - if (get_class($node) != "DLNode") {throw new InvalidArgument("Expected a DLNode, got: " . get_class($node)); } |
|
| 51 | - if (get_class($newNode) != "DLNode") {throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 58 | + if (get_class($node) != "DLNode") |
|
| 59 | + { |
|
| 60 | +throw new InvalidArgument("Expected a DLNode, got: " . get_class($node)); } |
|
| 61 | + if (get_class($newNode) != "DLNode") |
|
| 62 | + { |
|
| 63 | +throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 52 | 64 | |
| 53 | 65 | $newNode->next = $node; |
| 54 | 66 | if ($node->prev == null) |
@@ -66,9 +78,11 @@ discard block |
||
| 66 | 78 | } |
| 67 | 79 | |
| 68 | 80 | public function insert_beginning($newNode) |
| 69 | - { |
|
| 81 | + { |
|
| 70 | 82 | //type checks |
| 71 | - if (get_class($newNode) != "DLNode") {throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 83 | + if (get_class($newNode) != "DLNode") |
|
| 84 | + { |
|
| 85 | +throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
| 72 | 86 | |
| 73 | 87 | if ($this->firstNode == null) |
| 74 | 88 | { |
@@ -100,13 +114,17 @@ discard block |
||
| 100 | 114 | public function contains($node) |
| 101 | 115 | { |
| 102 | 116 | //check this so we could possibly avoid list traversal |
| 103 | - if ($node == $this->firstNode || $node == $this->lastNode) {return true; } |
|
| 117 | + if ($node == $this->firstNode || $node == $this->lastNode) |
|
| 118 | + { |
|
| 119 | +return true; } |
|
| 104 | 120 | |
| 105 | 121 | //traverse the list and try to find the node |
| 106 | 122 | $n = $this->firstNode->next; //we can start one node ahead as we already checked the firstNode |
| 107 | 123 | while ($n != null) |
| 108 | 124 | { |
| 109 | - if ($n == $node) {return true; } |
|
| 125 | + if ($n == $node) |
|
| 126 | + { |
|
| 127 | +return true; } |
|
| 110 | 128 | $n = $n->next; |
| 111 | 129 | } |
| 112 | 130 | |
@@ -116,7 +134,9 @@ discard block |
||
| 116 | 134 | public function remove($node) |
| 117 | 135 | { |
| 118 | 136 | //check if the node exists |
| 119 | - if ($this->contains($node) == false) {throw new InvalidArgument("The node given does not exist in this list!"); } |
|
| 137 | + if ($this->contains($node) == false) |
|
| 138 | + { |
|
| 139 | +throw new InvalidArgument("The node given does not exist in this list!"); } |
|
| 120 | 140 | |
| 121 | 141 | if ($node->prev == null) |
| 122 | 142 | { |