Skip to content

Commit 40efd77

Browse files
committed
Fix doubly linked list algorithm
1 parent dc720a8 commit 40efd77

File tree

1 file changed

+83
-50
lines changed

1 file changed

+83
-50
lines changed

Diff for: data_structures/linked_list/doubly_linked_list.py

+83-50
Original file line numberDiff line numberDiff line change
@@ -9,76 +9,109 @@
99
Delete operation is more efficient"""
1010

1111

12-
class LinkedList: # making main class named linked list
13-
def __init__(self):
14-
self.head = None
15-
self.tail = None
12+
class LinkedList:
13+
"""
14+
>>> linked_list = LinkedList()
15+
>>> linked_list.insert_at_head("a")
16+
>>> linked_list.insert_at_tail("b")
17+
>>> linked_list.delete_tail()
18+
'b'
19+
>>> linked_list.is_empty
20+
False
21+
>>> linked_list.delete_head()
22+
'a'
23+
>>> linked_list.is_empty
24+
True
25+
"""
1626

17-
def insertHead(self, x):
18-
newLink = Link(x) # Create a new link with a value attached to it
19-
if self.isEmpty(): # Set the first element added to be the tail
20-
self.tail = newLink
27+
def __init__(self):
28+
self.head = None # First node in list
29+
self.tail = None # Last node in list
30+
31+
def insert_at_head(self, data):
32+
new_node = Node(data)
33+
if self.is_empty:
34+
self.tail = new_node
35+
self.head = new_node
2136
else:
22-
self.head.previous = newLink # newLink <-- currenthead(head)
23-
newLink.next = self.head # newLink <--> currenthead(head)
24-
self.head = newLink # newLink(head) <--> oldhead
25-
26-
def deleteHead(self):
27-
temp = self.head
28-
self.head = self.head.next # oldHead <--> 2ndElement(head)
29-
# oldHead --> 2ndElement(head) nothing pointing at it so the old head will be
30-
# removed
31-
self.head.previous = None
32-
if self.head is None:
33-
self.tail = None # if empty linked list
34-
return temp
35-
36-
def insertTail(self, x):
37-
newLink = Link(x)
38-
newLink.next = None # currentTail(tail) newLink -->
39-
self.tail.next = newLink # currentTail(tail) --> newLink -->
40-
newLink.previous = self.tail # currentTail(tail) <--> newLink -->
41-
self.tail = newLink # oldTail <--> newLink(tail) -->
42-
43-
def deleteTail(self):
44-
temp = self.tail
45-
self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None
46-
self.tail.next = None # 2ndlast(tail) --> None
47-
return temp
48-
49-
def delete(self, x):
37+
self.head.previous = new_node
38+
new_node.next = self.head
39+
self.head = new_node
40+
41+
def delete_head(self) -> str:
42+
if self.is_empty:
43+
return "List is empty"
44+
45+
head_data = self.head.data
46+
if self.head.next:
47+
self.head = self.head.next
48+
self.head.previous = None
49+
50+
else: # If there is no next previous node
51+
self.head = None
52+
self.tail = None
53+
54+
return head_data
55+
56+
def insert_at_tail(self, data):
57+
new_node = Node(data)
58+
self.tail.next = new_node
59+
new_node.previous = self.tail
60+
self.tail = new_node
61+
62+
def delete_tail(self) -> str:
63+
if self.is_empty:
64+
return "List is empty"
65+
66+
tail_data = self.tail.data
67+
if self.tail.previous:
68+
self.tail = self.tail.previous
69+
self.tail.next = None
70+
else: # if there is no previous node
71+
self.head = None
72+
self.tail = None
73+
74+
return tail_data
75+
76+
def delete(self, data) -> str:
5077
current = self.head
5178

52-
while current.value != x: # Find the position to delete
53-
current = current.next
79+
while current.data != data: # Find the position to delete
80+
if current.next:
81+
current = current.next
82+
else: # We have reached the end an no value matches
83+
return "No data matching given value"
5484

5585
if current == self.head:
56-
self.deleteHead()
86+
self.delete_head()
5787

5888
elif current == self.tail:
59-
self.deleteTail()
89+
self.delete_tail()
6090

6191
else: # Before: 1 <--> 2(current) <--> 3
6292
current.previous.next = current.next # 1 --> 3
6393
current.next.previous = current.previous # 1 <--> 3
94+
return data
6495

65-
def isEmpty(self): # Will return True if the list is empty
96+
@property
97+
def is_empty(self): # Will return True if the list is empty
6698
return self.head is None
6799

68100
def display(self): # Prints contents of the list
101+
if self.is_empty:
102+
return "List is empty"
69103
current = self.head
70104
while current is not None:
71-
current.displayLink()
105+
current.display_node()
72106
current = current.next
73107
print()
74108

75109

76-
class Link:
77-
next = None # This points to the link in front of the new link
78-
previous = None # This points to the link behind the new link
79-
80-
def __init__(self, x):
81-
self.value = x
110+
class Node:
111+
def __init__(self, data):
112+
self.data = data
113+
self.previous = None
114+
self.next = None
82115

83-
def displayLink(self):
84-
print(f"{self.value}", end=" ")
116+
def display_node(self):
117+
print(f"{self.data}", end=" ")

0 commit comments

Comments
 (0)