6
6
class Node (object ):
7
7
# Each node has its data and a pointer that points to next node in the Linked List
8
8
def __init__ (self , data , next = None ):
9
- self .data = data ;
10
- self .next = next ;
11
-
12
- # function to set data
13
- def setData (self , data ):
14
- self .data = data ;
15
-
16
- # function to get data of a particular node
17
- def getData (self ):
18
- return self .data
19
-
20
- # function to set next node
21
- def setNext (self , next ):
9
+ ''' constructor '''
10
+ self .data = data
22
11
self .next = next
23
12
24
- # function to get the next node
25
- def getNext (self ):
26
- return self .next
27
-
28
13
# easily retrieve the source code of the Node class
29
14
def get_code (self ):
15
+ ''' return the code for the current class '''
30
16
import inspect
31
17
return inspect .getsource (Node )
32
18
33
19
class SinglyLinkedList (object ):
34
20
# Defining the head of the linked list
35
21
def __init__ (self ):
22
+ ''' constructor '''
36
23
self .head = None
37
24
38
- # printing the data in the linked list
25
+ def _search (self , node , data ):
26
+ ''' searches the node, if valid returns the node else return false '''
27
+ if node == None :
28
+ return False
29
+ if node .data == data :
30
+ return node
31
+ return self ._search (node .get_next (), data )
32
+
39
33
def get_data (self ):
34
+ ''' prints the elements in the linked list '''
40
35
temp = self .head
41
36
List = []
42
37
while (temp ):
@@ -46,8 +41,8 @@ def get_data(self):
46
41
47
42
return List
48
43
49
- # inserting the node at the beginning
50
44
def insert_at_start (self , data ):
45
+ ''' insert an item at the beginning of the linked list '''
51
46
if self .head == None :
52
47
newNode = Node (data )
53
48
self .head = newNode
@@ -56,26 +51,23 @@ def insert_at_start(self, data):
56
51
newNode .next = self .head
57
52
self .head = newNode
58
53
59
- # inserting the node in between the linked list (after a specific node)
60
- def insert_between (self , next_node_data , data ):
61
- # if (previousNode.next is None):
62
- # print('Previous node should have next node!')
63
- # else:
54
+ def insert_after (self , next_node_data , data ):
55
+ ''' insert an item after an element in the linked list '''
64
56
newNode = Node (data )
65
- currentNode = self .search (self .head , next_node_data )
57
+ currentNode = self ._search (self .head , next_node_data )
66
58
newNode .next = currentNode .next
67
59
currentNode .next = newNode
68
60
69
- # inserting at the end of linked list
70
61
def insert_at_end (self , data ):
62
+ ''' insert an item at the end of the linked list '''
71
63
newNode = Node (data )
72
64
temp = self .head
73
65
while (temp .next != None ): # get last node
74
66
temp = temp .next
75
67
temp .next = newNode
76
68
77
- # deleting an item based on data(or key)
78
69
def delete (self , data ):
70
+ ''' to delete specified element from the linked list '''
79
71
temp = self .head
80
72
# if data/key is found in head node itself
81
73
if (temp is not None ):
@@ -84,7 +76,7 @@ def delete(self, data):
84
76
temp = None
85
77
return
86
78
else :
87
- # else search all the nodes
79
+ # else _search all the nodes
88
80
while (temp .next != None ):
89
81
if (temp .data == data ):
90
82
break
@@ -98,25 +90,19 @@ def delete(self, data):
98
90
prev .next = temp .next
99
91
return
100
92
101
- # iterative search
102
- def search (self , node , data ):
103
- if node == None :
104
- return False
105
- if node .data == data :
106
- return node
107
- return self .search (node .getNext (), data )
108
-
109
93
# easily retrieve the source code of the SinglyLinkedList class
110
94
def get_code (self ):
95
+ ''' return the code for the current class '''
111
96
import inspect
112
97
return inspect .getsource (SinglyLinkedList )
113
98
114
99
class DoublyLinkedList (object ):
115
100
def __init__ (self ):
101
+ ''' constructor '''
116
102
self .head = None
117
103
118
- # printing the data in the linked list
119
104
def get_data (self ):
105
+ ''' prints the elements in the linked list '''
120
106
temp = self .head
121
107
List = []
122
108
while (temp ):
@@ -126,8 +112,8 @@ def get_data(self):
126
112
127
113
return List
128
114
129
- # for inserting at beginning of linked list
130
115
def insert_at_start (self , data ):
116
+ ''' insert an element at the beginning of the linked list '''
131
117
if self .head == None :
132
118
newNode = Node (data )
133
119
self .head = newNode
@@ -137,17 +123,17 @@ def insert_at_start(self, data):
137
123
newNode .next = self .head
138
124
self .head = newNode
139
125
140
- # for inserting at end of linked list
141
126
def insert_at_end (self , data ):
127
+ ''' insert an element at the end of the linked list '''
142
128
newNode = Node (data )
143
129
temp = self .head
144
130
while (temp .next != None ):
145
131
temp = temp .next
146
132
temp .next = newNode
147
133
newNode .previous = temp
148
134
149
- # deleting a node from linked list
150
135
def delete (self , data ):
136
+ ''' to delete specified element from the linked list '''
151
137
temp = self .head
152
138
if (temp .next != None ):
153
139
# if head node is to be deleted
@@ -178,5 +164,6 @@ def delete(self, data):
178
164
179
165
# easily retrieve the source code of the DoublyLinkedList class
180
166
def get_code (self ):
167
+ ''' returns the code of the current class '''
181
168
import inspect
182
169
return inspect .getsource (DoublyLinkedList )
0 commit comments