} 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); // var_dump($newList2); // $newList->remove(1); // $newList->remove(3); // $newList->remove(5); $newList->insert(9, 'New inserted value'); $newList->printValues(); ?>
} 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();
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();