@@ -4,6 +4,7 @@ def __init__(self, data=None, next=None, prev=None):
4
4
self .next = next
5
5
self .prev = prev
6
6
7
+
7
8
class DoublyLinkedList :
8
9
def __init__ (self ):
9
10
self .head = None
@@ -44,7 +45,7 @@ def get_length(self):
44
45
count = 0
45
46
itr = self .head
46
47
while itr :
47
- count += 1
48
+ count += 1
48
49
itr = itr .next
49
50
50
51
return count
@@ -71,10 +72,10 @@ def insert_at_end(self, data):
71
72
itr .next = Node (data , None , itr )
72
73
73
74
def insert_at (self , index , data ):
74
- if index < 0 or index > self .get_length ():
75
+ if index < 0 or index > self .get_length ():
75
76
raise Exception ("Invalid Index" )
76
77
77
- if index == 0 :
78
+ if index == 0 :
78
79
self .insert_at_begining (data )
79
80
return
80
81
@@ -92,10 +93,10 @@ def insert_at(self, index, data):
92
93
count += 1
93
94
94
95
def remove_at (self , index ):
95
- if index < 0 or index >= self .get_length ():
96
+ if index < 0 or index >= self .get_length ():
96
97
raise Exception ("Invalid Index" )
97
98
98
- if index == 0 :
99
+ if index == 0 :
99
100
self .head = self .head .next
100
101
self .head .prev = None
101
102
return
@@ -110,7 +111,7 @@ def remove_at(self, index):
110
111
break
111
112
112
113
itr = itr .next
113
- count += 1
114
+ count += 1
114
115
115
116
def insert_values (self , data_list ):
116
117
self .head = None
@@ -120,16 +121,21 @@ def insert_values(self, data_list):
120
121
121
122
if __name__ == '__main__' :
122
123
ll = DoublyLinkedList ()
123
- ll .insert_values (["banana" ,"mango" ,"grapes" ,"orange" ])
124
+ ll .insert_values (["banana" , "mango" , "grapes" , "orange" ])
124
125
ll .print_forward ()
125
126
ll .print_backward ()
126
127
ll .insert_at_end ("figs" )
127
128
ll .print_forward ()
128
- ll .insert_at (0 ,"jackfruit" )
129
+ ll .insert_at (0 , "jackfruit" )
129
130
ll .print_forward ()
130
- ll .insert_at (6 ,"dates" )
131
+ ll .insert_at (6 , "dates" )
131
132
ll .print_forward ()
132
- ll .insert_at (2 ,"kiwi" )
133
+ ll .insert_at (2 , "kiwi" )
134
+ ll .print_forward ()
135
+ print ("--------------------------" )
136
+ ll .remove_at (0 )
137
+ ll .print_forward ()
138
+ ll .remove_at (ll .get_length ()- 1 )
139
+ ll .print_forward ()
140
+ ll .remove_at (3 )
133
141
ll .print_forward ()
134
-
135
-
0 commit comments