-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.py
97 lines (76 loc) · 2.42 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
#!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
pass
def append(self, item):
"""Insert the given item at the tail of this linked list"""
# TODO: append given item
pass
def prepend(self, item):
"""Insert the given item at the head of this linked list"""
# TODO: prepend given item
pass
def delete(self, item):
"""Delete the given item from this linked list, or raise ValueError"""
# TODO: find given item and delete if found
pass
def find(self, quality):
"""Return an item from this linked list satisfying the given quality"""
# TODO: find item where quality(item) is True
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()