-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list_brute force.py
75 lines (62 loc) · 2.12 KB
/
linked_list_brute force.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Node:
def __init__(self,data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insertNode(self, nodeName):
if self.head is None:
self.head = nodeName
else:
lastNode = self.head
while True:
if lastNode.next is None:
break
else:
lastNode = lastNode.next
lastNode.next = nodeName
def printNodes(self):
currentNode = self.head
if currentNode is None:
print("linked list is empty")
else:
while True:
if currentNode is None:
break
else:
print(currentNode.data)
currentNode = currentNode.next
def search_data(self,search_element):
c = 1 #counter
f= 0 #flag 0 -> no element found 1-> if element is found
nodeNow = self.head
if nodeNow.data == search_element:
print("the element you want to find is the head of the linkedlist")
f=1
else:
nodeNow = nodeNow.next
c = c + 1
while True:
if nodeNow.data == search_element:
print("Linked list element no.")
print(c)
f = 1
nodeNow = nodeNow.next
c = c + 1
if nodeNow is None:
break
if f == 0:
print("No such element was found in the linked list")
print("There is a linked list with 3 names in 3 different Nodes",end='\n')
#Linked List is Mihir--> Anmol--> Aman
linkedList1 = LinkedList()
node1 = Node("Mihir")
linkedList1.insertNode(node1)
node2 = Node("Anmol")
linkedList1.insertNode(node2)
node3 = Node("Aman")
linkedList1.insertNode(node3)
linkedList1.printNodes()
####Creating a function that accepts an array 3 elements and puts each
####element into a linked list