Saltar al contenido

listnode en ejemplo de código de Python

Hola usuario de nuestra página web, hallamos la respuesta a tu pregunta, has scroll y la obtendrás aquí.

Ejemplo 1: lista vinculada en python

classNode:def__init__(self, data =None, next_node =None):
        self.data = data
        self.nextNode = next_node

    defget_data(self):return self.data

    defset_data(self, data):
        self.data = data

    defget_nextNode(self):return self.nextNode

    defset_nextNode(self, nextNode):
        self.nextNode = nextNode


classLinkedList:def__init__(self, head =None):
        self.head = head


    defadd_Node(self, data):# if emptyif self.head ==None:
            self.head = Node(data)# not emptyelse:
            curr_Node = self.head
            
            # if node added is at the startif data < curr_Node.get_data():
                self.head = Node(data, curr_Node)# not at startelse:while data > curr_Node.get_data()and curr_Node.get_nextNode()!=None:
                    prev_Node = curr_Node
                    curr_Node = curr_Node.get_nextNode()# if node added is at the middleif data < curr_Node.get_data():
                    prev_Node.set_nextNode(Node(data, curr_Node))# if node added is at the lastelif data > curr_Node.get_data()and curr_Node.get_nextNode()==None:
                    curr_Node.set_nextNode(Node(data))defsearch(self, data):
        curr_Node = self.head
        while curr_Node !=None:if data == curr_Node.get_data():returnTrueelse:
                curr_Node = curr_Node.get_nextNode()returnFalsedefdelete_Node(self, data):if self.search(data):# if data is found

            curr_Node = self.head
            #if node to be deleted is the first nodeif curr_Node.get_data()== data:
                self.head = curr_Node.get_nextNode()else:while curr_Node.get_data()!= data:
                    prev_Node = curr_Node
                    curr_Node = curr_Node.get_nextNode()#node to be deleted is middleif curr_Node.get_nextNode()!=None:
                    prev_Node.set_nextNode(curr_Node.get_nextNode())# node to be deleted is at the endelif curr_Node.get_nextNode()==None:
                    prev_Node.set_nextNode(None)else:return"Not found."defreturn_as_lst(self):
        lst =[]
        curr_Node = self.head
        while curr_Node !=None:
            lst.append(curr_Node.get_data())
            curr_Node = curr_Node.get_nextNode()return lst

    defsize(self):
        curr_Node = self.head
        count =0while curr_Node:
            count +=1
            curr_Node = curr_Node.get_nextNode()return count

      
## TEST CASES #
test1 = LinkedList()
test2 = LinkedList()
test1.add_Node(20)
test1.add_Node(15)
test1.add_Node(13)
test1.add_Node(14)
test1.delete_Node(17)print(test1.return_as_lst())print(test2.size())

Ejemplo 2: ejemplo de Python de lista enlazada

classNode:def__init__(self, data=None):
        self.data = data
        self.next=NoneclassSLinkedList:def__init__(self):
        self.head =NonedefAtbegining(self, data_in):
        NewNode = Node(data_in)
        NewNode.next= self.head
        self.head = NewNode
		
# Function to remove nodedefRemoveNode(self, Removekey):

        HeadVal = self.head

        if(HeadVal isnotNone):if(HeadVal.data == Removekey):
                self.head = HeadVal.next
                HeadVal =Nonereturnwhile(HeadVal isnotNone):if HeadVal.data == Removekey:break
            prev = HeadVal
            HeadVal = HeadVal.nextif(HeadVal ==None):return

        prev.next= HeadVal.next

        HeadVal =NonedefLListprint(self):
        printval = self.head
        while(printval):print(printval.data),
            printval = printval.next


llist = SLinkedList()
llist.Atbegining("Mon")
llist.Atbegining("Tue")
llist.Atbegining("Wed")
llist.Atbegining("Thu")
llist.RemoveNode("Tue")
llist.LListprint()

Te mostramos las reseñas y valoraciones de los usuarios

Si tienes algún reparo o forma de refinar nuestro enunciado eres capaz de dejar una explicación y con deseo lo observaremos.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *