-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathList_1.py
105 lines (83 loc) · 2.4 KB
/
List_1.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class Node:
def __init__(self,init_data):
self.data = init_data
self.next = None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_data(self, new_data):
self.data = new_data
def set_next(self, new_next):
self.next = new_next
class UnorderedList:
def __init__(self):
self.head = None
def is_empty(self):
return self.head == None
def add(self, item):
temp = Node(item)
temp.next = self.head
self.head = temp
def size(self):
count = 0
current = self.head
while current != None:
count += 1
current = current.get_next()
return count
def search(self, item):
loc = None
count = 0
found = False
current = self.head
while current != None and not found:
if item == current.get_data():
found = True
loc = count
current = current.get_next()
count += 1
if found:
return str("item found at position %r"%(loc))
else:
return "item not found"
def remove(self, item):
prev = None
found = False
current = self.head
while current != None and not found:
if item == current.get_data():
found = True
else:
prev = current
current = current.get_next()
if prev == None:
self.head = current.get_next()
else:
prev.set_next(current.get_next())
def append(self, item):
prev = None
found = False
current = self.head
while current != None and not found:
prev = current
current = current.get_next()
temp = Node(item)
prev.set_next(temp)
list1 = UnorderedList()
print list1.is_empty()
list1.add(31)
list1.add(77)
list1.add(17)
print list1.size()
list1.add(93)
list1.add(54)
print list1.size()
print list1.remove(31)
print list1.size()
print list1.remove(54)
print list1.size()
list1.append(73)
print list1.size()
list1.append(799)
print list1.size()