Skip to content

Linked list#3

Open
julienawilson wants to merge 11 commits intomasterfrom
linked_list
Open

Linked list#3
julienawilson wants to merge 11 commits intomasterfrom
linked_list

Conversation

@julienawilson
Copy link
Collaborator

Updates to Linked list, including ability to have an empty linked list and testing more edge cases.

self.length = 1
self.head_node = None
self.length = 0
try:

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.

def pop(self):
"""Remove and return the current head node."""
if not self.head_node:
print("Linked list is already empty")

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.

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

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!

def pop(self):
"""Remove and return the current head node."""
if not self.head_node:
raise AttributeError("List is empty")

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.

while current_node.next_node is not None:
current_node = current_node.next_node
new_list.append(current_node.contents)
return tuple(new_list)

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."""

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants