-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathll.py
More file actions
71 lines (60 loc) · 1.32 KB
/
ll.py
File metadata and controls
71 lines (60 loc) · 1.32 KB
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
class node:
def __init__(self, inData):
self.data = inData
self.next = None
class linkedList:
def __init__(self):
self.head = None
self.tail = None
def contains(self, target):
pointer = self.head
while pointer:
if pointer.data == target:
return True
pointer = pointer.next
return False
def add(self, data):
if self.head == None:
self.head = node(data)
self.tail = self.head
else:
self.tail.next = node(data)
self.tail = self.tail.next
def remove(self, target):
if self.head:
if self.head.data == target:
self.head = self.head.next
return
pointer = self.head
while pointer.next:
if pointer.next.data == target:
pointer.next = pointer.next.next
return
pointer = pointer.next
def printList(self):
pointer = self.head
while pointer:
print(pointer.data, end="")
pointer = pointer.next
print()
print("build the list")
list = linkedList()
for i in range(8):
list.add(i)
list.printList()
print("remove 4")
list.remove(4)
list.printList()
print("remove 7")
list.remove(7)
list.printList()
print("remove 0")
list.remove(0)
list.printList()
print("remove 9")
list.remove(9)
list.printList()
print("contains 1:",list.contains(1))
print("contains 6:",list.contains(6))
print("contains 3:",list.contains(3))
print("contains 4:",list.contains(4))