Skip to content

Commit bffeb90

Browse files
author
Amogh Singhal
authored
Create linked_list_data_structure.py
1 parent cafa650 commit bffeb90

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

linked_list_data_structure.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Node:
2+
def __init__(self, data=None):
3+
self.data = data
4+
self.next_node = None
5+
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+
18+
class LinkedList:
19+
def __init__(self, head=None):
20+
self.head = head
21+
22+
def isEmpty(self):
23+
return self.head == None
24+
25+
def insert(self, data):
26+
temp = Node(data)
27+
temp.setNextNode(self.head)
28+
self.head = temp
29+
30+
def size(self):
31+
current = self.head
32+
count = 0
33+
while current:
34+
count+=1
35+
current.getNextNode()
36+
return count
37+
38+
def search(self, data):
39+
current = self.head
40+
found = False
41+
while current and not found:
42+
if current.getData() == data:
43+
found = True
44+
else:
45+
current = current.getNextNode()
46+
if current is None:
47+
raise ValueError("Data is not in the list")
48+
return current
49+
50+
def delete(self, data):
51+
current = self.head
52+
previous = None
53+
found = False
54+
while current and not found:
55+
if current.getData() == data:
56+
found = True
57+
else:
58+
previous = current
59+
current = current.getNextNode()
60+
if current is None:
61+
raise ValueError("Data is not in the list")
62+
if previous is None:
63+
self.head = current.getNextNode()
64+
else:
65+
previous.setNextNode(current.getNextNode())
66+

0 commit comments

Comments
 (0)