-
-
Notifications
You must be signed in to change notification settings - Fork 29
Glasgow | 25 SDC NOV | Katarzyna Kazimierczuk | Sprint 2 | linked list #117
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 5 commits
05c5b15
9db693f
1e30d3d
202c582
a628d8a
62f8bf3
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 |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ | |
| node_modules | ||
| .venv | ||
| __pycache__ | ||
| package-lock.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # //operations | ||
| # `push_head` should add an element to the start of the list. | ||
| # It should return something that can be passed to `remove` to | ||
| # remove that element in the future. | ||
|
|
||
|
|
||
|
|
||
| # `pop_tail` should remove an element from the end of the list. | ||
| # * `remove` takes a handle from `push_head`, | ||
| # and removes that element from the list. | ||
| class Node: | ||
| def __init__(self, tracker, inserted_item_key): | ||
| self.tracker = tracker | ||
| self.inserted_item_key = inserted_item_key | ||
|
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. Typically a linked list node store some data in addition to the next and previous references.
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. Thank you, I implemented all the requested changes. Since it now returns the node obj, the tracker is unnecessary and I deleted it. I didn't realize there was a standard naming convention and have renamed it. I understand adding None is good practice because it removes the node's references to the active list. |
||
| self.next = None | ||
| self.prev = None | ||
|
|
||
| class LinkedList: | ||
| def __init__(self): | ||
| self.head = None | ||
| self.tail=None | ||
| self.tracker_number = 0 | ||
|
|
||
| def push_head(self, item_to_insert): | ||
| # //wrap item to insert in {} | ||
| wrapped_item = Node(None, item_to_insert) | ||
|
|
||
| if not self.head: | ||
| self.head = self.tail = wrapped_item | ||
| else: | ||
| wrapped_item.next = self.head | ||
| self.head.prev = wrapped_item | ||
| self.head = wrapped_item | ||
|
|
||
| # self.our_list.insert(0, wrapped_item) | ||
| self.tracker_number +=1 | ||
| return wrapped_item | ||
|
|
||
| def remove(self, id_for_this_particular_item): | ||
|
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.
|
||
| node_to_remove = id_for_this_particular_item | ||
|
|
||
| if node_to_remove.prev: | ||
| node_to_remove.prev.next = node_to_remove.next | ||
| else: | ||
| self.head = node_to_remove.next | ||
|
|
||
| if node_to_remove.next: | ||
| node_to_remove.next.prev = node_to_remove.prev | ||
| else: | ||
| self.tail = node_to_remove.prev | ||
|
|
||
| return node_to_remove.inserted_item_key | ||
|
|
||
| def pop_tail(self): | ||
| if not self.tail: | ||
| return None | ||
| return self.remove(self.tail) | ||
Uh oh!
There was an error while loading. Please reload this page.