def test_delete(self): dll = DoublyLinkedList(['A', 'B', 'C']) dll.delete('A') assert dll.head.data == 'B' # new head assert dll.tail.data == 'C' # unchanged assert dll.head.previous == None assert dll.tail.next == None assert dll.head.next is dll.tail assert dll.head is dll.tail.previous assert dll.size == 2 dll.delete('C') assert dll.head.data == 'B' # unchanged assert dll.tail.data == 'B' # new tail assert dll.head.next == None assert dll.tail.next == None assert dll.head.previous == None assert dll.tail.previous == None assert dll.size == 1 dll.delete('B') assert dll.head is None # new head assert dll.tail is None # new head assert dll.size == 0 with self.assertRaises(ValueError): dll.delete('X') # item not in list
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 test_prepend(self): dll = DoublyLinkedList() dll.prepend('C') assert dll.head.data == 'C' # new head assert dll.tail.data == 'C' # new head assert dll.head.previous == None assert dll.tail.next == None assert dll.size == 1 dll.prepend('B') assert dll.head.data == 'B' # new head assert dll.tail.data == 'C' # unchanged assert dll.head.previous == None assert dll.tail.next == None assert dll.head.next is dll.tail assert dll.tail.previous is dll.head assert dll.size == 2 dll.prepend('A') assert dll.head.data == 'A' # new head assert dll.tail.data == 'C' # unchanged assert dll.size == 3 assert dll.head.previous == None assert dll.tail.next == None assert dll.head.next is dll.tail.previous assert dll.head.next.next is dll.tail assert dll.tail.previous.previous is dll.head assert dll.size == 3
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_delete(self): ll = DoublyLinkedList(['A', 'B', 'C']) ll.delete('A') assert ll.head.data == 'B' # new head assert ll.tail.data == 'C' # unchanged assert ll.size == 2 ll.delete('C') assert ll.head.data == 'B' # unchanged assert ll.tail.data == 'B' # new tail assert ll.size == 1 ll.delete('B') assert ll.head is None # new head assert ll.tail is None # new head assert ll.size == 0 with self.assertRaises(ValueError): ll.delete('X') # item not in list
def test_replace(self): dll = DoublyLinkedList(['A', 'B', 'C']) dll.replace('A', 'D') assert dll.head.data == 'D' # new head assert dll.tail.data == 'C' # unchanged assert dll.size == 3 dll.replace('B', 'E') assert dll.head.data == 'D' # unchanged assert dll.tail.data == 'C' # unchanged assert dll.size == 3 dll.replace('C', 'F') assert dll.head.data == 'D' # unchanged assert dll.tail.data == 'F' # new tail assert dll.size == 3 with self.assertRaises(ValueError): dll.replace('X', 'Y') # item not in list
def test_get_at_index(self): dll = DoublyLinkedList(['A', 'B', 'C']) assert dll.get_at_index(0) == 'A' # head item assert dll.get_at_index(1) == 'B' # middle item assert dll.get_at_index(2) == 'C' # tail item with self.assertRaises(ValueError): dll.get_at_index(3) # index too high with self.assertRaises(ValueError): dll.get_at_index(-1) # index too low
def test_init_with_list(self): dll = DoublyLinkedList(['A', 'B', 'C']) assert dll.head.data == 'A' # first item assert dll.tail.data == 'C' # last item assert dll.head.next.data == 'B' assert dll.head.previous == None assert dll.tail.previous.data == 'B' assert dll.tail.next == None assert dll.size == 3
def test_prepend(self): ll = DoublyLinkedList() ll.prepend('C') assert ll.head.data == 'C' # new head assert ll.tail.data == 'C' # new head assert ll.size == 1 ll.prepend('B') assert ll.head.data == 'B' # new head assert ll.tail.data == 'C' # unchanged assert ll.size == 2 ll.prepend('A') assert ll.head.data == 'A' # new head assert ll.tail.data == 'C' # unchanged assert ll.size == 3
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_delete_index(self): ll = DoublyLinkedList(['A', 'B', 'C', 'D', 'E']) assert ll.size == 5 ll.delete_index(4) assert ll.head.data == 'A' assert ll.tail.data == 'D' assert ll.size == 4 ll.delete_index(0) assert ll.head.data == 'B' assert ll.tail.data == 'D' assert ll.size == 3 ll.delete_index(1) assert ll.head.data == 'B' assert ll.tail.data == 'D' assert ll.size == 2
def test_insert_at_index(self): dll = DoublyLinkedList() dll.insert_at_index(0, 'B') # append('B') assert dll.head.data == 'B' # new head (at index 0) assert dll.tail.data == 'B' # new tail (at index 0) assert dll.size == 1 dll.insert_at_index(0, 'A') # prepend('A') assert dll.head.data == 'A' # new head (at index 0) assert dll.tail.data == 'B' # unchanged (now at index 1) assert dll.size == 2 dll.insert_at_index(2, 'D') # append('D') assert dll.head.data == 'A' # unchanged (at index 0) assert dll.tail.data == 'D' # new tail (now at index 2) assert dll.size == 3 dll.insert_at_index(2, 'C') # insert 'C' between 'B' and 'D' assert dll.head.data == 'A' # unchanged (at index 0) assert dll.tail.data == 'D' # unchanged (now at index 3) assert dll.size == 4 with self.assertRaises(ValueError): dll.insert_at_index(5, 'X') # index too high with self.assertRaises(ValueError): dll.insert_at_index(-1, 'Y') # index too low
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_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_init_with_list(self): ll = DoublyLinkedList(['A', 'B', 'C']) assert ll.head.data == 'A' # first item assert ll.tail.data == 'C' # last item assert ll.size == 3
def test_find(self): dll = DoublyLinkedList(['A', 'B', 'C']) assert dll.find(lambda item: item == 'B') == 'B' assert dll.find(lambda item: item < 'B') == 'A' assert dll.find(lambda item: item > 'B') == 'C' assert dll.find(lambda item: item == 'X') is None
def test_init(self): dll = DoublyLinkedList() assert dll.head is None assert dll.tail is None assert dll.size == 0
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