#implementing circulat linked list using python 3

class Node:
    def __init__(self, data):
        self.data = data 
        self.next = None

class CircularLinkedList:
    def __init__(self):
        self.head = None

    def prepend(self, data):
        
        #adding node at the beginning of the list 
        new_node = Node(data)
        cur = self.head
        new_node.next = self.head

        #if list is empty
        if not self.head:
            new_node.next = new_node
        else:
            while cur.next != self.head:
                cur = cur.next
            cur.next = new_node
        self.head = new_node

    def append(self, data):
        #if list is empty
        if not self.head:
            self.head = Node(data)
            self.head.next = self.head

        else:
            new_node = Node(data)
            cur = self.head
            
            #checking till the current nodes next element in not head 
            #if nodes next elemt is head, make current node next as new
            #and new node next as self.head 
            while cur.next != self.head: 
                cur = cur.next
            cur.next = new_node
            new_node.next = self.head

    def print_list(self):
        cur = self.head
        while cur:
            print(cur.data)
            cur = cur.next 

            #loop need to break when cur.next == head
            if cur == self.head:
                break


cllist = CircularLinkedList()
cllist.append("C")
cllist.append("D")
cllist.prepend("B")
cllist.prepend("A")
cllist.print_list()

