Conversation
| self.length = 1 | ||
| self.head_node = None | ||
| self.length = 0 | ||
| try: |
There was a problem hiding this comment.
Very good! You probably want to go ahead and raise an exception instead of just printing a message. Raising an exception is the best way to let users know that they done fucked up.
src/linked_list.py
Outdated
| def pop(self): | ||
| """Remove and return the current head node.""" | ||
| if not self.head_node: | ||
| print("Linked list is already empty") |
There was a problem hiding this comment.
Again, rather than just print a message, this is where you should raise an exception too let the user know that something has gone wrong. Remember, this could be in a scenario where thousands of Linked Lists are being used in the depths of a very complex program. The designer of that program probably won't see the printed message, so an exception is the correct way to communicate. And this exception can be handled in a way that makes sense to the user's program needs.
There was a problem hiding this comment.
See what exception the basic Python list raises when you try to pop an empty list, and use that.
| return | ||
| old_head_node = self.head_node | ||
| self.head_node = self.head_node.next_node | ||
| self.length -= 1 |
There was a problem hiding this comment.
Pop should return the value/contents, not the node itself!
src/linked_list.py
Outdated
| def pop(self): | ||
| """Remove and return the current head node.""" | ||
| if not self.head_node: | ||
| raise AttributeError("List is empty") |
There was a problem hiding this comment.
Good to raise an error here! However, this is not the appropriate type of error.
An AttributeError indicates an attribute doesn't exist; however, this is not relevant to your user, because it concerns the internal logic.
Find out what error is raised when you pop an empty regular built in Python list, and use that instead.
src/linked_list.py
Outdated
| while current_node.next_node is not None: | ||
| current_node = current_node.next_node | ||
| new_list.append(current_node.contents) | ||
| return tuple(new_list) |
There was a problem hiding this comment.
This is really good and really close; however the assignment specs say:
display() will return a unicode string representing the list as if it were a Python tuple literal: “(12, ‘sam’, 37, ‘tango’)”
| return None | ||
|
|
||
| def remove(self, remove_node): | ||
| """Remove a node from linked list.""" |
There was a problem hiding this comment.
remove method is still throwing errors when trying to remove from an empty LinkedList.
Updates to Linked list, including ability to have an empty linked list and testing more edge cases.