I want to buy the linked list structure for PHP. It should provide support to circular…
- User
I want to buy the linked list structure for PHP. It should provide support to circular linked list and have the functions to append a new node, remove a node, sort the list and export it to an array.
Assistant
I'll create a PHP linked list implementation with circular linked list support and all the requested functions. Here's a complete solution:
<?php class Node { public $data; public $next; public function __construct($data) { $this->data = $data; $this->next = null; } } class LinkedList { private $head; private $isCircular; private $size; public function __construct($isCircular = false) { $this->head = null; $this->isCircular = $isCircular; $this->size = 0; } /** * Append a new node to the end of the list */ public function append($data) { $newNode = new Node($data); if ($this->head === null) { $this->head = $newNode; if ($this->isCircular) { $newNode->next = $this->head; } } else { $current = $this->head; // Find the last node while ($current->next !== null && $current->next !== $this->head) { $current = $current->next; } $current->next = $newNode; if ($this->isCircular) { $newNode->next = $this->head; } } $this->size++; return true; } /** * Remove a node with specific data */ public function remove($data) { if ($this->head === null) { return false; } // If head needs to be removed if ($this->head->data === $data) { if ($this->size === 1) { $this->head = null; } else { if ($this->isCircular) { // Find the last node to update its next pointer $current = $this->head; while ($current->next !== $this->head) { $current = $current->next; } $current->next = $this->head->next; } $this->head = $this->head->next; } $this->size--; return true; } // Search for the node to remove $current = $this->head; $previous = null; do { if ($current->data === $data) { $previous->next = $current->next; $this->size--; return true; } $previous = $current; $current = $current->next; } while ($current !== null && $current !== $this->head); return false; } /** * Sort the linked list in ascending order */ public function sort() { if ($this->head === null || $this->size <= 1) { return; } // Convert to array for easier sorting $array = $this->toArray(); sort($array); // Rebuild the list with sorted data $this->clear(); foreach ($array as $value) { $this->append($value); } } /** * Export the linked list to an array */ public function toArray() { $result = []; if ($this->head === null) { return $result; } $current = $this->head; $visited = []; do { // Prevent infinite loop in circular list if (in_array(spl_object_id($current), $visited)) { break; } $result[] = $current->data; $visited[] = spl_object_id($current); $current = $current->next; } while ($current !== null && $current !== $this->head); return $result; } /** * Clear the entire list */ public function clear() { $this->head = null; $this->size = 0; } /** * Get the size of the list */ public function getSize() { return $this->size; } /** * Check if the list is empty */ public function isEmpty() { return $this->head === null; } /** * Check if the list is circular */ public function isCircular() { return $this->isCircular; } /** * Display the list (for debugging) */ public function display() { if ($this->head === null) { echo "List is empty\n"; return; } $current = $this->head; $output = ""; do { $output .= $current->data . " -> "; $current = $current->next; } while ($current !== null && $current !== $this->head); if ($this->isCircular) { $output .= "(back to head)"; } else { $output .= "NULL"; } echo $output . "\n"; } } // Example usage: // Regular linked list $list = new LinkedList(); $list->append(5); $list->append(3); $list->append(8); $list->append(1); echo "Original list: "; $list->display(); $list->sort(); echo "Sorted list: "; $list->display(); echo "Array representation: "; print_r($list->toArray()); $list->remove(3); echo "After removing 3: "; $list->display(); // Circular linked list echo "\n--- Circular Linked List ---\n"; $circularList = new LinkedList(true); $circularList->append(10); $circularList->append(20); $circularList->append(30); echo "Circular list: "; $circularList->display(); echo "Array representation: "; print_r($circularList->toArray());This implementation provides:
- Node class: Represents individual nodes with data and next pointer
- LinkedList class: Main class with circular list support
- append($data): Adds a new node to the end of the list
- remove($data): Removes the first node with matching data
- sort(): Sorts the list in ascending order
- toArray(): Exports the list to a PHP array
- Additional utility methods: clear(), getSize(), isEmpty(), display()
Key features:
- Handles both regular and circular linked lists (specified in constructor)
- Prevents infinite loops when working with circular lists
- Maintains proper linking when adding/removing nodes
- Efficient sorting by converting to array and rebuilding
The implementation is robust and handles edge cases like empty lists, single-node lists, and circular list traversal. You can extend it further by adding methods like prepend(), insertAt(), reverse(), or find() based on your needs.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.