Skip to content

Définition de la structure d'un noeud dans la liste singulièrement liée exemple de code python

Si vous rencontrez une partie que vous ne comprenez pas, vous pouvez la laisser dans la section des commentaires et nous vous aiderons aussi vite que possible.

Exemple : liste singulièrement liée 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 ==0definsert_at_the_beginning(self, data):
        self.insert_with_index(0, data)definsert_at_the_ending(self, data):
        self.insert_with_index(self.size, data)definsert_with_index(self, index, data):if index > self.size or index <0:print("check given", index,"index value and enter again")returnFalseif index ==0:
            self.head = Node(data, self.head)else:
            current = self.head
            for i inrange(index -1):
                current = current.next
            current.next= Node(data, current.next)
        self.size +=1defpeek_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()

linked_list = Linkedlist()deftrail1():
    linked_list.insert_at_the_beginning(45)
    linked_list.insert_at_the_beginning(65)
    linked_list.insert_at_the_beginning(34)
    linked_list.insert_at_the_beginning(56)
    linked_list.insert_at_the_beginning(78)
    linked_list.insert_at_the_beginning(98)
    linked_list.insert_at_the_beginning(63)
    linked_list.insert_at_the_beginning(31)
    linked_list.print_val()deftrail2():
    linked_list.insert_at_the_beginning(78)
    linked_list.insert_at_the_ending(67778)
    linked_list.insert_at_the_ending(899)
    linked_list.insert_at_the_ending(99)
    linked_list.print_val()
    trail1()deftrail3():
    linked_list.insert_at_the_beginning(34)
    linked_list.insert_at_the_beginning(56)
    linked_list.insert_at_the_beginning(78)
    linked_list.insert_at_the_beginning(31)
    linked_list.insert_at_the_ending(12)
    linked_list.insert_at_the_ending(14)
    linked_list.insert_at_the_ending(56)
    linked_list.insert_with_index(90,345)
    linked_list.insert_with_index(5,23)print(linked_list.peek_index(2))print(linked_list.peek_bottom())print(linked_list.peek_top())
    linked_list.peek_element(16)
    linked_list.peek_element(33)
    linked_list.insert_at_the_beginning(128)
    linked_list.insert_at_the_beginning(784)
    linked_list.insert_at_the_beginning(314)
    linked_list.print_val()print(linked_list.delete_with_index(5))
    linked_list.print_val()print(linked_list.delete_top_element())
    linked_list.print_val()print(linked_list.delete_bottom_element())
    linked_list.print_val()
    linked_list.delete_with_value(12)
    linked_list.print_val()# trail2()# this is siva# signing offif __name__ =="__main__":
    trail3()

Nous vous montrons les avis et les notes des utilisateurs

A la fin de tout, vous pouvez trouver les explications des autres administrateurs, vous avez également la possibilité d'insérer les vôtres si vous le souhaitez.


Tags :

Utilisez notre moteur de recherche

Ricerca
Generic filters

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.