forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorted_Inserted_Linked_List.py
More file actions
46 lines (40 loc) · 1.35 KB
/
Copy pathSorted_Inserted_Linked_List.py
File metadata and controls
46 lines (40 loc) · 1.35 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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Circular_Linked_List:
def __init__(self):
self.head = None
def Sorted_Insert(self, new_node):
current = self.head
if current is None:
new_node.next = new_node
self.head = new_node
elif current.data >= new_node.data:
while current.next != self.head:
current = current.next
current.next = new_node
new_node.next = self.head
self.head = new_node
else:
while current.next != self.head and current.next.data < new_node.data:
current = current.next
new_node.next = current.next
current.next = new_node
def Display(self):
temp = self.head
if self.head is not None:
while temp:
print(temp.data, "->", end=" ")
temp = temp.next
if temp == self.head:
print(temp.data)
break
if __name__ == "__main__":
L_list = Circular_Linked_List()
Test_list = [12, 56, 2, 11, 1, 90]
for keys in Test_list:
temp = Node(keys)
L_list.Sorted_Insert(temp)
print("Sorted Inserted Circular Linked List: ")
L_list.Display()