Double Linked Listt
Double Linked Listt
class Node:
def __init__(self, data):
self.data = data #adding an element to the node
self.next = None # Initally this node will not be linked with any other node
self.prev = None # It will not be linked in either direction
class DoublyLinkedList:
def __init__(self):
self.head = None # Initally there are no elements in the list
else: # If the list is empty, make new node both head and tail
self.head = new_node
self.tail = new_node
new_node.prev = None # There's only one element so both pointers refer to null
if self.tail == None: # checks whether the list is empty, if so make both head and tail as new node
self.head = new_node
self.tail = new_node
new_node.next = None # the first element's previous pointer has to refer to null
else:
temp = self.head
temp.next.prev = None # remove previous pointer referring to old head
self.head = temp.next # make second element the new head
temp.next = None # remove next pointer referring to new head
return temp.data
else:
temp = self.tail
temp.prev.next = None # removes next pointer referring to old tail
self.tail = temp.prev # make second to last element the new tail
temp.prev = None # remove previous pointer referring to new tail
return temp.data
def insert_after(self, temp_node, new_data): # Inserting a new node after a given node
if temp_node == None:
print("Given node is empty")
if temp_node != None:
new_node = Node(new_data)
new_node.next = temp_node.next
temp_node.next = new_node
new_node.prev = temp_node
if new_node.next != None:
new_node.next.prev = new_node
if temp_node == self.tail: # checks whether new node is being added to the last element
self.tail = new_node # makes new node the new tail
def insert_before(self, temp_node, new_data): # Inserting a new node before a given node
if temp_node == None:
print("Given node is empty")
if temp_node != None:
new_node.prev = temp_node.prev
temp_node.prev = new_node
new_node.next = temp_node
if new_node.prev != None:
new_node.prev.next = new_node
if temp_node == self.head: # checks whether new node is being added before the first element
self.head = new_node # makes new node the new head