Skip to content

Commit 1b19028

Browse files
adityasharma1234harshildarji
authored andcommitted
Update doubly_linked_list.py (TheAlgorithms#545)
1 parent 6db1994 commit 1b19028

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

data_structures/linked_list/doubly_linked_list.py

+22-21
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,50 @@
22
- A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes.
33
- This is an example of a double ended, doubly linked list.
44
- Each link references the next link and the previous one.
5-
'''
5+
- 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.
6+
- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent'''
67
from __future__ import print_function
78

89

9-
class LinkedList:
10+
class LinkedList: #making main class named linked list
1011
def __init__(self):
1112
self.head = None
1213
self.tail = None
1314

1415
def insertHead(self, x):
15-
newLink = Link(x) #Create a new link with a value attached to it
16-
if(self.isEmpty() == True): #Set the first element added to be the tail
16+
newLink = Link(x) #Create a new link with a value attached to it
17+
if(self.isEmpty() == True): #Set the first element added to be the tail
1718
self.tail = newLink
1819
else:
19-
self.head.previous = newLink # newLink <-- currenthead(head)
20-
newLink.next = self.head # newLink <--> currenthead(head)
21-
self.head = newLink # newLink(head) <--> oldhead
20+
self.head.previous = newLink # newLink <-- currenthead(head)
21+
newLink.next = self.head # newLink <--> currenthead(head)
22+
self.head = newLink # newLink(head) <--> oldhead
2223

2324
def deleteHead(self):
2425
temp = self.head
25-
self.head = self.head.next # oldHead <--> 2ndElement(head)
26-
self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed
26+
self.head = self.head.next # oldHead <--> 2ndElement(head)
27+
self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed
2728
if(self.head is None):
28-
self.tail = None
29+
self.tail = None #if empty linked list
2930
return temp
3031

3132
def insertTail(self, x):
3233
newLink = Link(x)
33-
newLink.next = None # currentTail(tail) newLink -->
34-
self.tail.next = newLink # currentTail(tail) --> newLink -->
35-
newLink.previous = self.tail #currentTail(tail) <--> newLink -->
36-
self.tail = newLink # oldTail <--> newLink(tail) -->
34+
newLink.next = None # currentTail(tail) newLink -->
35+
self.tail.next = newLink # currentTail(tail) --> newLink -->
36+
newLink.previous = self.tail #currentTail(tail) <--> newLink -->
37+
self.tail = newLink # oldTail <--> newLink(tail) -->
3738

3839
def deleteTail(self):
3940
temp = self.tail
40-
self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None
41-
self.tail.next = None # 2ndlast(tail) --> None
41+
self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None
42+
self.tail.next = None # 2ndlast(tail) --> None
4243
return temp
4344

4445
def delete(self, x):
4546
current = self.head
4647

47-
while(current.value != x): # Find the position to delete
48+
while(current.value != x): # Find the position to delete
4849
current = current.next
4950

5051
if(current == self.head):
@@ -57,19 +58,19 @@ def delete(self, x):
5758
current.previous.next = current.next # 1 --> 3
5859
current.next.previous = current.previous # 1 <--> 3
5960

60-
def isEmpty(self): #Will return True if the list is empty
61+
def isEmpty(self): #Will return True if the list is empty
6162
return(self.head is None)
6263

63-
def display(self): #Prints contents of the list
64+
def display(self): #Prints contents of the list
6465
current = self.head
6566
while(current != None):
6667
current.displayLink()
6768
current = current.next
6869
print()
6970

7071
class Link:
71-
next = None #This points to the link in front of the new link
72-
previous = None #This points to the link behind the new link
72+
next = None #This points to the link in front of the new link
73+
previous = None #This points to the link behind the new link
7374
def __init__(self, x):
7475
self.value = x
7576
def displayLink(self):

0 commit comments

Comments
 (0)