Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules
.venv
__pycache__
package-lock.json
57 changes: 57 additions & 0 deletions Sprint-2/implement_linked_list/linked_list.py
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
Comment thread
katarzynakaz marked this conversation as resolved.
Outdated
self.inserted_item_key = inserted_item_key
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.
You could name the property data or value if you needed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • for_this_particular does not carry much useful info.

  • Why not just name the parameter node_to_remove?

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)