Circular Linked List

 avatar
shinta0x01
python
7 months ago
4.7 kB
0
Indexable
Never
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
class Circular:
    def __init__(self):
        self.head = self.tail = None

    def insert_start(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = self.tail = new_node
            self.tail.next = self.head
        else:
            new_node.next = self.head
            self.head = new_node
            self.tail.next = self.head

    def insert_end(self, data):
        new_node = Node(data)
        current = self.head
        if self.head is None:
            self.head = self.tail = new_node
            self.tail.next = self.head
        else:
            self.tail.next = new_node
            self.tail = new_node
            self.tail.next = self.head

    def insert_before(self, key, data):
        new_node = Node(data)
        current = self.head
        if self.head is None:
            print('empty')
            return
        if current.data == key:
            new_node.next = self.head
            self.head = new_node
            self.tail.next = self.head
            return
        while current.data != key:
            pre_current = current
            current = current.next
            if current.data != key and current == self.head:
                print('Key not found')
                return
        pre_current.next = new_node
        new_node.next = current

    def insert_after(self, key, data):
        new_node = Node(data)
        current = self.head

        if self.head is None:
            print('empty')
            return
        if current.data == key:
            post_current = current.next
            current.next = new_node
            new_node.next = post_current.next
            if current.data == key and current.next == self.head:
                current.next = new_node
                new_node.next = post_current
                self.tail = new_node
                self.tail.next = self.head
                return

        while current.data != key:
            current = current.next
            post_current = current.next
            if current.data == key and current.next == self.head:
                current.next = new_node
                new_node.next = post_current
                self.tail = new_node
                self.tail.next = self.head
        current.next = new_node
        new_node.next = post_current


    def del_first(self):
        current = self.head
        if self.head is None:
            print('empty')
        else:
            self.head = current.next
            self.tail.next = self.head

    def del_end(self):
        current = self.head
        while current.next != self.head:
            pre_current = current
            current = current.next
        self.tail = pre_current
        self.tail.next = self.head

    def del_given(self, key):
        current = self.head
        if self.head is None:
            print('empty')
            return
        if current.data == key:
            pre_current = current
            current = current.next
            self.head = current
            self.tail.next = self.head
            return
        while current.data != key:
            pre_current = current
            current = current.next
            if current.data == key and current.next == self.head:
                self.tail = pre_current
                self.tail.next = self.head
                return
        pre_current.next = current.next

    def display(self):
        current = self.head
        while True:
            print(current.data, '', end='')
            current = current.next
            if current == self.head:
                break
    def check(self):
        print('Head: ', self.head.data)
        print('Tail: ', self.tail.data)
        print('Tail next: ', self.tail.next.data)

    def concatenate(self, c1):
        temp = self.tail
        self.tail = c1.tail
        temp.next = c1.head
        self.tail.next = self.head


c = Circular()
c1 = Circular()

#1 5 8 9 10
c.insert_start(10)
c.insert_start(9)
c.insert_start(8)
c.insert_start(5)
c.insert_start(1)
# c.insert_end(111)
# c.insert_end(32)
# c.insert_before(1, 100)
# c.insert_after(9, 200)
# c.insert_after(10, 220)
# c.del_end()
# c.del_end()
# c.del_given(220)

c1.insert_start(1)
c1.insert_start(3)
c1.insert_start(2)
c1.insert_start(9)
c1.insert_start(6)



# c.check()
print('list 1')
c.display()
print('\nlist 2')
c1.display()
print()
c.concatenate(c1)
c.display()




Leave a Comment