-
-
Notifications
You must be signed in to change notification settings - Fork 29
Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 2 | Implement a linked list in Python #125
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 1 commit
e862c3b
dc94ae0
5100b36
addda43
301d10d
56786ff
7a292df
28029c0
8c606a2
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,64 @@ | ||
| class Node: | ||
| def __init__(self, data): | ||
| self.data = data | ||
| self.previous = None | ||
| self.next = None | ||
|
|
||
| class LinkedList: | ||
| def __init__(self): | ||
| self.head = None | ||
| self.tail = None | ||
|
|
||
| def push_head(self, data): | ||
| new_head_node = Node(data) | ||
| if self.head is not None: | ||
| new_head_node.next = self.head | ||
| self.head.previous = new_head_node | ||
|
|
||
| self.head = new_head_node | ||
| if self.tail is None: | ||
| self.tail = new_head_node | ||
|
|
||
| return new_head_node | ||
|
|
||
|
|
||
|
|
||
| def pop_tail(self): | ||
| if self.tail is not None: | ||
| tail_node = self.tail | ||
| previous = self.tail.previous | ||
| self.tail = previous | ||
| if self.tail is not None: | ||
| self.tail.next = None | ||
| else: | ||
| self.head = None | ||
| else: | ||
| raise IndexError("Unable to remove from empty linked list") | ||
|
|
||
| return tail_node.data | ||
|
Comment on lines
+30
to
+32
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.
Author
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. yeah line 40 is definitely useless since a tail node's next should already be None. Not so intelligent of me!
Author
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. Regarding "You could replace the code on lines 30-42 by 3 lines of code if you delegate the node-removing task to remove()", I just updated my logic inside pop_tail function accordingly. |
||
|
|
||
|
|
||
|
|
||
| def remove(self, node): | ||
| if node.previous is not None: | ||
| node.previous.next = node.next | ||
| else: | ||
| self.head = node.next | ||
|
|
||
| if node.next is not None: | ||
| node.next.previous = node.previous | ||
| else: | ||
| self.tail = node.previous | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.