Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 086a1f0

Browse files
author
Amogh Singhal
authored
Update linked_list_data_structure.py
1 parent 3a53ffc commit 086a1f0

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

‎linked_list_data_structure.py‎

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@ def __init__(self, data=None):
33
self.data = data
44
self.next_node = None
55

6-
def getData(self):
7-
return self.data
8-
9-
def setData(self, data):
10-
self.data = data
11-
12-
def getNextNode(self):
13-
return self.next_node
14-
15-
def setNextNode(self, new_next):
16-
self.next_node = new_next
17-
186
class LinkedList:
197
def __init__(self, head=None):
208
self.head = head
@@ -23,44 +11,74 @@ def isEmpty(self):
2311
return self.head == None
2412

2513
def insert(self, data):
26-
temp = Node(data)
27-
temp.setNextNode(self.head)
14+
# create a temp node
15+
temp = Node(data=data)
16+
# point new node to head
17+
temp.next_node = self.head
18+
# set the head as new node
2819
self.head = temp
2920

21+
def insert_after(self, prev, data):
22+
if prev is None:
23+
raise ValueError("Given node is not found...")
24+
return prev
25+
26+
# create a temp node
27+
temp = Node(data=data)
28+
# set next node of temp to the next node of previous
29+
temp.next_node = prev.next_node
30+
# set next node of previous to point temp
31+
prev.next_node = temp
32+
3033
def size(self):
34+
# start with the head
3135
current = self.head
3236
count = 0
37+
38+
# loop unless current is not None
3339
while current:
3440
count+=1
35-
current.getNextNode()
41+
current=current.next_node
3642
return count
3743

3844
def search(self, data):
45+
# start with the head
3946
current = self.head
4047
found = False
48+
49+
# loop unless current is not None
4150
while current and not found:
42-
if current.getData() == data:
51+
# if found, change flag and return data
52+
if current.data == data:
4353
found = True
4454
else:
45-
current = current.getNextNode()
55+
# change current to next node
56+
current = current.next_node
4657
if current is None:
58+
# raise Exception if not found
4759
raise ValueError("Data is not in the list")
4860
return current
4961

5062
def delete(self, data):
63+
# start with the head
5164
current = self.head
5265
previous = None
5366
found = False
67+
68+
# loop unless current is not None
5469
while current and not found:
70+
# if found, change flag
5571
if current.getData() == data:
5672
found = True
5773
else:
5874
previous = current
59-
current = current.getNextNode()
75+
current = current.next_node
76+
6077
if current is None:
78+
# raise Exception if not found
6179
raise ValueError("Data is not in the list")
6280
if previous is None:
63-
self.head = current.getNextNode()
81+
self.head = current.next_node
6482
else:
65-
previous.setNextNode(current.getNextNode())
83+
previous.next_node=current.next_node
6684

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /