-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularlinkedlist.py
49 lines (41 loc) · 1.25 KB
/
circularlinkedlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Python3 program to illustrate
# creation and traversal of
# Circular LL
# structure of Node
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
self.last_node = None
# function to add elements to circular linked list
def append(self, data):
# is circular linked list is empty then last_node will be none so in if condition head will be created
if self.last_node is None:
self.head = Node(data)
self.last_node = self.head
# adding node to the tail of circular linked list
else:
self.last_node.next = Node(data)
self.last_node = self.last_node.next
self.last_node.next = self.head
# function to print the content of circular linked list
def display(self):
current = self.head
while current is not None:
print(current.data, end=' ')
current = current.next
if current == self.head:
break
print()
# Driver code
if __name__ == '__main__':
L = CircularLinkedList()
L.append(12)
L.append(56)
L.append(2)
L.append(11)
# Function call
L.display()