-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.py
More file actions
145 lines (135 loc) · 3.92 KB
/
DoublyLinkedList.py
File metadata and controls
145 lines (135 loc) · 3.92 KB
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
class Node():
def __init__(self, value):
self.value = value
self.next = None
self.prev = None
class DoublyLinkedList():
def __init__(self):
self.head = None
self.tail = None
self.length = 0
def __repr__(self):
if self.length > 0:
currentNode = self.head
outputString = '<-> '
for i in range(0, self.length):
outputString += str(currentNode.value) + ' <-> '
currentNode = currentNode.next
return str(self.head.value) + ' | ' + str(self.tail.value) + ' | ' + str(self.length) + ' | ' + outputString
else:
return '## | ## | 0 | '
def push(self, value):
newNode = Node(value)
if self.length == 0:
self.head = newNode
self.tail = newNode
else:
self.tail.next = newNode
newNode.prev = self.tail
self.tail = self.tail.next
self.length +=1
def pop(self):
if self.length == 1:
returnValue = self.head.value
self.head = None
self.tail = None
elif self.length > 1:
returnValue = self.tail.value
lastNode = self.tail
self.tail = self.tail.prev
lastNode.prev = None
self.tail.next = None
else:
return None
self.length -= 1
return returnValue
def shift(self):
firstNode = self.head
if self.length == 1:
self.head = None
self.tail = None
elif self.length > 1:
self.head = self.head.next
self.head.prev = None
firstNode.next = None
self.length -= 1
return firstNode.value
def unshift(self, value):
newNode = Node(value)
if self.length == 0:
self.head = newNode
self.tail = newNode
newNode.next = self.head
self.head.prev = newNode
self.head = newNode
self.length += 1
def get(self, position):
if position >=0 and position < self.length:
midpoint = self.length // 2
if position < midpoint:
startPoint = self.head
for i in range(0, position):
startPoint = startPoint.next
else:
startPoint = self.tail
for i in range(0, self.length - position -1):
startPoint = startPoint.prev
return startPoint.value
else:
return None
def set(self, position, value):
if position >=0 and position < self.length:
midpoint = self.length // 2
if position < midpoint:
startPoint = self.head
for i in range(0, position):
startPoint = startPoint.next
else:
startPoint = self.tail
for i in range(0, self.length - position -1):
startPoint = startPoint.prev
startPoint.value = value
def insert(self, position, value):
if position == 0:
self.unshift(value)
elif position == self.length:
self.push(value)
elif position > 0 and position < self.length:
newNode = Node(value)
midpoint = self.length // 2
if position < midpoint:
startPoint = self.head
for i in range(0, position):
startPoint = startPoint.next
else:
startPoint = self.tail
for i in range(0, self.length - position -1):
startPoint = startPoint.prev
prevNode = startPoint.prev
prevNode.next = newNode
newNode.prev = prevNode
newNode.next = startPoint
startPoint.prev = newNode
self.length += 1
def remove(self, position):
if position == 0:
self.shift()
elif position == self.length-1:
self.pop()
elif position > 0 and position < self.length-1:
midpoint = self.length // 2
if position < midpoint:
startPoint = self.head
for i in range(0, position):
startPoint = startPoint.next
else:
startPoint = self.tail
for i in range(0, self.length - position -1):
startPoint = startPoint.prev
prevNode = startPoint.prev
nextNode = startPoint.next
nextNode.prev = prevNode
prevNode.next = nextNode
startPoint.next = None
startPoint.prev = None
self.length -= 1