Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1a28c64

Browse files
committed
Even more readable PHP for novices (I hope)!
1 parent f27620c commit 1a28c64

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

‎singly_linked_list/singly_linked_list_object_oriented.php‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* The singly linked list itself is the logical container for all the
2626
* list's elements. The list needs to know which element is the first
27-
* element. It does *not* need to * know the order of the list items
27+
* element. It does *not* need to know the order of the list items
2828
* themselves, because each element is responsible for knowing which
2929
* one comes after it.
3030
*/
@@ -52,25 +52,26 @@ class Singly_Linked_List {
5252
*/
5353
public function appendElement ($value = null) {
5454
// Find the last element in the list.
55-
$last_element = $this->end();
55+
$last_element = $this->getLastElement();
5656

57-
// If there are no elements in the list, the "last element"
58-
// will actually be a `null` value.
59-
if (null === $last_element) {
60-
// In that case, we make a new element,
61-
// and assign that element to the beginning of the list.
62-
$this->first = new Singly_Linked_List_Element($value);
63-
} else {
64-
// If there are already elements in the list, we set the
65-
// new element to its previous element's `$next` member.
57+
if ($last_element instanceof Singly_Linked_List_Element) {
58+
// If there are already elements in the list, we add the
59+
// new element to the end of the list by making the last
60+
// element's `$next` variable point to the new element.
6661
$last_element->next = new Singly_Linked_List_Element($value);
62+
} else {
63+
// Otherwise we make a new element to be the beginning of
64+
// the list.
65+
$this->first = new Singly_Linked_List_Element($value);
6766
}
6867
}
6968

7069
/**
71-
* Seek to the end of the list.
70+
* Find the element at the end of the list.
71+
*
72+
* @return null|Singly_Linked_List_Element
7273
*/
73-
public function end () {
74+
public function getLastElement () {
7475
// Start at the beginning.
7576
$current_element = $this->first;
7677

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /