Связанный список

Создание связного списка.

class Node:   # Node Creation
    def __init__(self, data):
        self.data=data
        self.ref=None 
class LinkedList:   # Connecting the nodes
    def __int__(self):
        self.head=None
    def print_ll(self):  # Printing of the created linhed list
        if self.head is None:
            print("Linked list is empty")
            return
        n=self.head
        while n:
            print(n.data)
            n=n.ref

node1 =Node(10)   # Given the vazlue in the 1st node
node2=Node(2)   # Giving the value in the second node
q1=LinkedList()  # Calling the Linkeglist function
q1.head=node1  # pointing of q1 as a head node
node1.ref=node2   # storing the addresss of the 2nd node into 
                    first node
q1.print_ll()  # printing of q1
Вход в полноэкранный режим Выход из полноэкранного режима

Оцените статью
devanswers.ru
Добавить комментарий