| [reply] [d/l] |
Great to hear this!
I don't code in PHP, but this looks like it has the basics of a linked list and demonstrates
understanding of the concept. It is a bit bizzare because inserts are positon based. Normally
when tranversing a linked list, you are searching for something like a hash key in the case
of a Perl bucket's list (search through entries that hash to the same hash value's index, looking for
a match, or not of a particular key). Counting through the list to go to say the 5th thing is weird. That
traversal of the list just to count them is inefficient.
As far as Perl goes, there is no need for something like this because you would just use a Perl array.
The Perl array can contain simple scalars or references to objects or whatever. One tricky bit is
inserting or deleting something in the middle of the array, but Perl handles all the details via splice(). And
of course also implements: push, pop, shift, unshift. So Perl array implements the functions of
the SplDoublyLinkedList class and handles all the messy memory allocation and reference stuff for you.
A C implementation of what this PHP code does (essentially an indexed array of pointers) would probably not be done via a linked list, but
but rather a simple linear array of pointer to objects. For the head, instead of a single pointer to pointer to array, you would need
a struct to describe: start of memory array allocation, end of memory array allocation, pointer to first sequential item within allocated
memory and pointer to last sequential item within allocated array memory. Basically a block of allocated memory for the pointer
array and a sequential block within that block that is actually "used" at the moment.
To shift an element off of the top, you just adjust the pointer of the first item within allocated
memory. To delete something from the bottom, you just adjust the pointer of the last item. Adding something to the
top or bottom is also easy as long as there is enough "slack space" (unused "entries" within the allocated block of
memory).
Adding/deleting something in the middle is more complicated. To delete the 2nd item, you have to move the pointers to the 3rd-nth
items "up" in the array. However moving blocks of memory is something that the processor can do very efficiently.
The normal C function that does this is very fast and at maximum optimization levels or via an ASM subroutine, this
is actually a single machine instruction! Growing the array would require memory allocation of new memory and copying
the existing stuff to that new bigger array.
Anyway, there is usually no need to implement a linked list like this PHP example in Perl because the Perl array basically does that.
There probably is some outlier case, but normally that is not the situation.
| [reply] |
Thank you very much for your kind reply.
The kid performed once more. And as you might have noticed already my C skills aren’t so good. I think the code he provided isn’t really good. I fear he simply cheated it from somewhere out there. If you still have the patience take a look…
Best regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [d/l] |
«…I see black…»
MeToo. I’ll report what happened. Best regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] |