Linked List implementation in PHP

You may also like...

16 Responses

  1. John says:

    I prefer to use “classic php arrays” for things like this…

  2. Nice, clean, and complete set of functions, and with Unit tests!

    I wrote my own implementation and found this a perfect reference to double check everything against.

  3. ramdani says:

    Do you have example of double linklist non circular for php

  4. sameer says:

    Not at present. But I’ll surely add it in coming days.

  5. My name says:

    Why is the count static?
    IMHO this is an attribute that belongs to an instance and not the whole class! Otherwise, you would count all nodes in all lists you have currently instantiated!

  6. sameer says:

    And you are absolutely right. Actually the above code was from a Singleton library, and hence the static modifier. You can see that my other post

    http://www.codediesel.com/algorithms/doubly-linked-list-in-php/

    does away with the static modifier for the ‘count’ variable. But I’ll make the necessary changes on this one too.

  7. usman says:

    hi. i learn linklist in c++ for solve the memory allocation problem.
    but why you use this in php….
    cnfuzd…

  8. sameer says:

    Memory allocation is one small application of a linked-list. Linked lists are general data structures which can be used to define other specific data structures like hash tables, stacks, queues etc.

  9. Miha says:

    I’m testing with windows 7 and wamp php 5.3.0
    $list = new LinkList();
    echo memory_get_usage(1)/1024 . “kB\n”;
    for($i = 0; $i small 50000; $i++) {
    $list->insertLast(10);
    }
    echo memory_get_usage(1) . “B\n”;

    This causes php cli seg. fault, i also tried on linux also seg fault,
    so there is some kind of limit if I try this it works:

    $array = array();
    for($i = 0; $i small 200000; $i++) {
    $array[] = new LinkedListNode(10);
    }

    Where/what is the limit?

  10. sameer says:

    Hello Miha,
    The code works fine at my end on both Windows and Linux. The most probable reason for the Segmentation Fault could be one of the PHP extensions. Try running it without the memory_get_usage(1).

  11. Miha says:

    http://pastie.org/840202

    I tried but still segfault, when I get to 29489 it craches.

  12. Ben says:

    You have a lot of mistakes in this code, mostly related to your understanding of variables and objects in PHP.

    You use &$link. In PHP5+ there is no reason to use references and its in fact considered bad practice. $link is a variable that points to the object(like a reference, but not a reference)

    An example of an error as a result of variable assignment can be seen in the deleteNode method.

    if($this->count == 1)
    {
    $this->lastNode = $this->firstNode;
    }
    $this->firstNode = $this->firstNode->next;

    Your last node points to your “original” first node and your $this->firstNode points to NULL. This in the long term could create a bug in which memory that should be released was not as you still have a variable pointing to the object

    Best Regards
    Ben

  13. Buck says:

    When you are assigning the $this->firstNode value to $link->next in the insertFirst() function, what will be the value of $this->firstNode there ?

  14. Mas Ripan says:

    Building Converted Indexs:

    Problem we have :

    -how to combine Linked List with BST..
    -we have 2 dokument text (file .txt)

    ex. Doc 1 contain : car, laptop, mobile, glass
    Doc 2 contain : train, sky globe, phone

    and then we sort both of this dokument with BST and then puts

    illusstration for case like picture below..
    http://sphotos-e.ak.fbcdn.net/hphotos-ak-snc6/165402_3961720941285_1372241414_n.jpg

  15. Jovin says:

    Good example of link list in PHP. Remind me about data structure lesson in Pascal. Thanks for sharing.

  16. Ajay says:

    Thanks, Nice content and very understandable..

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">