-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordered_list.py
More file actions
41 lines (27 loc) · 777 Bytes
/
ordered_list.py
File metadata and controls
41 lines (27 loc) · 777 Bytes
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
class OrderedList:
def __init__(self):
self.list = []
def insert(self, item, ord):
size = len(self.list)
idx = 0
while (idx < size and self.list[idx].ord < ord):
idx += 1
self.list[idx:idx] = [ListItem(item, ord)]
def remove(self, item):
size = len(self.list)
idx = 0
while (idx < size and self.list[idx].item != item):
idx += 1
if idx < size:
self.list[idx:idx+1] = []
def head(self):
return self.list[0] if self.list else None
def pop(self):
if self.list:
return self.list.pop(0)
class ListItem:
def __init__(self, item, ord):
self.item = item
self.ord = ord
def __eq__(a, b):
return a.item == b.item and a.ord == b.ord