Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a96b354

Browse files
Added files via upload
Linked list example on python
1 parent a5c2bcd commit a96b354

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

linkedlist.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
class Node:
2+
def __init__ (self, data):
3+
self.data = data
4+
self.next = None
5+
6+
def getData(self):
7+
return self.data
8+
9+
def getNext(self):
10+
return self.next
11+
12+
def setData(self):
13+
self.data = newdata
14+
15+
def setNext(self, newnext):
16+
self.next = newnext
17+
18+
class linkedList:
19+
20+
def __init__(self):
21+
self.head = None
22+
23+
def isEmpty (self):
24+
return self.head == None
25+
26+
def add(self,item):
27+
temp = Node(item)
28+
temp.setNext(self.head)
29+
self.head = temp
30+
31+
def size(self):
32+
current = self.head
33+
count = 0
34+
while current!= None:
35+
count = count + 1
36+
current = current.getNext()
37+
return count
38+
39+
def search(self, item):
40+
current = self.head
41+
found = False
42+
while current != None and found != True:
43+
if current.getData() == item:
44+
found = True
45+
else:
46+
current = current.getNext()
47+
return found
48+
49+
def delete(self, item):
50+
current = self.head
51+
previous = None
52+
found = False
53+
while current != None and not found:
54+
if current.getData()==item:
55+
found = True
56+
else:
57+
previous = current
58+
current= current.getNext()
59+
if previous == None:
60+
self.head = current.getNext()
61+
else:
62+
previous.setNext(current.getNext())
63+
64+
myList = linkedList()
65+
myList.add(23)
66+
myList.add(13)
67+
myList.add(3)
68+
myList.add(99)
69+
myList.add(12)
70+
myList.add(18)
71+
myList.add(67)
72+
myList.add(55)
73+
myList.add(43)
74+
75+
76+
print myList.size()
77+
print myList.search(26)
78+
79+
80+
81+
82+
83+
84+

0 commit comments

Comments
 (0)