Luego de de esta larga recopilación de información pudimos resolver esta problema que pueden tener muchos los lectores. Te dejamos la solución y nuestro objetivo es serte de gran apoyo.
Ejemplo: lista enlazada ordenada en python
classNode:def__init__(self, data, next1):
self.data = data
self.next= next1
classLinkedlist:def__init__(self):
self.head =None
self.size =0deflength(self):return self.size
defis_empty(self):return self.size ==0# for normal singly linked list"""
def insert_at_the_beginning(self, data):
self.insert_with_index(0, data)
def insert_at_the_ending(self, data):
self.insert_with_index(self.size, data)
def insert_with_index(self, index, data):
if index > self.size or index < 0:
print("check given", index, "index value and enter again")
return False
if index == 0:
self.head = Node(data, self.head)
else:
current = self.head
for i in range(index - 1):
current = current.next
current.next = Node(data, current.next)
self.size += 1
"""defpeek_top(self):return self.peek_index(0)defpeek_bottom(self):return self.peek_index(self.size -1)defpeek_index(self, index):if index >= self.size or index <0:print("check given", index,"index value and enter again")returnFalse
current = self.head
for i inrange(index):
current = current.nextreturn current.data
defpeek_element(self, data):
current = self.head
while current.data != data:if current.nextisNone:print("element", data,"not found")returnFalse
current = current.nextprint("element", data,"is found")returnTruedefdelete_top_element(self):return self.delete_with_index(0)defdelete_bottom_element(self):return self.delete_with_index(self.size -1)defdelete_with_index(self, index):if index >= self.size or index <0:print("check given", index,"index value and enter again")returnFalse
self.size -=1if index ==0:
temp = self.head
self.head = self.head.nextreturn temp.data
current = self.head
for i inrange(index -1):
current = current.next
temp = current.next
current.next= current.next.nextreturn temp.data
defdelete_with_value(self, data):
current = self.head
previous = current
while current.data != data:if current.nextisNone:print("element", data,"not found")returnFalse
previous = current
current = current.next
temp = previous.next
previous.next= current.nextprint("element", data,"is found and deleted")
self.size -=1return temp.data
defprint_val(self):
current = self.head
while current:print(current.data,"b--->", end="")
current = current.nextprint()classSorted_Linked_List(Linkedlist):definsert_element(self, data):if self.head isNone:
self.head = Node(data, self.head)else:
current = self.head
previous = current
while current.data < data:if current.nextisNone:
current.next= Node(data, current.next)returnTrue
previous = current
current = current.nextif previous == current:
self.head = Node(data, self.head)else:
previous.next= Node(data, previous.next)
self.size +=1
linkedlist = Sorted_Linked_List()deftrail1():
linkedlist.insert_element(78)
linkedlist.insert_element(34)
linkedlist.insert_element(45)
linkedlist.insert_element(899)
linkedlist.insert_element(0)
linkedlist.insert_element(-1)
linkedlist.insert_element(8999999)
linkedlist.print_val()if __name__ =="__main__":
trail1()
Reseñas y puntuaciones
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)