Ejemplo n.º 1
0
 def test_items(self):
     ll = DoubleLinkedList()
     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']
Ejemplo n.º 2
0
 def test_size(self):
     ll = DoubleLinkedList()
     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
Ejemplo n.º 3
0
 def test_length(self):
     ll = DoubleLinkedList()
     assert ll.length() == 0
     # append and prepend operations increase length
     ll.append('B')
     assert ll.head.previous == None
     assert ll.head.next == None
     assert ll.tail.previous == None
     assert ll.tail.next == None
     assert ll.length() == 1
     ll.prepend('A')
     assert ll.head.previous == None
     assert ll.head.next.data == 'B'
     assert ll.tail.previous.data == 'A'
     assert ll.tail.next == None
     assert ll.length() == 2
     ll.append('C')
     assert ll.head.previous == None
     assert ll.head.next.data == 'B'
     assert ll.tail.previous.data == 'B'
     assert ll.tail.next == None
     assert ll.length() == 3
     # delete operations decrease length
     ll.delete('B')
     assert ll.head.previous == None
     assert ll.head.next.data == 'C'
     assert ll.tail.previous.data == 'A'
     assert ll.tail.next == None
     assert ll.length() == 2
     ll.delete('C')
     assert ll.head.previous == None
     assert ll.head.next == None
     assert ll.tail.previous == None
     assert ll.tail.next == None
     assert ll.length() == 1
     ll.delete('A')
     assert ll.head == None
     assert ll.head == None
     assert ll.length() == 0
Ejemplo n.º 4
0
 def test_prepend(self):
     ll = DoubleLinkedList()
     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
     assert ll.head.next.data == 'C'
     assert ll.tail.previous.data == 'B'
     ll.prepend('A')
     assert ll.head.data == 'A'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 3
     assert ll.head.next.data == 'B'
     assert ll.tail.previous.data == 'B'
Ejemplo n.º 5
0
class Deque(object):
    def __init__(self, iterable=None):
        """Initialize this deque and enqueue the given items, if any."""
        # Initialize a new linked list to store the items
        self.list = DoubleLinkedList()
        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={} back ={})'.format(
            self.length(), self.front(), self.back())

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

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

    def push_back(self, item):
        """Insert the given item at the back of this deque.
        Running time: O(1)"""
        # TODO: Insert given item
        self.list.append(item)

    def push_front(self, item):
        """Insert the given item at the front of this deque.
        Running time: O(1)"""
        # TODO: Insert given item
        self.list.prepend(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.is_empty()):
            print("deque is empty!")
            return None

        return self.list.head.data

    def back(self):
        """Return the item at the front of this deque without removing it,
        or None if this deque is empty."""
        if (self.is_empty()):
            print("deque is empty!")
            return None

        return self.list.tail.data

    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)"""
        if (self.is_empty()):
            raise ValueError("deque is empty!")

        to_return = self.list.head.data
        self.list.head = self.list.head.next
        self.list.size -= 1
        return to_return

    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)"""
        if (self.is_empty()):
            raise ValueError("deque is empty!")

        to_return = self.list.tail.data
        self.list.tail = self.list.tail.previous
        self.list.size -= 1
        return to_return