Skip to content

Commit 9e9a8d8

Browse files
updated cll
updated insert at middle function and get length function
1 parent 99e7aea commit 9e9a8d8

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

circular linked list.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def prepending_a_node(self,new_data):
4646
self.head=newNode
4747
#creating a function for inserting at middle
4848
def insert_at_middle(self,index,new_data):
49-
if index < 0 or index >= self.get_length_of_cll():
49+
if index < 0 and index >= self.get_length_of_cll():
5050
raise Exception("index error")
5151
return
5252
if index == 0:
@@ -129,9 +129,12 @@ def get_length_of_cll(self):
129129
return
130130
firstNOde=self.head
131131
count=0
132-
while firstNOde and firstNOde.next != self.head:
132+
while firstNOde:
133133
firstNOde=firstNOde.next
134134
count+=1
135+
if firstNOde == self.head:
136+
break
137+
#print("length of cll is {} ".format(count))
135138
return count
136139

137140

@@ -162,11 +165,23 @@ def main():
162165
c1.insert_at_end(4646)
163166
c1.insert_at_end(1)
164167
c1.insert_at_middle(1,45)
168+
c1.get_length_of_cll()
165169
c1.print_cll()
166170
c1.removing_an_element_at_beg()
171+
c1.print_cll()
167172
c1.removing_an_element_at_end()
173+
c1.print_cll()
168174
c1.removing_an_element_at_middle(1)
175+
c=c1.get_length_of_cll()
176+
print("The length of the cll is {}".format(c))
169177
c1.print_cll()
170178

179+
c2=cll("Second Circular Linked list")
180+
c2.removing_an_element_at_beg()
181+
c2.removing_an_element_at_end()
182+
c2.insert_at_middle(0,4545)
183+
c2.removing_an_element_at_middle(0)
184+
c2.print_cll()
185+
171186
if __name__=="__main__":
172187
main()

0 commit comments

Comments
 (0)