-
Notifications
You must be signed in to change notification settings - Fork 0
/
Singly linked list.py
70 lines (58 loc) · 1.48 KB
/
Singly linked list.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
class bucket:
def __init__(self, add):
self.data = add
self.next = None
class linkedlist:
def __init__(self):
self.head = None
def insert(self, new_bucket, point):
if point == 'end':
if self.head != None:
last_bucket = self.head
while last_bucket.next != None:
last_bucket = last_bucket.next
last_bucket.next = new_bucket
else:
self.head = new_bucket
if point != 'end':
last_bucket = self.head
for i in range(0 , (point - 2)):
last_bucket = last_bucket.next
new_bucket.next = last_bucket.next
last_bucket.next = new_bucket
def count(self):
temp_bucket = self.head
i = 0
while temp_bucket != None:
temp_bucket = temp_bucket.next
i = i + 1
print(" ",{i})
def printlist(self):
temp_bucket = self.head
print("[", end="")
while temp_bucket != None:
print(temp_bucket.data , end="")
if temp_bucket.next != None:
print(",", end=" ")
temp_bucket = temp_bucket.next
print("]")
def check(self, tocheck):
temp_bucket = self.head
while temp_bucket != None:
if temp_bucket.data == tocheck:
print("Exists")
temp_bucket = temp_bucket.next
break
else:
temp_bucket = temp_bucket.next
if __name__ == '__main__':
newlist = linkedlist()
newlist.insert(bucket('s'), 'end')
newlist.insert(bucket('a'), 'end')
newlist.insert(bucket('n'), 'end')
newlist.insert(bucket('d'), 'end')
newlist.insert(bucket('u'), 'end')
newlist.insert(bucket('h'), 5)
newlist.count()
newlist.printlist()
newlist.check('h')