-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCircurlarSinglyLinkedList.py
131 lines (116 loc) · 4.09 KB
/
CircurlarSinglyLinkedList.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class Node:
def __init__(self, value=None):
self.value = value
self.next = None
class CircularSinglyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def __iter__(self):
node = self.head
while node:
yield node
node = node.next
if node == self.tail.next:
break
# Creation of circular singly linked list
def createCSLL(self, nodeValue):
node = Node(nodeValue)
node.next = node
self.head = node
self.tail = node
return "The CSLL has been created"
# Insertion of a node in circular singly linked list
def insertCSLL(self, value, location):
if self.head is None:
return "The head reference is None"
else:
newNode = Node(value)
if location == 0:
newNode.next = self.head
self.head = newNode
self.tail.next = newNode
elif location == 1:
newNode.next = self.tail.next
self.tail.next = newNode
self.tail = newNode
else:
tempNode = self.head
index = 0
while index < location - 1:
tempNode = tempNode.next
index += 1
nextNode = tempNode.next
tempNode.next = newNode
newNode.next = nextNode
return "The node has been successfully inserted"
# Traversal of a node in circular singly linked list
def traversalCSLL(self):
if self.head is None:
print("There is not any element for traversal")
else:
tempNode = self.head
while tempNode:
print(tempNode.value)
tempNode = tempNode.next
if tempNode == self.tail.next:
break
# Searching for a node in circular singly linked list
def searchCSLL(self, nodeValue):
if self.head is None:
return "There is not any node in this CSLL"
else:
tempNode = self.head
while tempNode:
if tempNode.value == nodeValue:
return tempNode.value
tempNode = tempNode.next
if tempNode == self.tail.next:
return "The node does not exist in this CSLL"
# Delete a node from circular singly linked list
def deleteNode(self, location):
if self.head is None:
print("There is not any node in CSLL")
else:
if location == 0:
if self.head == self.tail:
self.head.next = None
self.head = None
self.tail = None
else:
self.head = self.head.next
self.tail.next = self.head
elif location == 1:
if self.head == self.tail:
self.head.next = None
self.head = None
self.tail = None
else:
node = self.head
while node is not None:
if node.next == self.tail:
break
node = node.next
node.next = self.head
self.tail = node
else:
tempNode = self.head
index = 0
while index < location - 1:
tempNode = tempNode.next
index += 1
nextNode = tempNode.next
tempNode.next = nextNode.next
# Delete entire circular sinlgy linked list
def deleteEntireCSLL(self):
self.head = None
self.tail.next = None
self.tail = None
circularSLL = CircularSinglyLinkedList()
circularSLL.createCSLL(0)
circularSLL.insertCSLL(1,1)
circularSLL.insertCSLL(2,1)
circularSLL.insertCSLL(3,1)
print([node.value for node in circularSLL])
circularSLL.deleteEntireCSLL()
print([node.value for node in circularSLL])