@@ -20,14 +20,24 @@ def __len__(self):
2020 def is_empty (self ):
2121 return self .size == 0
2222
23+ def search (self , key ):
24+ curr_node = self .head
25+ while curr_node is not None :
26+ if curr_node .key == key :
27+ return curr_node
28+ curr_node = curr_node .next
29+ if curr_node is self .head :
30+ return None
31+ return None
32+
2333 def __str__ (self ):
2434 """
2535 For printing the linked list.
2636 """
2737 elements = []
2838 current_node = self .head
2939 while current_node is not None :
30- elements .append (current_node . data )
40+ elements .append (str ( current_node ) )
3141 current_node = current_node .next
3242 if current_node == self .head :
3343 break
@@ -43,19 +53,19 @@ class DoublyLinkedList(LinkedList):
4353 >>> from pydatastructs import DoublyLinkedList
4454 >>> dll = DoublyLinkedList()
4555 >>> dll.append(6)
46- >>> dll[0].data
56+ >>> dll[0].key
4757 6
48- >>> dll.head.data
58+ >>> dll.head.key
4959 6
5060 >>> dll.append(5)
5161 >>> dll.append_left(2)
5262 >>> str(dll)
53- '[2, 6, 5]'
54- >>> dll[0].data = 7.2
55- >>> dll.extract(1).data
63+ "['2', '6', '5']"
64+ >>> dll[0].key = 7.2
65+ >>> dll.extract(1).key
5666 6
5767 >>> str(dll)
58- '[ 7.2, 5]'
68+ "[' 7.2', '5']"
5969
6070 References
6171 ==========
@@ -72,7 +82,7 @@ def __new__(cls):
7282 obj .size = 0
7383 return obj
7484
75- def append_left (self , data ):
85+ def append_left (self , key , data = None ):
7686 """
7787 Pushes a new node at the start i.e.,
7888 the left of the list.
@@ -83,9 +93,9 @@ def append_left(self, data):
8393 data
8494 Any valid data to be stored in the node.
8595 """
86- self .insert_at (0 , data )
96+ self .insert_at (0 , key , data )
8797
88- def append (self , data ):
98+ def append (self , key , data = None ):
8999 """
90100 Appends a new node at the end of the list.
91101
@@ -95,9 +105,9 @@ def append(self, data):
95105 data
96106 Any valid data to be stored in the node.
97107 """
98- self .insert_at (self .size , data )
108+ self .insert_at (self .size , key , data )
99109
100- def insert_after (self , prev_node , data ):
110+ def insert_after (self , prev_node , key , data = None ):
101111 """
102112 Inserts a new node after the prev_node.
103113
@@ -112,7 +122,7 @@ def insert_after(self, prev_node, data):
112122 Any valid data to be stored in the node.
113123 """
114124 self .size += 1
115- new_node = LinkedListNode (data ,
125+ new_node = LinkedListNode (key , data ,
116126 links = ['next' , 'prev' ],
117127 addrs = [None , None ])
118128 new_node .next = prev_node .next
@@ -124,7 +134,7 @@ def insert_after(self, prev_node, data):
124134 if new_node .next is None :
125135 self .tail = new_node
126136
127- def insert_before (self , next_node , data ):
137+ def insert_before (self , next_node , key , data = None ):
128138 """
129139 Inserts a new node before the next_node.
130140
@@ -139,7 +149,7 @@ def insert_before(self, next_node, data):
139149 Any valid data to be stored in the node.
140150 """
141151 self .size += 1
142- new_node = LinkedListNode (data ,
152+ new_node = LinkedListNode (key , data ,
143153 links = ['next' , 'prev' ],
144154 addrs = [None , None ])
145155 new_node .prev = next_node .prev
@@ -150,7 +160,7 @@ def insert_before(self, next_node, data):
150160 else :
151161 self .head = new_node
152162
153- def insert_at (self , index , data ):
163+ def insert_at (self , index , key , data = None ):
154164 """
155165 Inserts a new node at the input index.
156166
@@ -173,7 +183,7 @@ def insert_at(self, index, data):
173183 raise IndexError ('%d index is out of range.' % (index ))
174184
175185 self .size += 1
176- new_node = LinkedListNode (data ,
186+ new_node = LinkedListNode (key , data ,
177187 links = ['next' , 'prev' ],
178188 addrs = [None , None ])
179189 if self .size == 1 :
@@ -300,19 +310,19 @@ class SinglyLinkedList(LinkedList):
300310 >>> from pydatastructs import SinglyLinkedList
301311 >>> sll = SinglyLinkedList()
302312 >>> sll.append(6)
303- >>> sll[0].data
313+ >>> sll[0].key
304314 6
305- >>> sll.head.data
315+ >>> sll.head.key
306316 6
307317 >>> sll.append(5)
308318 >>> sll.append_left(2)
309319 >>> str(sll)
310- '[2, 6, 5]'
311- >>> sll[0].data = 7.2
312- >>> sll.extract(1).data
320+ "['2', '6', '5']"
321+ >>> sll[0].key = 7.2
322+ >>> sll.extract(1).key
313323 6
314324 >>> str(sll)
315- '[ 7.2, 5]'
325+ "[' 7.2', '5']"
316326
317327 References
318328 ==========
@@ -329,7 +339,7 @@ def __new__(cls):
329339 obj .size = 0
330340 return obj
331341
332- def append_left (self , data ):
342+ def append_left (self , key , data = None ):
333343 """
334344 Pushes a new node at the start i.e.,
335345 the left of the list.
@@ -340,9 +350,9 @@ def append_left(self, data):
340350 data
341351 Any valid data to be stored in the node.
342352 """
343- self .insert_at (0 , data )
353+ self .insert_at (0 , key , data )
344354
345- def append (self , data ):
355+ def append (self , key , data = None ):
346356 """
347357 Appends a new node at the end of the list.
348358
@@ -352,9 +362,9 @@ def append(self, data):
352362 data
353363 Any valid data to be stored in the node.
354364 """
355- self .insert_at (self .size , data )
365+ self .insert_at (self .size , key , data )
356366
357- def insert_after (self , prev_node , data ):
367+ def insert_after (self , prev_node , key , data = None ):
358368 """
359369 Inserts a new node after the prev_node.
360370
@@ -369,7 +379,7 @@ def insert_after(self, prev_node, data):
369379 Any valid data to be stored in the node.
370380 """
371381 self .size += 1
372- new_node = LinkedListNode (data ,
382+ new_node = LinkedListNode (key , data ,
373383 links = ['next' ],
374384 addrs = [None ])
375385 new_node .next = prev_node .next
@@ -378,7 +388,7 @@ def insert_after(self, prev_node, data):
378388 if new_node .next is None :
379389 self .tail = new_node
380390
381- def insert_at (self , index , data ):
391+ def insert_at (self , index , key , data = None ):
382392 """
383393 Inserts a new node at the input index.
384394
@@ -401,7 +411,7 @@ def insert_at(self, index, data):
401411 raise IndexError ('%d index is out of range.' % (index ))
402412
403413 self .size += 1
404- new_node = LinkedListNode (data ,
414+ new_node = LinkedListNode (key , data ,
405415 links = ['next' ],
406416 addrs = [None ])
407417 if self .size == 1 :
@@ -524,19 +534,19 @@ class SinglyCircularLinkedList(SinglyLinkedList):
524534 >>> from pydatastructs import SinglyCircularLinkedList
525535 >>> scll = SinglyCircularLinkedList()
526536 >>> scll.append(6)
527- >>> scll[0].data
537+ >>> scll[0].key
528538 6
529- >>> scll.head.data
539+ >>> scll.head.key
530540 6
531541 >>> scll.append(5)
532542 >>> scll.append_left(2)
533543 >>> str(scll)
534- '[2, 6, 5]'
535- >>> scll[0].data = 7.2
536- >>> scll.extract(1).data
544+ "['2', '6', '5']"
545+ >>> scll[0].key = 7.2
546+ >>> scll.extract(1).key
537547 6
538548 >>> str(scll)
539- '[ 7.2, 5]'
549+ "[' 7.2', '5']"
540550
541551 References
542552 ==========
@@ -545,7 +555,7 @@ class SinglyCircularLinkedList(SinglyLinkedList):
545555
546556 """
547557
548- def insert_after (self , prev_node , data ):
558+ def insert_after (self , prev_node , key , data = None ):
549559 """
550560 Inserts a new node after the prev_node.
551561
@@ -559,11 +569,12 @@ def insert_after(self, prev_node, data):
559569 data
560570 Any valid data to be stored in the node.
561571 """
562- super (SinglyCircularLinkedList , self ).insert_after (prev_node , data )
572+ super (SinglyCircularLinkedList , self ).\
573+ insert_after (prev_node , key , data )
563574 if prev_node .next .next == self .head :
564575 self .tail = prev_node .next
565576
566- def insert_at (self , index , data ):
577+ def insert_at (self , index , key , data = None ):
567578 """
568579 Inserts a new node at the input index.
569580
@@ -576,7 +587,7 @@ def insert_at(self, index, data):
576587 data
577588 Any valid data to be stored in the node.
578589 """
579- super (SinglyCircularLinkedList , self ).insert_at (index , data )
590+ super (SinglyCircularLinkedList , self ).insert_at (index , key , data )
580591 if self .size == 1 :
581592 self .head .next = self .head
582593 new_node = self .__getitem__ (index )
@@ -618,27 +629,27 @@ class DoublyCircularLinkedList(DoublyLinkedList):
618629 >>> from pydatastructs import DoublyCircularLinkedList
619630 >>> dcll = DoublyCircularLinkedList()
620631 >>> dcll.append(6)
621- >>> dcll[0].data
632+ >>> dcll[0].key
622633 6
623- >>> dcll.head.data
634+ >>> dcll.head.key
624635 6
625636 >>> dcll.append(5)
626637 >>> dcll.append_left(2)
627638 >>> str(dcll)
628- '[2, 6, 5]'
629- >>> dcll[0].data = 7.2
630- >>> dcll.extract(1).data
639+ "['2', '6', '5']"
640+ >>> dcll[0].key = 7.2
641+ >>> dcll.extract(1).key
631642 6
632643 >>> str(dcll)
633- '[ 7.2, 5]'
644+ "[' 7.2', '5']"
634645
635646 References
636647 ==========
637648
638649 .. [1] https://en.wikipedia.org/wiki/Doubly_linked_list#Circular_doubly_linked_lists
639650
640651 """
641- def insert_after (self , prev_node , data ):
652+ def insert_after (self , prev_node , key , data = None ):
642653 """
643654 Inserts a new node after the prev_node.
644655
@@ -652,11 +663,12 @@ def insert_after(self, prev_node, data):
652663 data
653664 Any valid data to be stored in the node.
654665 """
655- super (DoublyCircularLinkedList , self ).insert_after (prev_node , data )
666+ super (DoublyCircularLinkedList , self )\
667+ .insert_after (prev_node , key , data )
656668 if prev_node .next .next == self .head :
657669 self .tail = prev_node .next
658670
659- def insert_before (self , next_node , data ):
671+ def insert_before (self , next_node , key , data = None ):
660672 """
661673 Inserts a new node before the next_node.
662674
@@ -670,11 +682,12 @@ def insert_before(self, next_node, data):
670682 data
671683 Any valid data to be stored in the node.
672684 """
673- super (DoublyCircularLinkedList , self ).insert_before (next_node ,data )
685+ super (DoublyCircularLinkedList , self ).\
686+ insert_before (next_node , key , data )
674687 if next_node == self .head :
675688 self .head = next_node .prev
676689
677- def insert_at (self , index , data ):
690+ def insert_at (self , index , key , data = None ):
678691 """
679692 Inserts a new node at the input index.
680693
@@ -687,7 +700,8 @@ def insert_at(self, index, data):
687700 data
688701 Any valid data to be stored in the node.
689702 """
690- super (DoublyCircularLinkedList , self ).insert_at (index , data )
703+ super (DoublyCircularLinkedList , self ).\
704+ insert_at (index , key , data )
691705 if self .size == 1 :
692706 self .head .next = self .head
693707 self .head .prev = self .head
0 commit comments