-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.py
More file actions
48 lines (39 loc) · 1.01 KB
/
SinglyLinkedList.py
File metadata and controls
48 lines (39 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Node:
def __init__(self, data = None, next = None):
self.data = data
self.next = None
def get_data(self):
return self.data
def set_next(self, next):
self.next = next
def get_next(self):
return self.next
class LinkedList:
def __init__(self, head):
self.head = head
def insert(self, data):
new_node = Node(data)
new_node.set_next(self.head)
self.head = new_node
def search(self, data):
index = 0
found = False
current = self.head
while found==False:
if(current.get_data()==data):
found = True
else :
current = current.get_next()
index+=1
return str(data)+" found at index "+str(index)
head_node = Node("a")
llist = LinkedList(head_node)
llist.insert("b")
llist.insert("c")
llist.insert("d")
llist.insert("e")
llist.insert("f")
llist.insert("g")
llist.insert("h")
llist.insert("i")
print(llist.search("f"))