Repository navigation
NW| 26-Jul-SDC | Ahmad Hmedan | Sprint 2 | Implement a linked list in Python #227
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: main
Are you sure you want to change the base?
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| class LinkedList: | ||
| class clsNode: | ||
| def __init__(self, value): | ||
| self.next = None | ||
| self.previous = None | ||
| self.value = value | ||
|
Comment on lines
+3
to
+6
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. May I suggest exploring the use of |
||
|
|
||
| def __init__(self): | ||
| self.head = None | ||
| self.tail = None | ||
| self.size = 0 | ||
|
|
||
| def push_head(self, value): | ||
| new_node = self.clsNode(value=value) | ||
| if self.head == None: | ||
| self.head = new_node | ||
| self.tail = new_node | ||
| else: | ||
| new_node.next = self.head | ||
| self.head.previous = new_node | ||
| self.head = new_node | ||
| self.size += 1 | ||
| return new_node | ||
|
|
||
| def pop_tail(self): | ||
| if self.tail == None: | ||
| return | ||
| removed_value = self.tail.value | ||
|
|
||
| if self.head == self.tail: | ||
| self.head = None | ||
| self.tail = None | ||
| self.size -= 1 | ||
| return removed_value | ||
| self.tail = self.tail.previous | ||
| self.tail.next = None | ||
| self.size -= 1 | ||
|
Comment on lines
+30
to
+37
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.
|
||
| return removed_value | ||
|
|
||
| def remove(self, node): | ||
| if node == None: | ||
| return | ||
| if node == self.head and node == self.tail: | ||
| self.head = None | ||
| self.tail = None | ||
|
|
||
| elif node == self.head: | ||
| self.head = self.head.next | ||
| self.head.previous = None | ||
|
|
||
| elif node == self.tail: | ||
| self.tail = self.tail.previous | ||
| self.tail.next = None | ||
|
|
||
| else: | ||
| node.next.previous = node.previous | ||
| node.previous.next = node.next | ||
| node.next = None | ||
| node.previous = None | ||
| self.size -= 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.
Suggestion: Look up "Python naming convention for inner class"?