Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^7: [OT:] Is this Curriculum right?

by Marshall (Canon)
on Nov 27, 2021 at 00:16 UTC ( [id://11139156]=note: print w/replies, xml ) Need Help??


in reply to Re^6: [OT:] Is this Curriculum right?
in thread [OT:] Is this Curriculum right?

Let's see what happens with the code that I sent you.
BTW, here in the US we had a talking horse on a popular TV show called, "Mr. Ed" and he was a pretty smart horse.
If your boy's instructor cannot understand my code, then as you perhaps would say, "I see black", Ich sehe schwarz.

Replies are listed 'Best First'.
Re^8: [OT:] Is this Curriculum right?
by karlgoethebier (Abbot) on Dec 06, 2021 at 17:33 UTC
      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.

        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»

Re^8: [OT:] Is this Curriculum right?
by karlgoethebier (Abbot) on Nov 29, 2021 at 14:59 UTC
    «…I see black…»

    MeToo. I’ll report what happened. Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139156]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 05:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found