class DoublyLinkedList:
def __init__(self):
self.head = self.tail = None
def appendleft(self, data):
new_node = ListNode(data)
if not self.head:
self.head = self.tail = new_node
else:
new_node.next = self.head
self.head.prev = self.head = new_node
def append(self, data):
new_node = ListNode(data)
if not self.head:
self.head = self.tail = new_node
else:
new_node.prev = self.tail
self.tail.next = self.tail = new_node
def insert(self, data, pos):
if pos <= 0:
self.appendleft(data)
elif pos >= self.index(self.tail.data):
self.append(data)
else:
current = self.head
index = 0
while current and index < pos:
current = current.next
index += 1
new_node = ListNode(data)
new_node.prev, new_node.next = current.prev, current
current.prev.next = current.prev = new_node
def index(self, data):
current = self.head
index = 0
while current:
if current.data == data:
return index
current = current.next
index += 1
def popleft(self):
if self.head:
data = self.head.data
if self.head == self.tail:
self.head = self.tail = None
else:
self.head, self.head.prev = self.head.next, None
return data
def pop(self):
if self.head:
data = self.tail.data
if self.head == self.tail:
self.head = self.tail = None
else:
self.tail, self.tail.next = self.tail.prev, None
return data```
#๐ How can I modify this DLL to support negative indexing?
18 messages ยท Page 1 of 1 (latest)
@austere nymph
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
def remove(self, data):
if self.head:
current = self.head
found = False
while current and not found:
if current.data == data:
if current == self.head:
self.popleft()
elif current == self.tail:
self.pop()
else:
current.prev.next, current.next.prev = current.next, current.prev
found = True
current = current.next```
how do you want to add negative indexing
at the insert() function?
yeah
I'm assuming -1 would put it at the end?
yeah
hmm, you'll need to change the behaviour from self.appendleft into something else
oh btw I spotted a bug, elif pos >= self.index(self.tail.data): in insert, if data repeats then the index might be calculated wrongly
how do i fix that
instead of using .index to find the length, just write another method specifically for finding the length
as in the number of nodes
Yeah, number of nodes
- When writing your own structures, you should store a length (node count) internally so you don't need to re-calculate it each time. Otherwise, getting the length is very expensive.
- To change a negative index to a "normal" one, add the negative index to the length of the list.
like this? class Node:
def init(self, data):
self.data = data
self.prev = self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = self.tail = None
def __len__(self):
count = 0
current = self.head
while current:
count += 1
current = current.next
return count
def __getitem__(self, index):
if index < 0:
index += len(self)
if index < 0 or index >= len(self):
raise IndexError("Index out of range")
current = self.head
for _ in range(index):
current = current.next
return current.data
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = self.tail = new_node
else:
self.tail.next = new_node
new_node.prev = self.tail
self.tail = new_node
def appendleft(self, data):
new_node = Node(data)
if not self.head:
self.head = self.tail = new_node
else:
new_node.next = self.head
self.head.prev = self.head = new_node
def pop(self):
if not self.tail:
raise IndexError("List is empty")
data = self.tail.data
if self.head == self.tail:
self.head = self.tail = None
else:
self.tail = self.tail.prev
self.tail.next = None
return data
def popleft(self):
if not self.head:
raise IndexError("List is empty")
data = self.head.data
if self.head == self.tail:
self.head = self.tail = None
else:
self.head = self.head.next
self.head.prev = None
return data```
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.