loading...

re: Singly Linked List: A Python Implementation VIEW POST

FULL DISCUSSION
 

Not clear where you will go from here. You have defined a Link, but you have not defined a List, and it is in the List that all the real behaviors would reside -- or so it seems to me. Insert(link), Append(link), Delete(Link), Exists(Value)->Link/None, all these are behaviors of a List, not of a Link.

You might think, well, a "list" could be any reference to a Link, so the Link class could offer insert, append, delete, exists methods, and they simply mean, do that action to the list that begins with this link.

One problem with that is, it is not possible to have an empty list, i.e. a list with no links in it. You might rule that an empty list is just a reference to None, but in that case, an attempt to call a method like insert (to add the first link to an empty list) would produce the dreaded "NoneType has no method named insert()".

But also, if the Link is the only class, doing all the work of being a list, that's error prone, because it is easy to treat any old reference to a link as if it were the head of a list -- but is it? If you use a link that exists half-way along a list, as the head of a list, things will go wrong. So at the least, you would need to distinguish a special list-head-link class that can only be the head link in a list -- so you can test a link and ask it, "are you a head or just some random link?" But if you do that, what happens to the list if you delete the first link? How does the previously-second link graduate to head link? (And again, what's an empty list?)

 

Thanks for the comment!
These are all good points. I was in fact planning on adding a List class, and may do that in the next post in the series.
Although, it's an interesting thought exercise to imagine implementing a linked list with only a Node class. For the reasons you've stated, I'm not sure if it's even possible, but it is interesting to think about.

 

If you would just leave the node class like that, it would function like a type of graph where the connections are in one direction and no more is aware of the nodes that point to it.

Thank for you the comment!
That's an interesting observation about the graph. But there's one thing about that I don't understand. Wouldn't the code still need some mechanism to keep track of the first node? It seem to me that, without a pointer to the first node, that node would get lost and none of the other nodes could be accessed.
From what I've learned, it seems sensible to make that pointer to the first node be part of another class, perhaps a class called Graph. Is there another approach I could consider?

In this case, whatever the calling code was would need to save the head pointer in the calling function and pass it around. This isn't exactly an antipattern as we do need to pass around both a string and its length in some languages like C, but it obviously isn't preferable.

Thanks for explaining that for me.

code of conduct - report abuse