-
Notifications
You must be signed in to change notification settings - Fork 0
Linked list #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Linked list #3
Conversation
| self.length = 1 | ||
| self.head_node = None | ||
| self.length = 0 | ||
| try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.