-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list_remove_nodes.py
126 lines (104 loc) · 3.15 KB
/
linked_list_remove_nodes.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
class LinkedList(object):
class Node(object):
def __init__(self, item, prev_item, next_item):
self.next_item = next_item
self.prev_item = prev_item
self.item = item
def __str__(self):
return "({0} <- {1} -> {2})".format(self.prev_item
if self.prev_item is None else self.prev_item.item,
self.item,
self.next_item
if self.next_item is None else self.next_item.item)
class LinkedListIterator(object):
def __init__(self, linked_list):
self.linked_list = linked_list
self.current = linked_list.first_node
def __iter__(self):
return self
def next(self):
if self.linked_list.is_empty():
raise StopIteration
else:
if self.current is None:
raise StopIteration
res = self.current
self.current = self.current.next_item
return res
def has_next(self):
return self.current.next_item is not None
def __iter__(self):
return LinkedList.LinkedListIterator(self)
def __init__(self):
self.first_node = None
self.end_node = None
def index(self):
pass
def insert(self, i, x):
pass
def remove(self, x):
if self.is_empty():
return
else:
current = self.first_node
while True:
if current.item == x:
if current.prev_item is None:
current.next_item.prev_item = None
elif current.next_item is None:
current.prev_item.next_item = None
else:
current.prev_item.next_item = current.next_item
current.next_item.prev_item = current.prev_item
if current.next_item is None:
break
else:
current = current.next_item
def append(self, x):
if self.first_node is None and self.end_node is None:
self.first_node = self.end_node = LinkedList.Node(x, None, None)
else:
new_item = LinkedList.Node(x, self.end_node, None)
self.end_node.next_item = new_item
self.end_node = new_item
def reverse(self):
pass
def pop(self, i=-1):
pass
def extend(self, t):
pass
def count(self, x):
pass
def sort(self, cmp=None, key=None, reverse=False):
pass
def is_empty(self):
return self.first_node is None and self.end_node is None
def __str__(self):
res = ""
for i in self:
res += str(i) + "\n"
return res
ll = LinkedList()
ll.append(1)
ll.append(1)
ll.append(1)
ll.append(2)
ll.append(3)
ll.append(1)
ll.append(1)
ll.append(4)
ll.append(5)
ll.append(5)
ll.append(1)
ll.append(5)
ll.append(6)
ll.append(6)
ll.append(6)
print ll
#
# ll.print_list()
# print ""
#
# ll.remove(1)
#
# ll.print_list()