Skip to content

Commit 9b0adf9

Browse files
committed
Made PEP8 compatible
1 parent 438df7e commit 9b0adf9

File tree

1 file changed

+27
-40
lines changed

1 file changed

+27
-40
lines changed

pygorithm/data_structures/linked_list.py

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,32 @@
66
class Node(object):
77
# Each node has its data and a pointer that points to next node in the Linked List
88
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
2211
self.next = next
2312

24-
# function to get the next node
25-
def getNext(self):
26-
return self.next
27-
2813
# easily retrieve the source code of the Node class
2914
def get_code(self):
15+
''' return the code for the current class '''
3016
import inspect
3117
return inspect.getsource(Node)
3218

3319
class SinglyLinkedList(object):
3420
# Defining the head of the linked list
3521
def __init__(self):
22+
''' constructor '''
3623
self.head = None
3724

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+
3933
def get_data(self):
34+
''' prints the elements in the linked list '''
4035
temp = self.head
4136
List = []
4237
while(temp):
@@ -46,8 +41,8 @@ def get_data(self):
4641

4742
return List
4843

49-
# inserting the node at the beginning
5044
def insert_at_start(self, data):
45+
''' insert an item at the beginning of the linked list '''
5146
if self.head == None:
5247
newNode = Node(data)
5348
self.head = newNode
@@ -56,26 +51,23 @@ def insert_at_start(self, data):
5651
newNode.next = self.head
5752
self.head = newNode
5853

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 '''
6456
newNode = Node(data)
65-
currentNode = self.search(self.head, next_node_data)
57+
currentNode = self._search(self.head, next_node_data)
6658
newNode.next = currentNode.next
6759
currentNode.next = newNode
6860

69-
# inserting at the end of linked list
7061
def insert_at_end(self, data):
62+
''' insert an item at the end of the linked list '''
7163
newNode = Node(data)
7264
temp = self.head
7365
while(temp.next != None): # get last node
7466
temp = temp.next
7567
temp.next = newNode
7668

77-
# deleting an item based on data(or key)
7869
def delete(self, data):
70+
''' to delete specified element from the linked list '''
7971
temp = self.head
8072
# if data/key is found in head node itself
8173
if (temp is not None):
@@ -84,7 +76,7 @@ def delete(self, data):
8476
temp = None
8577
return
8678
else:
87-
# else search all the nodes
79+
# else _search all the nodes
8880
while(temp.next != None):
8981
if(temp.data == data):
9082
break
@@ -98,25 +90,19 @@ def delete(self, data):
9890
prev.next = temp.next
9991
return
10092

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-
10993
# easily retrieve the source code of the SinglyLinkedList class
11094
def get_code(self):
95+
''' return the code for the current class '''
11196
import inspect
11297
return inspect.getsource(SinglyLinkedList)
11398

11499
class DoublyLinkedList(object):
115100
def __init__(self):
101+
''' constructor '''
116102
self.head = None
117103

118-
# printing the data in the linked list
119104
def get_data(self):
105+
''' prints the elements in the linked list '''
120106
temp = self.head
121107
List = []
122108
while(temp):
@@ -126,8 +112,8 @@ def get_data(self):
126112

127113
return List
128114

129-
# for inserting at beginning of linked list
130115
def insert_at_start(self, data):
116+
''' insert an element at the beginning of the linked list '''
131117
if self.head == None:
132118
newNode = Node(data)
133119
self.head = newNode
@@ -137,17 +123,17 @@ def insert_at_start(self, data):
137123
newNode.next = self.head
138124
self.head = newNode
139125

140-
# for inserting at end of linked list
141126
def insert_at_end(self, data):
127+
''' insert an element at the end of the linked list '''
142128
newNode = Node(data)
143129
temp = self.head
144130
while(temp.next != None):
145131
temp = temp.next
146132
temp.next = newNode
147133
newNode.previous = temp
148134

149-
# deleting a node from linked list
150135
def delete(self, data):
136+
''' to delete specified element from the linked list '''
151137
temp = self.head
152138
if(temp.next != None):
153139
# if head node is to be deleted
@@ -178,5 +164,6 @@ def delete(self, data):
178164

179165
# easily retrieve the source code of the DoublyLinkedList class
180166
def get_code(self):
167+
''' returns the code of the current class '''
181168
import inspect
182169
return inspect.getsource(DoublyLinkedList)

0 commit comments

Comments
 (0)