Skip to content

Commit 99e7aea

Browse files
updated cll funcs
re writed few cll functions because earlier functions getting error while running
1 parent 14fe878 commit 99e7aea

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

Diff for: circular linked list.py

+46-16
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ def insert_at_end(self,new_data):
2222
self.head=newNode
2323
newNode.next=self.head
2424
return
25-
25+
firstNOde=self.head
26+
newNode=Node(new_data)
27+
newNode.next=firstNOde
28+
while firstNOde and firstNOde.next!=self.head:
29+
firstNOde=firstNOde.next
30+
firstNOde.next=newNode
31+
#newNode.next=self.head
2632

2733
#creating prepending function or inserting at beginning function
2834
def prepending_a_node(self,new_data):
@@ -33,8 +39,11 @@ def prepending_a_node(self,new_data):
3339
return
3440
firstNOde=self.head
3541
newNode=Node(new_data)
42+
newNode.next= firstNOde
43+
while firstNOde and firstNOde.next!=self.head:
44+
firstNOde=firstNOde.next
45+
firstNOde.next=newNode
3646
self.head=newNode
37-
newNode.next=firstNOde
3847
#creating a function for inserting at middle
3948
def insert_at_middle(self,index,new_data):
4049
if index < 0 or index >= self.get_length_of_cll():
@@ -65,11 +74,13 @@ def removing_an_element_at_beg(self):
6574
if self.head and self.get_length_of_cll() == 1:
6675
self.head=None
6776
return
68-
firstNOde=self.head
69-
self.head=firstNOde.next
70-
firstNOde.next=firstNOde.next.next
77+
firstNOde=self.head
78+
while firstNOde and firstNOde.next!=self.head:
79+
firstNOde=firstNOde.next
80+
firstNOde.next=None
81+
self.head=self.head.next
7182
firstNOde=None
72-
'''
83+
7384
#removing an element at end
7485
def removing_an_element_at_end(self):
7586
n=self.name
@@ -89,8 +100,23 @@ def removing_an_element_at_end(self):
89100
break
90101
firstNOde=firstNOde.next
91102
count+=1
92-
'''
103+
93104

105+
#removing an element at middle
106+
def removing_an_element_at_middle(self,index):
107+
if index < 0 or index >= self.get_length_of_cll():
108+
raise Exception("index error")
109+
return
110+
if index == 0:
111+
self.removing_an_element_at_beg()
112+
return
113+
firstNOde=self.head
114+
count=0
115+
while firstNOde and firstNOde.next!=self.head:
116+
if count == index-1:
117+
firstNOde.next=firstNOde.next.next
118+
firstNOde=firstNOde.next
119+
count+=1
94120

95121

96122

@@ -118,24 +144,28 @@ def print_cll(self):
118144
return
119145
firstNOde=self.head
120146
string=" "
121-
while firstNOde and firstNOde.next != self.head:
147+
while firstNOde :
122148
string=str(firstNOde.data)+" --> "
123149
firstNOde=firstNOde.next
124150
print(string,end=" ")
151+
if firstNOde == self.head:
152+
break
125153
print()
126154

127155
#creating objects in main function
128156
def main():
129157
c1=cll("first Circular linked list")
130-
#c1.insert_at_end(89)
131-
#c1.insert_at_end(90)
158+
c1.insert_at_end(89)
159+
c1.insert_at_end(90)
132160
c1.prepending_a_node(91)
133-
#c1.prepending_a_node(1000)
134-
#c1.insert_at_end(1)
135-
#c1.insert_at_middle(2,45)
136-
#c1.print_cll()
137-
#c1.removing_an_element_at_beg()
138-
#c1.removing_an_element_at_end()
161+
c1.prepending_a_node(1000)
162+
c1.insert_at_end(4646)
163+
c1.insert_at_end(1)
164+
c1.insert_at_middle(1,45)
165+
c1.print_cll()
166+
c1.removing_an_element_at_beg()
167+
c1.removing_an_element_at_end()
168+
c1.removing_an_element_at_middle(1)
139169
c1.print_cll()
140170

141171
if __name__=="__main__":

0 commit comments

Comments
 (0)