-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| #!/usr/bin/env python | ||
| from __future__ import unicode_literals | ||
|
|
||
|
|
||
| class Node(object): | ||
|
|
@@ -7,32 +8,33 @@ def __init__(self, data, nextNode=None): | |
| self.nextNode = nextNode | ||
|
|
||
| def __str__(self): | ||
| return self.data | ||
| return str(self.data) | ||
|
|
||
| def __repr__(self): | ||
| return self.data | ||
| return repr(self.data) | ||
|
|
||
|
|
||
| class LinkedList(object): | ||
| def __init__(self, firstNode=None): | ||
| self.firstNode = firstNode | ||
|
|
||
| def insert(self, newNode): | ||
| # insert newNode at beginning of list | ||
| def insert(self, val): | ||
| # insert val at beginning of list | ||
| self.newNode = Node(val) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will you ever use |
||
| if not self.firstNode: | ||
| self.firstNode = newNode | ||
| self.firstNode = self.newNode | ||
| else: | ||
| newNode.nextNode = self.firstNode | ||
| self.firstNode = newNode | ||
| self.newNode.nextNode = self.firstNode | ||
| self.firstNode = self.newNode | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are still 'asking permission rather than forgiveness' here. |
||
|
|
||
| def pop(self): | ||
| # pops first value from list and returns it | ||
| if self.size() == 0: | ||
| return "THE LIST! IT'S EMPTY!!" | ||
| raise ValueError("The list is empty") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much better. This is usable. Still though, you are asking permission. Don't ask if you can return a value, just try it and clean up when you're done if it fails. |
||
| else: | ||
| obsoleteNode = self.firstNode | ||
| self.firstNode = self.firstNode.nextNode | ||
| return obsoleteNode.data | ||
| return obsoleteNode.data.encode('utf-8') | ||
|
|
||
| def size(self): | ||
| # returns length of list | ||
|
|
@@ -46,6 +48,8 @@ def size(self): | |
| def search(self, val): | ||
| # return node containing 'val' in list, if present (else None) | ||
| currentNode = self.firstNode | ||
| if currentNode.data is None: | ||
| raise ValueError() | ||
| while currentNode.data != val: | ||
| if currentNode.nextNode is None: | ||
| return None | ||
|
|
@@ -56,15 +60,12 @@ def search(self, val): | |
| def remove(self, node): | ||
| # remove node from list, wherever it might be | ||
| if self.size() == 0: | ||
| return "THE LIST! IT'S EMPTY!!" | ||
| raise ValueError("The list is empty") | ||
| else: | ||
| prevNode = None | ||
| currentNode = self.firstNode | ||
| foundNode = False | ||
| while not foundNode: | ||
| if currentNode == node: | ||
| foundNode = True | ||
| elif currentNode is None: | ||
| while currentNode is not node: | ||
| if currentNode is None: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though this is much much cleaner, it's still a bit over-done. As you move forward through the other linked-list-related data structures, see if you can't find more efficient, more readable ways of accomplishing this task. |
||
| raise ValueError() | ||
| else: | ||
| prevNode = currentNode | ||
|
|
@@ -80,6 +81,6 @@ def display(self): | |
| display = "(" | ||
| currentNode = self.firstNode | ||
| while currentNode is not None: | ||
| display += currentNode.data + ", " | ||
| display += currentNode.data.encode('utf-8') + ", " | ||
| currentNode = currentNode.nextNode | ||
| return display + ")" | ||
This file was deleted.
This file was deleted.
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.
You might want to provide some way of differentiating a node that contains a value from the value itself. But this is a vast improvement over what you had before. Good. (same goes for
__repr__below)