-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_linkedlist.py
More file actions
49 lines (41 loc) · 1.15 KB
/
Copy pathcircular_linkedlist.py
File metadata and controls
49 lines (41 loc) · 1.15 KB
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
class Node:
def __init__(self, value):
self.data = value
self.next = None
class CircularLinkedList:
def __init__(self, value=None):
self.head = Node(value)
self.tail = Node(value)
self.head.next = self.tail
self.tail.next = self.head
def is_empty(self):
return self.head.data is None
def add(self, value):
node = Node(value)
if self.is_empty():
self.head = node
self.tail = node
node.next = self.head
else:
self.tail.next = node
self.tail = node
self.tail.next = self.head
def display(self):
current = self.head
if self.is_empty():
print("List is empty")
else:
print("Nodes in the list are:")
print(current.data)
while current.next != self.head:
current = current.next
print(current.data)
cl = CircularLinkedList()
#Adds data to the list
cl.add(1)
cl.add(2)
cl.add(3)
cl.add(4)
print(cl)
#Displays all the nodes present in the list
cl.display()