in reply to Re^3: Perl linked list
in thread Perl linked list

Thanks Laurent, beginner lever is good for me. I actually worked in biology department, and learned Perl just for analyzing data generated from our Next generation sequencing project. I guess linked list will not be something I will use in my data analysis in near future since it seems quite complex to me..... Thanks a lot for your code,though!

Replies are listed 'Best First'.
Re^5: Perl linked list
by Laurent_R (Canon) on Mar 31, 2015 at 20:14 UTC
    Hmm, linked lists are not that complicated, but it is a data structure that you almost never need in Perl (I don't think that I ever implemented one in Perl for actual work problem), because Perl's arrays and hashes are very versatile and provide easier solutions for most of the same types of problems (circular buffer, for example).

    If you look at the code I posted, it is really not very complicated.

    my $pt = { value => "D", next => undef };
    I am starting with the terminator, the last element of the linked list. Therefore, the next field is undefined and the value is "D". The $pt variable (for "pointer", but I should not use that terminology, it is a reference, not a pointer) is something that gives me access to the data structure. This reference will be used in the next code line:
    my $pt2 = {value => "C", next => $pt};
    There, I am saying that the value is "C", and that the next record is the one I just created before. And so on until the first element. Please feel free to ask if you don't understand.

    In lower level languages such as C or C++, linked lists are needed much more often and they are also often more difficult to implement.

    Je suis Charlie.