Skip to content

Add a __len__() function to doubly_linked_list.py #2042

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

Closed
wants to merge 25 commits into from
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1cd0e54
Update __init__.py
Lakshmikanth2001 Apr 15, 2020
21d7333
Update __init__.py
Lakshmikanth2001 Apr 16, 2020
dc0c694
Update doubly_linked_list.py
Lakshmikanth2001 Apr 16, 2020
6938c58
Merge pull request #2 from Lakshmikanth2001/Lakshmikanth2001-patch-2
Lakshmikanth2001 Apr 22, 2020
5300746
Merge pull request #1 from Lakshmikanth2001/Lakshmikanth2001-patch-1
Lakshmikanth2001 May 28, 2020
6a19daf
prime number _better method
Lakshmikanth2001 May 28, 2020
bcafc11
comments
Lakshmikanth2001 May 28, 2020
f110e49
Updated init.py 2
Lakshmikanth2001 May 30, 2020
bc75424
updated length function
Lakshmikanth2001 May 30, 2020
ffb58d2
Add doctests to LinkedList().__len__()
cclauss May 31, 2020
df7a07c
Add doctests to LinkedList().__len__()
cclauss May 31, 2020
da43761
Revert changes to prime_check.py
cclauss May 31, 2020
6d549a3
Revert changes to prime_check.py
cclauss May 31, 2020
22cc5d2
Merge branch 'master' into master
cclauss May 31, 2020
911dde9
Update doubly_linked_list.py
cclauss May 31, 2020
639d77b
Update doubly_linked_list.py
cclauss May 31, 2020
0587999
Update doubly_linked_list.py
cclauss May 31, 2020
81b3a68
Update doubly_linked_list.py
cclauss May 31, 2020
dbb57e9
Update doubly_linked_list.py
cclauss May 31, 2020
1d6b37e
Update doubly_linked_list.py
cclauss May 31, 2020
b1967c5
fixup! Format Python code with psf/black push
May 31, 2020
8285c08
Update doubly_linked_list.py
cclauss May 31, 2020
dc86f40
fixup! Format Python code with psf/black push
May 31, 2020
54e661b
self.is_empty()
cclauss May 31, 2020
93b0652
Revert all changes to __init__.py
cclauss Jun 1, 2020
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
85 changes: 54 additions & 31 deletions data_structures/linked_list/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,60 @@
- Each link references the next link and the previous one.
- A Doubly Linked List (DLL) contains an extra pointer, typically called previous
pointer, together with next pointer and data which are there in singly linked list.
- Advantages over SLL - IT can be traversed in both forward and backward direction.,
Delete operation is more efficient"""
- Advantages over SLL - IT can be traversed in both forward and backward direction.
Delete operation is more efficient.
"""


class Link:
next = None # This points to the link in front of the new link
previous = None # This points to the link behind the new link

def __init__(self, x):
self.value = x

def displayLink(self):
print(self.value, end=" ")


class LinkedList: # making main class named linked list
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0

def insertHead(self, x):
newLink = Link(x) # Create a new link with a value attached to it
if self.isEmpty(): # Set the first element added to be the tail
self.tail = newLink
def insert_head(self, x):
new_link = Link(x) # Create a new link with a value attached to it
if self.is_empty(): # Set the first element added to be the tail
self.tail = new_link
else:
self.head.previous = newLink # newLink <-- currenthead(head)
newLink.next = self.head # newLink <--> currenthead(head)
self.head = newLink # newLink(head) <--> oldhead
self.head.previous = new_link # new_link <-- currenthead(head)
new_link.next = self.head # new_link <--> currenthead(head)
self.head = new_link # new_link(head) <--> oldhead
self.size += 1

def deleteHead(self):
def delete_head(self):
temp = self.head
self.head = self.head.next # oldHead <--> 2ndElement(head)
# oldHead --> 2ndElement(head) nothing pointing at it so the old head will be
# removed
self.head.previous = None
if self.head is None:
self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed
if not self.head:
self.tail = None # if empty linked list
self.size -= 1
return temp

def insertTail(self, x):
def insert_tail(self, x):
newLink = Link(x)
newLink.next = None # currentTail(tail) newLink -->
self.tail.next = newLink # currentTail(tail) --> newLink -->
newLink.previous = self.tail # currentTail(tail) <--> newLink -->
self.tail = newLink # oldTail <--> newLink(tail) -->
self.size += 1

def deleteTail(self):
def delete_tail(self):
temp = self.tail
self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None
self.tail.next = None # 2ndlast(tail) --> None
self.size -= 1
return temp

def delete(self, x):
Expand All @@ -54,31 +69,39 @@ def delete(self, x):

if current == self.head:
self.deleteHead()

elif current == self.tail:
self.deleteTail()

else: # Before: 1 <--> 2(current) <--> 3
current.previous.next = current.next # 1 --> 3
current.next.previous = current.previous # 1 <--> 3
self.size -= 1

def isEmpty(self): # Will return True if the list is empty
def is_empty(self): # Will return True if the list is empty
return self.head is None

def display(self): # Prints contents of the list
current = self.head
while current is not None:
while current:
current.displayLink()
current = current.next
print()


class Link:
next = None # This points to the link in front of the new link
previous = None # This points to the link behind the new link

def __init__(self, x):
self.value = x

def displayLink(self):
print(f"{self.value}", end=" ")
def __len__(self):
"""
>>> linked_list = LinkedList()
>>> len(linked_list)
0
>>> linked_list.insert_head("a")
>>> len(linked_list)
1
>>> linked_list.insert_tail("b")
>>> len(linked_list)
2
>>> _ = linked_list.delete_tail()
>>> len(linked_list)
1
>>> _ = linked_list.delete_head()
>>> len(linked_list)
0
"""
return self.size