Data structure code challenge questions along with their solutions in PHP:
- Linked List Implementation
Question: Implement a singly linked list in PHP with methods for insertion at the beginning, insertion at the end, deletion from the beginning, deletion from the end, and traversal.
class Node { public $data; public $next; public function __construct($data) { $this->data = $data; $this->next = null; } } class LinkedList { private $head; public function __construct() { $this->head = null; } public function insertBeginning($data) { $newNode = new Node($data); $newNode->next = $this->head; $this->head = $newNode; } public function insertEnd($data) { $newNode = new Node($data); if ($this->head === null) { $this->head = $newNode; } else { $current = $this->head; while ($current->next !== null) { $current = $current->next; } $current->next = $newNode; } } public function deleteBeginning() { if ($this->head !== null) { $temp = $this->head; $this->head = $this->head->next; $temp = null; } } public function deleteEnd() { if ($this->head === null) { return; } if ($this->head->next === null) { $this->head = null; return; } $current = $this->head; while ($current->next->next !== null) { $current = $current->next; } $current->next = null; } public function display() { $current = $this->head; while ($current !== null) { echo $current->data . " "; $current = $current->next; } } } $list = new LinkedList(); $list->insertBeginning(3); $list->insertBeginning(2); $list->insertBeginning(1); $list->insertEnd(4); $list->insertEnd(5); $list->deleteBeginning(); $list->deleteEnd(); $list->display(); // Output: 2 3 4
- Stack Implementation
Question: Implement a stack using an array in PHP with functions for push, pop, and peek operations.
class Stack { private $stack; public function __construct() { $this->stack = []; } public function push($data) { array_push($this->stack, $data); } public function pop() { if ($this->isEmpty()) { return null; } return array_pop($this->stack); } public function peek() { if ($this->isEmpty()) { return null; } return end($this->stack); } public function isEmpty() { return empty($this->stack); } } $stack = new Stack(); $stack->push(1); $stack->push(2); $stack->push(3); echo $stack->pop(); // Output: 3 echo $stack->peek(); // Output: 2
- Queue Implementation
Question: Implement a queue using an array in PHP with functions for enqueue, dequeue, and peek operations.
class Queue { private $queue; public function __construct() { $this->queue = []; } public function enqueue($data) { array_push($this->queue, $data); } public function dequeue() { if ($this->isEmpty()) { return null; } return array_shift($this->queue); } public function peek() { if ($this->isEmpty()) { return null; } return $this->queue[0]; } public function isEmpty() { return empty($this->queue); } } $queue = new Queue(); $queue->enqueue(1); $queue->enqueue(2); $queue->enqueue(3); echo $queue->dequeue(); // Output: 1 echo $queue->peek(); // Output: 2
These examples cover some basic data structure implementations in PHP. You can expand upon them or create additional challenges based on these concepts. Let me know if you need more examples or have any other questions!
Comments
Post a Comment