forked from dsrao711/DSA-Together-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular_linked_list_traversal.py
74 lines (56 loc) · 1.63 KB
/
circular_linked_list_traversal.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Traversal of Circular Linked List
class Node:
def __init__(self , data):
self.data = data
self.next = None
class CircularLinkedList :
def __init__(self):
self.head = None
def InsertNode(self , value):
newNode = Node(value)
# Adding a new node in the beginning
newNode.next = self.head
temp = self.head
# Store the address of new node in the last node
if(self.head is not None):
while(temp.next != self.head):
temp = temp.next
temp.next = newNode
else :
newNode.next = newNode
self.head = newNode
# Insert a Node at the end
def InsertAtEnd(self , value) :
newNode = Node(value)
temp = self.head
if(self.head is None):
self.head = newNode
newNode.next = self.head
return
else :
while(temp.next != self.head):
temp = temp.next
temp.next = newNode
newNode.next = self.head
#Insert a node at any position
# Print the contents of the CLL
def PrintList(self):
temp = self.head
if(temp!= None):
while(True):
print(temp.data)
temp = temp.next
if(temp == self.head):
break
else:
print("Linked List is Empty")
MyList = CircularLinkedList()
MyList.InsertNode(22)
MyList.InsertNode(32)
MyList.InsertNode(42)
MyList.InsertNode(52)
MyList.InsertNode(62)
MyList.PrintList()
MyList.InsertAtEnd(34)
print("Insert at 34 at End")
MyList.PrintList()