Чтобы вставить элемент в вдвойне связанный список, нам сначала необходимо создать список:
class Nodeq: def __init__(self, data): self.data = data self.next = None self.prev = None
Существует три способа вставки элемента в вдвойне связанный список:
inslip_beginning.
insert_end.
insert_after.
Вставьте начало
Начало вставки – это способ, чтобы включить new_node
Перед узел головки и представляете new_node.
как новый узел головки.
def insert_beginning(self,data): new_node = Nodeq(data)#creating a node with the given data if(self.head == None): self.head = new_node; return self.head.prev = new_node new_node.next = self.head self.head = new_node #making the new_node as the new head node
Вставить конец
С помощью insert_end
Мы можем вставлять элементы в конце вдвойне связанного списка.
def insert_end(self, new_data): new_node = Nodeq(new_data) if self.head is None:#checking if the DLL exists new_node.prev = None self.head = new_node return last = self.head while last.next:#to asigning the last node to the "last" last = last.next last.next = new_node new_node.prev = last#inserting the new node at the end of the DLL return
Вставить после
С помощью insert_after
Мы можем вставить после данного элемента.
Prev_data
это данный элемент.
def insert_after(self, prev_data, data): h=self.head while h:#searching for the given data if prev_data == h.data: break h=h.next if h is None: print("The previous data is not present in the DLL") return new_node = Nodeq(data) # the new_node is inserted after the given node new_node.next= h.next h.next=new_node return
Чтобы распечатать элементы в вдвойне связанном списке, мы используем Count_display
метод.
def Count_Display(self): if self.head==None: # to check if DLL exists print("We need a DLL to print") return temp = self.head count=0 while temp: print (temp.data)#printing node data count+=1 temp = temp.next return count
Код
Окончательный код для вставки элементов в вдвойне связанный список показан ниже.
class Nodeq: def __init__(self, data): self.data = data self.next = None self.prev = None class DoublyLinkedList: def __init__(self): self.head = None def Count_Display(self): if self.head==None: print("We need a DLL to print") return temp = self.head count=0 while temp: print (temp.data) count+=1 temp = temp.next return count def insert_begining(self,data): new_node = Nodeq(data) if(self.head == None): self.head = new_node return self.head.prev = new_node new_node.next = self.head self.head = new_node def insert_end(self, new_data): new_node = Nodeq(new_data) if self.head is None: new_node.prev = None self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node new_node.prev = last return def insert_after(self, prev_data, data): h=self.head while h: if prev_data == h.data: break h=h.next if h is None: print("The previous Node data must be in the DLL") return new_node = Nodeq(data) new_node.next= h.next h.next=new_node return Dllist = DoublyLinkedList() Dllist.insert_end(2) Dllist.insert_begining(3) Dllist.insert_begining(4) Dllist.insert_end(5) Dllist.insert_after(2,0) print("The number of elements in the DLL list are :",Dllist.Count_Display())
Оригинал: “https://dev.to/221910301042/how-to-insert-elements-in-a-doubly-linked-list-591p”