Implement linked list in php

后端 未结 8 700
小蘑菇
小蘑菇 2020-12-08 05:22

How should I implement a linked list in PHP? Is there a implementation built in into PHP?

I need to do a lot of insert and delete operations, and at same time I need

8条回答
  •  情书的邮戳
    2020-12-08 05:44

    Here is the code in php which will implement Linked List, only with the reference of head node i.e first node and then you add at first, last and delete a key, and also maintain the code of the keys in list.

    data = $item;
            $this->next = null;
        }
    }
    
    /**
     * Class LinkList
     */
    class LinkList
    {
        public $head = null;
    
        private static $count = 0;
    
        /**
         * @return int
         */
        public function GetCount()
        {
            return self::$count;
        }
    
        /**
         * @param mixed $item
         */
        public function InsertAtFist($item) {
            if ($this->head == null) {
                $this->head = new Node($item);
            } else {
                $temp = new Node($item);
    
                $temp->next = $this->head;
    
                $this->head = $temp;
            }
    
            self::$count++;
        }
    
        /**
         * @param mixed $item
         */
        public function InsertAtLast($item) {
            if ($this->head == null) {
                $this->head = new Node($item);
            } else {
                /** @var Node $current */
                $current = $this->head;
                while ($current->next != null)
                {
                    $current = $current->next;
                }
    
                $current->next = new Node($item);
            }
    
            self::$count++;
        }
    
        /**
         * Delete the node which value matched with provided key
         * @param $key
         */
        public function Delete($key)
        {
            /** @var Node $current */
            $current = $previous = $this->head;
    
            while($current->data != $key) {
                $previous = $current;
                $current = $current->next;
            }
    
            // For the first node
            if ($current == $previous) {
                $this->head = $current->next;
            }
    
            $previous->next = $current->next;
    
            self::$count--;
        }
    
        /**
         * Print the link list as string like 1->3-> ...
         */
        public function PrintAsList()
        {
            $items = [];
            /** @var Node $current */
            $current = $this->head;
            while($current != null) {
                array_push($items, $current->data);
                $current = $current->next;
            }
    
            $str = '';
            foreach($items as $item)
            {
                $str .= $item . '->';
            }
    
            echo $str;
    
            echo PHP_EOL;
        }
    }
    
    $ll = new LinkList();
    
    $ll->InsertAtLast('KP');
    $ll->InsertAtLast(45);
    $ll->InsertAtFist(11);
    $ll->InsertAtLast('FE');
    $ll->InsertAtFist('LE');
    $ll->InsertAtFist(100);
    $ll->InsertAtFist(199);
    $ll->InsertAtLast(500);
    
    $ll->PrintAsList();
    echo 'Total elements ' . $ll->GetCount();
    echo PHP_EOL;
    $ll->Delete(45);
    $ll->PrintAsList();
    echo 'Total elements ' . $ll->GetCount();
    echo PHP_EOL;
    $ll->Delete(500);
    $ll->PrintAsList();
    echo 'Total elements ' . $ll->GetCount();
    echo PHP_EOL;
    $ll->Delete(100);
    $ll->PrintAsList();
    echo 'Total elements ' . $ll->GetCount();
    echo PHP_EOL;
    

    Code out put as:

    $ php LinkList.php
    199->100->LE->11->KP->45->FE->500->
    Total elements 8
    199->100->LE->11->KP->FE->500->
    Total elements 7
    199->100->LE->11->KP->FE->
    Total elements 6
    199->LE->11->KP->FE->
    Total elements 5
    

提交回复
热议问题