} public function add($val) { //return true if added correctly //check to see if there is not a head because than we can create the head node if ($this->head == null) { //point head to that new node $this->head = new Node($val); } else { //we have a head lets navigate through the Linked List till we get to the end //when we reach the end we can add the new node there //temp to help us navigate the linked list $current = $this->head; //while loops are extremely skilled at looping through a linked list while ($current->next) { //move our pointer forward to the next node $current = $current->next; } //we reached the end! time to add the new node to the end $current->next = new Node($val); } } } //now we can execute our new function to add nodes to a linked list! $newList2 = new SinglyLinkedList(); $newList2->add(1); $newList2->add(2); $newList2->add(3); $newList2->add(4); $newList2->add(5); var_dump($newList2);
return null; } while ($current->next != null) { echo $current->val . "<br>"; $current = $current->next; } echo $current->val; } public function find($value) { if ($current == null) { echo "empty"; return null; } //return node if value is found //return false if not found } public function isEmpty() { //return true if the linked list is empty //return false if linked list has nodes } } $list = new SinglyLinkedList(); $list->add('Steven'); $list->add('Bobby'); $list->add('Mr Poopybutthole'); var_dump($list); ?>
if (Node($val) == null) { return true; } } } // create new Linked List Object $newList = new SinglyLinkedList(); // set the linked list head to a new node $newList->head = new Node(1); // head node now add the next node $newList->head->next = new Node(2); // continue adding nodes to end of Linked List $newList->head->next->next = new Node(3); $newList->head->head->next->next = new Node(4); $newList->head->next->next->next->next = new Node(5); $newList2 = new SinglyLinkedList(); $newList2->add(1); $newList2->add(2); $newList2->add(3); $newList2->add(4); $newList2->add(5); $newList->head->next->next->next->next = null; $temp = $newList->head->next->next->next; $newList->head->next = $temp; $newList->remove(1); $newList->remove(3); $newList->remove(5); var_dump($newList->find(3)); var_dump($newList->find(99)); $newList->add(1); $newList->add(2);
} public function isEmpty() { if ($this->head == null) { return true; } return false; } public function removeDups() { //what are dups? any duplicates } } $newList = new SinglyLinkedList(); var_dump($newList->isempty()); $newList->head = new Node(1); $newList->head->next = new Node(2); $newList->head->next->next = new Node(3); $newList->head->next->next->next = new Node(4); $newList->head->next->next->next->next = new Node(5); var_dump($newList); $newList2 = new SinglyLinkedList(); $newList2->addHead(1); $newList2->addHead(2); $newList2->addHead(3); $newList2->addHead(4); $newList2->addHead(5); $newList2->insert(3, "A"); var_dump($newList2); var_dump($newList2->find(2)); $newList2->printValues();
} else { return false; } } public function removeDups($val) { //return true if all values were removed if ($this->head->value == null && $this->head->next == null) { return true; } else { return false; } } } // create new linked List Object $newList = new SinglyLinkedList(); //set the Linked list head to a new node $newList->head = new Node(1); $newList->head->next = new Node(2); $newList->head->next->next = new Node(3); $newList->head->next->next->next = new Node(4); $newList->head->next->next->next->next = new Node(5); $newList->head->next->next->next->next->next = new Node(6); // var_dump($newList); // //now we can execute our new function to add nodes to a linked list! // $newList2 = new SinglyLinkedList(); // $newList2->add(1); // $newList2->add(2); // $newList2->add(3); // $newList2->add(4); // $newList2->add(5);
if ($current->next == null) { return false; } else { $current = $current->next; } } return $current->value; //return node if value is found //return false if not found } public function isEmpty() { if ($this->count = 0) { return true; } else { return false; } //return true if the linked list is empty //return false if linked list has nodes } } $list = new SinglyLinkedList(); $list->add("John"); $list->add("Todd"); $list->add("Tim"); $list->add("Pat"); $list->add("Joey"); $list->add("Lewis"); $list->add("Michael"); $list->insert("Todd", "Elena"); $list->printList();
} //set previous so we can find it $previous = $current; //iterate $current = $current->next; } return true; } //if we get here array was empty IE no values to remove return false; } } class Node { function __construct($val) { $this->value = $val; $this->next = null; } } $newList = new SinglyLinkedList(); $newList->push(1); $newList->push(2); $newList->push(3); $newList->push(1); $newList->push(4); $newList->push(5); $newList->printValues(); echo "<h2> test </h2>"; $newList->removeDups(1); $newList->printValues();