@@ -6,13 +6,13 @@ |
||
6 | 6 | |
7 | 7 | class InvalidArgument extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Invalid Argument!", $code = 1, Exception $previous = null) |
|
9 | + public function __construct($message="Invalid Argument!", $code = 1, 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 InvalidArgument extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Invalid Argument!", $code = 1, Exception $previous = null) |
|
9 | + public function __construct($message = "Invalid Argument!", $code = 1, Exception $previous = null) |
|
10 | 10 | { |
11 | 11 | parent::__construct($message, $code, $previous); |
12 | 12 | } |
@@ -6,13 +6,13 @@ |
||
6 | 6 | |
7 | 7 | class InvalidArgument extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Invalid Argument!", $code = 1, Exception $previous = null) |
|
10 | - { |
|
9 | + public function __construct($message="Invalid Argument!", $code = 1, 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 |
@@ -6,13 +6,13 @@ |
||
6 | 6 | |
7 | 7 | class StackUnderflow extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Stack Underflow!", $code = 3, Exception $previous = null) |
|
9 | + public function __construct($message="Stack Underflow!", $code = 3, 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 StackUnderflow extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Stack Underflow!", $code = 3, Exception $previous = null) |
|
9 | + public function __construct($message = "Stack Underflow!", $code = 3, Exception $previous = null) |
|
10 | 10 | { |
11 | 11 | parent::__construct($message, $code, $previous); |
12 | 12 | } |
@@ -6,13 +6,13 @@ |
||
6 | 6 | |
7 | 7 | class StackUnderflow extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Stack Underflow!", $code = 3, Exception $previous = null) |
|
10 | - { |
|
9 | + public function __construct($message="Stack Underflow!", $code = 3, 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 |
@@ -6,13 +6,13 @@ |
||
6 | 6 | |
7 | 7 | class StackOverflow extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Stack Overflow!", $code = 2, Exception $previous = null) |
|
9 | + public function __construct($message="Stack Overflow!", $code = 2, 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 StackOverflow extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Stack Overflow!", $code = 2, Exception $previous = null) |
|
9 | + public function __construct($message = "Stack Overflow!", $code = 2, Exception $previous = null) |
|
10 | 10 | { |
11 | 11 | parent::__construct($message, $code, $previous); |
12 | 12 | } |
@@ -6,13 +6,13 @@ |
||
6 | 6 | |
7 | 7 | class StackOverflow extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Stack Overflow!", $code = 2, Exception $previous = null) |
|
10 | - { |
|
9 | + public function __construct($message="Stack Overflow!", $code = 2, 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 |
@@ -6,13 +6,13 @@ |
||
6 | 6 | |
7 | 7 | class UnexpectedType extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Unexpected Type given!", $code = 0, Exception $previous = null) |
|
9 | + public function __construct($message="Unexpected Type given!", $code = 0, 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 UnexpectedType extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Unexpected Type given!", $code = 0, Exception $previous = null) |
|
9 | + public function __construct($message = "Unexpected Type given!", $code = 0, Exception $previous = null) |
|
10 | 10 | { |
11 | 11 | parent::__construct($message, $code, $previous); |
12 | 12 | } |
@@ -6,13 +6,13 @@ |
||
6 | 6 | |
7 | 7 | class UnexpectedType extends Exception |
8 | 8 | { |
9 | - public function __construct($message="Unexpected Type given!", $code = 0, Exception $previous = null) |
|
10 | - { |
|
9 | + public function __construct($message="Unexpected Type given!", $code = 0, 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 |
@@ -29,8 +29,8 @@ discard block |
||
29 | 29 | */ |
30 | 30 | public function __construct($maxsize) |
31 | 31 | { |
32 | - if(gettype($maxsize) != "integer"){throw new UnexpectedType("Expected an integer, got: ".gettype($maxsize));} |
|
33 | - if($maxsize < 2) {throw new InvalidArgument("Stack has a minimum size of 2!");} |
|
32 | + if (gettype($maxsize) != "integer") {throw new UnexpectedType("Expected an integer, got: " . gettype($maxsize)); } |
|
33 | + if ($maxsize < 2) {throw new InvalidArgument("Stack has a minimum size of 2!"); } |
|
34 | 34 | |
35 | 35 | $this->maxsize = $maxsize; |
36 | 36 | $this->items = array(); |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | */ |
45 | 45 | public function push($item) |
46 | 46 | { |
47 | - if($this->top == $this->maxsize) {throw new StackOverflow("Pushing this item would overflow the stack (exceed the max stack size)!");} |
|
47 | + if ($this->top == $this->maxsize) {throw new StackOverflow("Pushing this item would overflow the stack (exceed the max stack size)!"); } |
|
48 | 48 | $this->items[$this->top++] = $item; |
49 | 49 | return $this; |
50 | 50 | } |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public function pop() |
56 | 56 | { |
57 | - if($this->top == 0) {throw new StackUnderflow("Cannot pop an empty stack!");} |
|
57 | + if ($this->top == 0) {throw new StackUnderflow("Cannot pop an empty stack!"); } |
|
58 | 58 | unset($this->items[$this->top--]); |
59 | 59 | return $this; |
60 | 60 | } |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | */ |
87 | 87 | public function peek() |
88 | 88 | { |
89 | - if($this->top == 0) {return null;} |
|
89 | + if ($this->top == 0) {return null; } |
|
90 | 90 | return $this->items[$this->top-1]; |
91 | 91 | } |
92 | 92 | |
@@ -111,10 +111,10 @@ discard block |
||
111 | 111 | public function __toString() |
112 | 112 | { |
113 | 113 | $str = "Stack:\n"; |
114 | - $str .= ((string)$this->items[$this->top-1])." <-- top\n"; |
|
115 | - for($i = $this->top-2; $i >= 0; $i--) |
|
114 | + $str .= ((string)$this->items[$this->top-1]) . " <-- top\n"; |
|
115 | + for ($i = $this->top-2; $i >= 0; $i--) |
|
116 | 116 | { |
117 | - $str .= ((string)$this->items[$i])."\n"; |
|
117 | + $str .= ((string)$this->items[$i]) . "\n"; |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | return $str; |
@@ -29,8 +29,12 @@ discard block |
||
29 | 29 | */ |
30 | 30 | public function __construct($maxsize) |
31 | 31 | { |
32 | - if(gettype($maxsize) != "integer"){throw new UnexpectedType("Expected an integer, got: ".gettype($maxsize));} |
|
33 | - if($maxsize < 2) {throw new InvalidArgument("Stack has a minimum size of 2!");} |
|
32 | + if(gettype($maxsize) != "integer") |
|
33 | + { |
|
34 | +throw new UnexpectedType("Expected an integer, got: ".gettype($maxsize));} |
|
35 | + if($maxsize < 2) |
|
36 | + { |
|
37 | +throw new InvalidArgument("Stack has a minimum size of 2!");} |
|
34 | 38 | |
35 | 39 | $this->maxsize = $maxsize; |
36 | 40 | $this->items = array(); |
@@ -44,7 +48,9 @@ discard block |
||
44 | 48 | */ |
45 | 49 | public function push($item) |
46 | 50 | { |
47 | - if($this->top == $this->maxsize) {throw new StackOverflow("Pushing this item would overflow the stack (exceed the max stack size)!");} |
|
51 | + if($this->top == $this->maxsize) |
|
52 | + { |
|
53 | +throw new StackOverflow("Pushing this item would overflow the stack (exceed the max stack size)!");} |
|
48 | 54 | $this->items[$this->top++] = $item; |
49 | 55 | return $this; |
50 | 56 | } |
@@ -54,7 +60,9 @@ discard block |
||
54 | 60 | */ |
55 | 61 | public function pop() |
56 | 62 | { |
57 | - if($this->top == 0) {throw new StackUnderflow("Cannot pop an empty stack!");} |
|
63 | + if($this->top == 0) |
|
64 | + { |
|
65 | +throw new StackUnderflow("Cannot pop an empty stack!");} |
|
58 | 66 | unset($this->items[$this->top--]); |
59 | 67 | return $this; |
60 | 68 | } |
@@ -86,7 +94,9 @@ discard block |
||
86 | 94 | */ |
87 | 95 | public function peek() |
88 | 96 | { |
89 | - if($this->top == 0) {return null;} |
|
97 | + if($this->top == 0) |
|
98 | + { |
|
99 | +return null;} |
|
90 | 100 | return $this->items[$this->top-1]; |
91 | 101 | } |
92 | 102 |
@@ -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(){} |
|
15 | + public function __construct() {} |
|
16 | 16 | } |
17 | 17 | |
18 | 18 | class DoublyLinkedList implements Countable |
@@ -21,16 +21,16 @@ discard block |
||
21 | 21 | |
22 | 22 | private $size = 0; |
23 | 23 | |
24 | - public function __construct(){} |
|
24 | + public function __construct() {} |
|
25 | 25 | |
26 | 26 | public function insert_after($node, $newNode) |
27 | 27 | { |
28 | 28 | //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));} |
|
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)); } |
|
31 | 31 | |
32 | 32 | $newNode->prev = $node; |
33 | - if($node->next == null) |
|
33 | + if ($node->next == null) |
|
34 | 34 | { |
35 | 35 | $newNode->next = null; |
36 | 36 | $this->lastNode = $newNode; |
@@ -47,11 +47,11 @@ discard block |
||
47 | 47 | public function insert_before($node, $newNode) |
48 | 48 | { |
49 | 49 | //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));} |
|
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)); } |
|
52 | 52 | |
53 | 53 | $newNode->next = $node; |
54 | - if($node->prev == null) |
|
54 | + if ($node->prev == null) |
|
55 | 55 | { |
56 | 56 | $newNode->prev = null; |
57 | 57 | $this->firstNode = $newNode; |
@@ -68,9 +68,9 @@ discard block |
||
68 | 68 | public function insert_beginning($newNode) |
69 | 69 | { |
70 | 70 | //type checks |
71 | - if(get_class($newNode) != "DLNode"){throw new InvalidArgument("Expected a DLNode, got: ".get_class($newNode));} |
|
71 | + if (get_class($newNode) != "DLNode") {throw new InvalidArgument("Expected a DLNode, got: " . get_class($newNode)); } |
|
72 | 72 | |
73 | - if($this->firstNode == null) |
|
73 | + if ($this->firstNode == null) |
|
74 | 74 | { |
75 | 75 | $this->firstNode = $newNode; |
76 | 76 | $this->lastNode = $newNode; |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | |
87 | 87 | public function insert_end($newNode) |
88 | 88 | { |
89 | - if($this->lastNode == null) |
|
89 | + if ($this->lastNode == null) |
|
90 | 90 | { |
91 | 91 | $this->insert_beginning($newNode); |
92 | 92 | } |
@@ -100,13 +100,13 @@ discard block |
||
100 | 100 | public function contains($node) |
101 | 101 | { |
102 | 102 | //check this so we could possibly avoid list traversal |
103 | - if($node == $this->firstNode || $node == $this->lastNode) {return true;} |
|
103 | + if ($node == $this->firstNode || $node == $this->lastNode) {return true; } |
|
104 | 104 | |
105 | 105 | //traverse the list and try to find the node |
106 | 106 | $n = $this->firstNode->next; //we can start one node ahead as we already checked the firstNode |
107 | - while($n != null) |
|
107 | + while ($n != null) |
|
108 | 108 | { |
109 | - if($n == $node) {return true;} |
|
109 | + if ($n == $node) {return true; } |
|
110 | 110 | $n = $n->next; |
111 | 111 | } |
112 | 112 | |
@@ -116,9 +116,9 @@ discard block |
||
116 | 116 | public function remove($node) |
117 | 117 | { |
118 | 118 | //check if the node exists |
119 | - if($this->contains($node) == false){throw new InvalidArgument("The node given does not exist in this list!");} |
|
119 | + if ($this->contains($node) == false) {throw new InvalidArgument("The node given does not exist in this list!"); } |
|
120 | 120 | |
121 | - if($node->prev == null) |
|
121 | + if ($node->prev == null) |
|
122 | 122 | { |
123 | 123 | $this->firstNode = $node->next; |
124 | 124 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | $node->prev->next = $node->next; |
128 | 128 | } |
129 | 129 | |
130 | - if($node->next == null) |
|
130 | + if ($node->next == null) |
|
131 | 131 | { |
132 | 132 | $this->lastNode = $node->prev; |
133 | 133 | } |
@@ -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 | { |