-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyList.py
75 lines (64 loc) · 1.39 KB
/
SinglyList.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
###########
#SINGLYLIST
class SinglyList(object):
def __init__(self):
self.h = None
def __iter__(self):
current = self.head
while current:
yield current
current = current.next
@property
def head(self):
return self.h
@head.setter
def head(self, val):
self.h = val
def isEmpty(self):
return self.head == None
def print_list(self):
for item in self:
print(item.c)
def add_head(self, node):
if self.isEmpty():
self.head = node
else:
node.next = self.head
self.head = node
def search(self, val):
if (self.isEmpty()):
return (False)
for item in self:
if (item.content == val):
return (True)
return (False)
def add_tail(self, node):
if (self.isEmpty()):
self.head = node
else:
for item in self:
current = item
current.next = node
def size(self):
count = 0
for _ in self:
count += 1
return (count)
def remove(self, val):
previous = None
for item in self:
if (item.content == val):
if (previous):
previous.next = item.next
break
else:
self.head = item.next
previous = item
def cycles(self):
itr = self.head
for item in self:
if (itr.next):
itr = itr.next.next
if (itr == item):
return (True)
return (False)