-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.py
142 lines (119 loc) · 3.71 KB
/
linkedlist.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!python
from __future__ import print_function
class Node(object):
def __init__(self, data):
"""Initialize this node with the given data"""
self.data = data
self.next = None
def __repr__(self):
"""Return a string representation of this node"""
return 'Node({})'.format(repr(self.data))
class LinkedList(object):
def __init__(self, iterable=None):
"""Initialize this linked list; append the given items, if any"""
self.head = None
self.tail = None
if iterable:
for item in iterable:
self.append(item)
def __repr__(self):
"""Return a string representation of this linked list"""
return 'LinkedList({})'.format(self.as_list())
def as_list(self):
"""Return a list of all items in this linked list"""
result = []
current = self.head
while current is not None:
result.append(current.data)
# result.append(current)
current = current.next
return result
def is_empty(self):
"""Return True if this linked list is empty, or False"""
return self.head is None
def length(self):
"""Return the length of this linked list by traversing its nodes"""
# TODO: count number of items
count = 0
node = self.head
while True:
if node is not None:
count += 1
node = node.next
else:
return count
pass
def append(self, item):
"""Insert the given item at the tail of this linked list"""
# TODO: append given item
new_node = Node(item)
if self.is_empty():
self.head = new_node
else:
self.tail.next = new_node
self.tail = new_node
pass
def prepend(self, item):
"""Insert the given item at the head of this linked list"""
# TODO: prepend given item
new_node = Node(item)
if self.is_empty():
self.tail = new_node
else:
new_node.next = self.head
self.head = new_node
pass
def delete(self, item):
"""Delete the given item from this linked list, or raise ValueError"""
# TODO: find given item and delete if found
node = self.head
while node is not None:
next_node = node.next
if self.head.data == item:
if self.head == self.tail:
self.head = None
self.tail = None
else:
self.head = self.head.next
return
elif next_node.data == item:
if next_node == self.tail:
self.tail = node
else:
node.next = next_node.next
return
node = node.next
raise ValueError("Error")
pass
def find(self, quality):
"""Return an item from this linked list satisfying the given quality"""
# TODO: find item where quality(item) is True
node = self.head
while node is not None:
if quality(node.data):
return node.data
node = node.next
pass
def test_linked_list():
ll = LinkedList()
print(ll)
ll.append('A')
print(ll)
ll.append('B')
print(ll)
ll.append('C')
print(ll)
print('head: ' + str(ll.head))
print('tail: ' + str(ll.tail))
print(ll.length())
ll.delete('A')
print(ll)
ll.delete('C')
print(ll)
ll.delete('B')
print(ll)
print('head: ' + str(ll.head))
print('tail: ' + str(ll.tail))
print(ll.length())
if __name__ == '__main__':
test_linked_list()