Example #1
0
 def __init__(self, iterable=None):
     """Initialize this deque and push_back the given items, if any."""
     # Initialize a new linked list to store the items
     self.list = LinkedList()
     if iterable is not None:
         for item in iterable:
             self.push_back(item)
Example #2
0
 def test_delete(self):
     ll = LinkedList(['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
Example #3
0
 def test_replace(self):
     ll = LinkedList(['A', 'B', 'C'])
     ll.replace('A', 'D')
     assert ll.head.data == 'D'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 3
     ll.replace('B', 'E')
     assert ll.head.data == 'D'  # unchanged
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 3
     ll.replace('C', 'F')
     assert ll.head.data == 'D'  # unchanged
     assert ll.tail.data == 'F'  # new tail
     assert ll.size == 3
     with self.assertRaises(ValueError):
         ll.replace('X', 'Y')  # item not in list
Example #4
0
 def test_get_at_index(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.get_at_index(0) == 'A'  # head item
     assert ll.get_at_index(1) == 'B'  # middle item
     assert ll.get_at_index(2) == 'C'  # tail item
     with self.assertRaises(ValueError):
         ll.get_at_index(3)  # index too high
     with self.assertRaises(ValueError):
         ll.get_at_index(-1)  # index too low
Example #5
0
 def test_prepend(self):
     ll = LinkedList()
     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
Example #6
0
 def test_size(self):
     ll = LinkedList()
     assert ll.size == 0
     # append and prepend operations increment size
     ll.append('B')
     assert ll.size == 1
     ll.prepend('A')
     assert ll.size == 2
     ll.append('C')
     assert ll.size == 3
     # delete operations decrement size
     ll.delete('B')
     assert ll.size == 2
     ll.delete('C')
     assert ll.size == 1
     ll.delete('A')
     assert ll.size == 0
Example #7
0
 def test_length(self):
     ll = LinkedList()
     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
     ll.delete('B')
     assert ll.length() == 2
     ll.delete('C')
     assert ll.length() == 1
     ll.delete('A')
     assert ll.length() == 0
Example #8
0
 def test_items(self):
     ll = LinkedList()
     assert ll.items() == []
     ll.append('B')
     assert ll.items() == ['B']
     ll.prepend('A')
     assert ll.items() == ['A', 'B']
     ll.append('C')
     assert ll.items() == ['A', 'B', 'C']
Example #9
0
 def test_init_with_list(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.head.data == 'A'  # first item
     assert ll.tail.data == 'C'  # last item
     assert ll.size == 3
Example #10
0
 def test_init(self):
     ll = LinkedList()
     assert ll.head is None
     assert ll.tail is None
     assert ll.size == 0
Example #11
0
 def test_insert_at_index(self):
     ll = LinkedList()
     ll.insert_at_index(0, 'B')  # append('B')
     assert ll.head.data == 'B'  # new head (at index 0)
     assert ll.tail.data == 'B'  # new tail (at index 0)
     assert ll.size == 1
     ll.insert_at_index(0, 'A')  # prepend('A')
     assert ll.head.data == 'A'  # new head (at index 0)
     assert ll.tail.data == 'B'  # unchanged (now at index 1)
     assert ll.size == 2
     ll.insert_at_index(2, 'D')  # append('D')
     assert ll.head.data == 'A'  # unchanged (at index 0)
     assert ll.tail.data == 'D'  # new tail (now at index 2)
     assert ll.size == 3
     ll.insert_at_index(2, 'C')  # insert 'C' between 'B' and 'D'
     assert ll.head.data == 'A'  # unchanged (at index 0)
     assert ll.tail.data == 'D'  # unchanged (now at index 3)
     assert ll.tail.previous.data == 'C'
     assert ll.head.next.data == 'B'
     assert ll.head.next.next.next.data == 'D'
     assert ll.tail.previous.previous.next.data == 'C'
     assert ll.size == 4
     with self.assertRaises(ValueError):
         ll.insert_at_index(5, 'X')  # index too high
     with self.assertRaises(ValueError):
         ll.insert_at_index(-1, 'Y')  # index too low
Example #12
0
 def test_find(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.find(lambda item: item == 'B') == 'B'
     assert ll.find(lambda item: item < 'B') == 'A'
     assert ll.find(lambda item: item > 'B') == 'C'
     assert ll.find(lambda item: item == 'X') is None
Example #13
0
class LinkedDeque(object):

    # Setting our front as the head of the doubly linked list
    def __init__(self, iterable=None):
        """Initialize this deque and push_back the given items, if any."""
        # Initialize a new linked list to store the items
        self.list = LinkedList()
        if iterable is not None:
            for item in iterable:
                self.push_back(item)

    def __repr__(self):
        """Return a string representation of this deque."""
        return 'Queue({} items, front={})'.format(self.length(), self.front())

    def is_empty(self):
        """Return True if this deque is empty, or False otherwise."""
        return self.list.is_empty()

    def length(self):
        """Return the number of items in this deque."""
        return self.list.length()

    def push_front(self, item):
        """Insert the given item at the front of the linkedlist.
        Running time: O(1) calling the ll prepend method by just changing the head node"""
        self.list.prepend(item)

    def push_back(self, item):
        """Insert the given item at the back of this deque.
        Running time: O(1) calling the ll append method by just changing the tail node"""
        self.list.append(item)

    def front(self):
        """Return the item at the front of this deque without removing it,
        or None if this deque is empty."""
        if self.length() != 0:
            return self.list.head.data  # returning the head
        else:
            return None

    def back(self):
        """Return the item at the end of this deque without removing it,
        or None if this deque is empty."""
        if self.length() != 0:
            return self.list.tail.data  # returning the tail
        else:
            return None

    def pop_front(self):
        """Remove and return the item at the front of this deque,
        or raise ValueError if this deque is empty.
        Running time: O(1) from getting the front item and removing at the head of the ll"""
        if self.length() != 0:
            front_item = self.front()
            self.list.delete(front_item)
            return front_item
        else:
            raise ValueError('No items in list to pop_front: []')

    def pop_back(self):
        """Remove and return the item at the back of this deque,
        or raise ValueError if this deque is empty.
        Running time: O(1) from getting the back item and removing at the tail of the ll"""
        if self.length() != 0:
            back_item = self.back()
            self.list.delete(back_item)
            return back_item
        else:
            raise ValueError('No items in list to pop_back: []')