-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinkedlist.py
205 lines (175 loc) · 5.57 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from node import Node
class LinkedList:
def __init__(self):
self.head: Node | None = None
self.tail: Node | None = None
self.size = 0
def __iter__(self):
node = self.head
while node:
yield node
node = node.next
def __len__(self):
return self.size
def __repr__(self):
current = self.head
node = []
while current:
node.append(current.data)
current = current.next
return "".join(map(str, node))
def isempty(self):
return self.head is None and self.tail is None
def append(self, node: Node):
if self.tail is None:
self.head = self.tail = node
else:
node.next = None
node.prev = self.tail
self.tail.next = node
self.tail = node
self.size += 1
def prepend(self, node: Node):
if self.head is None:
self.head = self.tail = node
else:
node.next = self.head
self.head = node
node.prev = None
self.size += 1
def insert_after_index(self, index: int, node: Node):
index += 1
if self.size == 0:
raise IndexError
elif index == 0:
self.prepend(node)
elif index == len(self) + 1:
self.append(node)
elif 0 < index < len(self) + 1:
count = 0
assert isinstance(self.head, Node)
head = self.head
while count < index - 2:
assert isinstance(head, Node)
head = head.next
count += 1
assert isinstance(head, Node)
node.next = head.next
head.next = node
self.size += 1
else:
raise IndexError
def sort_by_object(self):
pass
def pop_code(self, pcode: str):
pass
def search(self, code):
pass
def pop_after_code(self, code):
pass
class ProductLinkedList(LinkedList):
def __init__(self):
super().__init__()
def search(self, code):
current = self.head
while current != None:
if current.data._pcode == code:
return current.data
current = current.next
return False
def sort_by_object(self):
i = 0
n = len(self)
while i < n:
j = 0
cur = self.head
while j < n - i and cur.next:
if int(cur.data._pcode[1:]) > int(cur.next.data._pcode[1:]):
cur.data, cur.next.data = cur.next.data, cur.data
cur = cur.next
j += 1
i += 1
def pop_after_code(self, pcode: str):
if len(self) == 1 or self.head == None:
return
current = self.head
while current.next:
product = current.data
if product._pcode == pcode:
current.next = current.next.next
self.size -= 1
return product
current = current.next
def pop_code(self, pcode: str):
if len(self) == 1 or self.head == None:
return
if self.head.data._pcode == pcode:
temp = self.head
self.head = self.head.next
return temp
current = self.head
while current.next:
product = current.next
if product.data._pcode == pcode:
current.next = current.next.next
self.size -= 1
return product
current = current.next
class CustomerLinkedList(LinkedList):
def __init__(self):
super().__init__()
def search(self, code):
current = self.head
while current != None:
if current.data._ccode == code:
return current.data
current = current.next
return False
def pop_code(self, ccode: str):
if len(self) == 1 or self.head == None:
return
if self.head.data._ccode == ccode:
temp = self.head
self.head = self.head.next
return temp
current = self.head
while current.next:
customer = current.next
if customer.data._ccode == ccode:
current.next = current.next.next
self.size -= 1
return customer
current = current.next
class OrderingLinkedList(LinkedList):
def __init__(self):
super().__init__()
def search_ccode(self, code):
current = self.head
while current != None:
if current.data._ccode == code:
return current.data
current = current.next
return False
def search_pcode(self, code):
current = self.head
while current != None:
if current.data._pcode == code:
return current.data
current = current.next
return False
def sort_by_object(self):
i = 0
n = len(self)
while i < n:
j = 0
cur = self.head
assert isinstance(cur, Node)
while j < n - i and cur.next:
if int(cur.data._pcode[1:]) > int(cur.next.data._pcode[1:]):
cur.data, cur.next.data = cur.next.data, cur.data
elif int(cur.data._pcode[1:]) == int(cur.next.data._pcode[1:]):
if int(cur.data._ccode[1:]) > int(cur.next.data._ccode[1:]):
cur.data, cur.next.data = cur.next.data, cur.data
cur = cur.next
j += 1
i += 1