Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Completed ring_buffer. Working on names.
  • Loading branch information
Wsoukkachang committed Jan 11, 2020
commit c0b85f32ead45771aca4d389da506a59011101d2
30 changes: 24 additions & 6 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,36 @@ def __init__(self, capacity):
self.storage = DoublyLinkedList()

def append(self, item):
self.storage[self.current] = item
self.current += 1
self.current = self.current % len(self.storage)
if self.storage.length < self.capacity:
self.storage.add_to_tail(item)
self.current = self.storage.head
elif self.storage.length == self.capacity:
to_delete = self.storage.head
self.storage.remove_from_head()
self.storage.add_to_tail(item)
if to_delete == self.current:
self.current = self.storage.tail

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

# TODO: Your code here
for index in self.storage:
if index != None:
list_buffer_contents.append(index)
int_node = self.current
list_buffer_contents.append(int_node.value)

if int_node.next is not None:
next_node = int_node.next
else:
next_node = self.storage.head

while next_node != int_node:
list_buffer_contents.append(next_node.value)
if next_node.next is not None:
next_node = next_node.next
else:
next_node = self.storage.head

return list_buffer_contents

# ----------------Stretch Goal-------------------
Expand Down