-
Notifications
You must be signed in to change notification settings - Fork 63
/
CircularLinkedList.py
98 lines (88 loc) · 2.94 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class Node:
def __init__(self, data=None, next_node=None):
self.data = data
self.next_node = next_node
class CircularLinkedList:
def __init__(self):
self.head = Node()
def addnode(self, data):
new_node = Node(data)
if self.head.data is None:
self.head = new_node
self.head.next_node = self.head
carrier = self.head
while carrier.next_node is not self.head:
carrier = carrier.next_node
carrier.next_node = new_node
new_node.next_node = self.head
print("******************Node ADDED*******************")
def addhead(self,data):
if self.head.data is None:
print("Create a LinkedList First")
else:
carrier = self.head
new_node = Node(data)
while carrier.next_node is not self.head:
carrier = carrier.next_node
carrier.next_node = new_node
new_node.next_node = self.head
self.head = new_node
print("**************HEAD ADDED*****************")
def delete_head(self):
if self.head.data is None:
print("Create a LinkedList First")
else:
carrier = self.head
while carrier.next_node is not self.head:
carrier = carrier.next_node
self.head = self.head.next_node
carrier.next_node = self.head
print("****************HEAD DELETED****************")
def add_target_node(self, target, data):
if self.head.data is None:
print("Create a LinkedList First")
else:
index=1
new_node = Node(data)
prev = Node()
carrier = self.head
while index != target:
prev = carrier
carrier = carrier.next_node
index += 1
prev.next_node = new_node
new_node.next_node = carrier
def del_target_node(self,target):
if self.head.data is None:
print("Create a LinkedList First")
else:
index=1
prev = Node()
carrier = self.head
while index != target:
prev = carrier
carrier = carrier.next_node
index += 1
prev.next_node = carrier.next_node
def show_list(self):
if self.head.data is None:
print("Create a LinkedList First")
else:
node_id = 1
carrier = self.head
while carrier.next_node is not self.head:
print(carrier.data)
carrier = carrier.next_node
node_id += 1
print(carrier.data)
if __name__ == '__main__':
noder = CircularLinkedList()
noder.addnode(1)
noder.addnode(2)
noder.addnode(3)
noder.addnode(4)
noder.addnode(5)
noder.addnode(6)
noder.addnode(7)
noder.del_target_node(4)
noder.show_list()