Skip to content

Commit 0c18724

Browse files
committed
finished MVP of ring_buffer.py, though I'm not entirely satisfied with the solution
1 parent 045b6d7 commit 0c18724

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

ring_buffer/ring_buffer.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from doubly_linked_list import DoublyLinkedList
1+
from doubly_linked_list import DoublyLinkedList, ListNode
22

33

44
class RingBuffer:
@@ -8,14 +8,45 @@ def __init__(self, capacity):
88
self.storage = DoublyLinkedList()
99

1010
def append(self, item):
11-
pass
11+
if self.storage.length == 0:
12+
self.storage.add_to_head(item)
13+
self.current = self.storage.head
14+
elif self.storage.length < self.capacity:
15+
self.storage.add_to_tail(item)
16+
else:
17+
if self.current is self.storage.head:
18+
self.storage.remove_from_head()
19+
self.storage.add_to_head(item)
20+
if self.storage.head.next:
21+
self.current = self.storage.head.next
22+
else:
23+
self.current = self.storage.head
24+
elif self.current is self.storage.tail:
25+
self.storage.remove_from_tail()
26+
self.storage.add_to_tail(item)
27+
self.current = self.storage.head
28+
else:
29+
current_next = self.current.next
30+
current_prev = self.current.prev
31+
self.storage.delete(self.current)
32+
new_node = ListNode(item)
33+
new_node.next = current_next
34+
new_node.prev = current_prev
35+
current_next.prev = new_node
36+
current_prev.next = new_node
37+
self.storage.length += 1
38+
self.current = new_node.next
1239

1340
def get(self):
1441
# Note: This is the only [] allowed
1542
list_buffer_contents = []
1643

1744
# TODO: Your code here
18-
45+
node = self.storage.head
46+
list_buffer_contents.append(node.value)
47+
while node.next:
48+
node = node.next
49+
list_buffer_contents.append(node.value)
1950
return list_buffer_contents
2051

2152
# ----------------Stretch Goal-------------------

0 commit comments

Comments
 (0)