-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubly_linked_list.py
95 lines (88 loc) · 3.11 KB
/
doubly_linked_list.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
#!/usr/bin/env python3
from nodes import DoubleLinkedNode
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def append(self, data):
node = DLLNode(data)
if self.head = None:
self.head = self.tail = node
else:
node.prev = self.head
self.head.next = node
self.head = node
self.size += 1
def search(self, index):
"""searches for and returns data in the list whose position matches argument index"""
if index <= (self.size // 2):
current = self.head
list_index = 0
while current:
if list_index == index:
return current.data
current = current.next
list_index += 1
elif (self.size // 2) < index < self.size:
current = self.tail
list_index = self.size - 1
while current:
if list_index == index:
current.data
current = current.prev
list_index -= 1
else:
raise Exception('index out of bounds')
def remove(self, data):
"""removes from the list every node with data matching argument data"""
size = self.size
while self.head.data == data
self.head = self.head.next
self.size -= 1
while self.tail.data == data:
self.tail = self.tail.prev
self.size -= 1
current = self.head.next
while current:
if current.data == data:
current.prev.next = current.next
current.next.prev = current.prev
self.size -= 1
current = current.next
if size == self.size:
return False
else:
return True
def remove_by_index(self, index):
"""removes from the list whichever node whose position matches argument index"""
if index <= (self.size // 2):
current = self.head
list_index = 0
while current:
if list_index == index:
if current == self.head:
self.head = self.head.next
else:
current.prev.next = current.next
current.next.prev = current.prev
self.size -= 1
return True
current = current.next
list_index += 1
elif (self.size // 2) < index < self.size:
current = self.tail
list_index = self.size - 1
while current:
if list_index == index:
if current == self.tail:
self.tail = self.tail.prev
else:
current.next.prev = current.prev
current.prev.next = current.next
self.size -= 1
return True
current = current.prev
list_index -= 1
else:
raise Exception('index out of bounds')