def test_length(self): ll = DoublyLinkedList() assert ll.length() == 0 # append and prepend operations increase length ll.append('B') assert ll.length() == 1 ll.prepend('A') assert ll.length() == 2 ll.append('C') assert ll.length() == 3 # delete operations decrease length # assert(ll) == DoublyLinkedList(['A', 'B', 'C']) assert(ll.head.data) == 'A' assert(ll.head.prev) == None assert(ll.head.next.prev.data) == 'A' assert(ll.tail.data) == 'C' assert(ll.tail.prev.data) == 'B' assert(ll.tail.next) == None ll.delete('B') assert ll.length() == 2 ll.delete('C') assert ll.length() == 1 ll.delete('A') assert ll.length() == 0
class LinkedDeque(object): '''double-ended queue''' def __init__(self, iterable=None): """Initialize this queue and enqueue the given items, if any.""" self.list = DoublyLinkedList() self.size = 0 if iterable is not None: for item in iterable: self.push_back(item) def __repr__(self): """Return a string representation of this deque.""" return 'Deque({} items, front={})'.format(self.length(), self.front()) def is_empty(self): """Return True if this queue is empty, or False otherwise.""" # runtime O(1) checking the value if self.size == 0: return True else: return False def length(self): """Return the number of items in the deque""" # runtime O(1) retrieving a variable return self.size def push_front(self, item): """Insert item at front of deque""" # runtime O(n) having to shift all the slots in the array back self.list.prepend(item) self.size += 1 def push_back(self, item): """Insert item at back of the deque""" # runtime O(1)*average self.list.append(item) self.size += 1 def front(self): """Returns item at front of deque""" # runtime O(1) retrieving value at an index return self.list.head.data def back(self): """Returns item at back of the deque""" # runtime O(1) retrieving value at an index return self.list.tail.data def pop_front(self): """Remove and return the item at the front of the deque""" # runtime O(n) having to shift later elements up the list self.size -= 1 return self.list.delete_index(0) def pop_back(self): """Remove and return the item at the back of the deque""" # runtime O(1) removing last element doesnt have effect on other elements self.size -= 1 return self.list.delete_index(self.size)
def test_items(self): dll = DoublyLinkedList() assert dll.items() == [] dll.append('B') assert dll.items() == ['B'] dll.prepend('A') assert dll.items() == ['A', 'B'] dll.append('C') assert dll.items() == ['A', 'B', 'C']
def test_size(self): dll = DoublyLinkedList() assert dll.size == 0 # append and prepend operations increment size dll.append('B') assert dll.size == 1 dll.prepend('A') assert dll.size == 2 dll.append('C') assert dll.size == 3 # delete operations decrement size dll.delete('B') assert dll.size == 2 dll.delete('C') assert dll.size == 1 dll.delete('A') assert dll.size == 0
def test_length(self): dll = DoublyLinkedList() assert dll.length() == 0 # append and prepend operations increase length dll.append('B') assert dll.length() == 1 dll.prepend('A') assert dll.length() == 2 dll.append('C') assert dll.length() == 3 # delete operations decrease length dll.delete('B') assert dll.length() == 2 dll.delete('C') assert dll.length() == 1 dll.delete('A') assert dll.length() == 0
def test_append(self): dll = DoublyLinkedList() dll.append('A') assert dll.head.data == 'A' # new head assert dll.tail.data == 'A' # new tail assert dll.size == 1 dll.append('B') assert dll.head.data == 'A' # unchanged assert dll.tail.data == 'B' # new tail # checking to see if pointers have the same memory address assert dll.head.next is dll.tail assert dll.head is dll.tail.previous assert dll.head.previous == None assert dll.tail.next == None assert dll.size == 2 dll.append('C') assert dll.head.data == 'A' # unchanged assert dll.tail.data == 'C' # new tail assert dll.head.next.next is dll.tail # two links right assert dll.head.next is dll.tail.previous # one link right, one link left assert dll.head is dll.tail.previous.previous # two links left assert dll.head.previous == None assert dll.tail.next == None assert dll.size == 3
def test_append(self): ll = DoublyLinkedList() ll.append('A') assert ll.head.data == 'A' # new head assert ll.tail.data == 'A' # new tail assert ll.size == 1 ll.append('B') assert ll.head.data == 'A' # unchanged assert ll.tail.data == 'B' # new tail assert ll.size == 2 ll.append('C') assert ll.head.data == 'A' # unchanged assert ll.tail.data == 'C' # new tail assert ll.size == 3