Python/data_structures/linked_list/doubly_linked_list.py

80 lines
2.9 KiB
Python
Raw Normal View History

2019-10-05 05:14:13 +00:00
"""
2018-10-19 12:48:28 +00:00
- A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes.
- This is an example of a double ended, doubly linked list.
- Each link references the next link and the previous one.
2018-10-31 07:34:55 +00:00
- 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.
2019-10-05 05:14:13 +00:00
- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent"""
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
class LinkedList: # making main class named linked list
2018-10-19 12:48:28 +00:00
def __init__(self):
self.head = None
self.tail = None
2018-10-19 12:48:28 +00:00
def insertHead(self, x):
2019-10-05 05:14:13 +00:00
newLink = Link(x) # Create a new link with a value attached to it
if self.isEmpty() == True: # Set the first element added to be the tail
2018-10-19 12:48:28 +00:00
self.tail = newLink
else:
2019-10-05 05:14:13 +00:00
self.head.previous = newLink # newLink <-- currenthead(head)
newLink.next = self.head # newLink <--> currenthead(head)
self.head = newLink # newLink(head) <--> oldhead
2018-10-19 12:48:28 +00:00
def deleteHead(self):
temp = self.head
2019-10-05 05:14:13 +00:00
self.head = self.head.next # oldHead <--> 2ndElement(head)
self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed
2019-10-05 05:14:13 +00:00
if self.head is None:
self.tail = None # if empty linked list
2018-10-19 12:48:28 +00:00
return temp
2018-10-19 12:48:28 +00:00
def insertTail(self, x):
newLink = Link(x)
2019-10-05 05:14:13 +00:00
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) -->
2018-10-19 12:48:28 +00:00
def deleteTail(self):
temp = self.tail
2019-10-05 05:14:13 +00:00
self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None
self.tail.next = None # 2ndlast(tail) --> None
2018-10-19 12:48:28 +00:00
return temp
2018-10-19 12:48:28 +00:00
def delete(self, x):
current = self.head
2019-10-05 05:14:13 +00:00
while current.value != x: # Find the position to delete
2018-10-19 12:48:28 +00:00
current = current.next
2019-10-05 05:14:13 +00:00
if current == self.head:
2018-10-19 12:48:28 +00:00
self.deleteHead()
2019-10-05 05:14:13 +00:00
elif current == self.tail:
2018-10-19 12:48:28 +00:00
self.deleteTail()
2019-10-05 05:14:13 +00:00
else: # Before: 1 <--> 2(current) <--> 3
current.previous.next = current.next # 1 --> 3
current.next.previous = current.previous # 1 <--> 3
2019-10-05 05:14:13 +00:00
def isEmpty(self): # Will return True if the list is empty
return self.head is None
2019-10-05 05:14:13 +00:00
def display(self): # Prints contents of the list
2018-10-19 12:48:28 +00:00
current = self.head
2019-10-05 05:14:13 +00:00
while current != None:
2018-10-19 12:48:28 +00:00
current.displayLink()
current = current.next
2018-10-19 12:48:28 +00:00
print()
2019-10-05 05:14:13 +00:00
2018-10-19 12:48:28 +00:00
class Link:
2019-10-05 05:14:13 +00:00
next = None # This points to the link in front of the new link
previous = None # This points to the link behind the new link
2018-10-19 12:48:28 +00:00
def __init__(self, x):
self.value = x
2019-10-05 05:14:13 +00:00
2018-10-19 12:48:28 +00:00
def displayLink(self):
print("{}".format(self.value), end=" ")