Ejemplo n.º 1
0
 def test_8(self):
     """ prev of prior tail is now tail """
     dl = DoubleLinkedList()
     n1 = Node(5)
     n2 = Node(3)
     dl.push(n1)
     dl.push(n2)
     dl.shift()
     self.assertEqual(dl.tail._value, 3)
Ejemplo n.º 2
0
	def test_7(self):
		"""check that shift removes last node"""
		n1 = Node('A')
		n2 = Node('B')
		n3 = Node('C')
		dl = DoubleLinkedList()
		dl.append(n1)
		dl.append(n2)
		dl.append(n3)
		dl.shift()
		self.assertEqual(dl.tail._value, 'B')
Ejemplo n.º 3
0
class Queue(object):
    """Class Queue for enqueue and dequeue data structure."""
    def __init__(self):
        """Init for Queue."""
        self._new_dll = DoubleLinkedList()

    def enqueue(self, val):
        """Enqueue function for queue pushes value to head."""
        return self._new_dll.push(val)

    def dequeue(self):
        """Dequeue function for queue removes val from tail and returns val."""
        return self._new_dll.shift()

    def peek(self):
        """Return a new value without dequeuing it."""
        print(self._new_dll.tail.data)

    def size(self):
        """Length function for the queue."""
        return self._new_dll.size()

    def __len__(self):
        """Length."""
        return self._new_dll.size()
Ejemplo n.º 4
0
class Deque(object):
    """Class deque for deque composition."""

    def __init__(self):
        """Init for deque."""
        self._new_dll = DoubleLinkedList()

    def append(self, val):
        """Enqueue function for queue pushes value to head."""
        return self._new_dll.push(val)

    def appendleft(self, val):
        """Append vals to the front of deque."""
        return self._new_dll.append(val)

    def pop(self):
        """Pop from dll."""
        return self._new_dll.pop()

    def popleft(self):
        """Shift for popleft."""
        return self._new_dll.shift()

    def peek(self):  # pragma no cover
        """Return a new value without dequeuing it."""
        print(self._new_dll.head.data)

    def peekleft(self):  # pragma no cover
        """Return a new value from the left without dequeing it."""
        print(self._new_dll.tail.data)

    def __len__(self):
        """Length function for the queue."""
        return len(self._new_dll)
Ejemplo n.º 5
0
class Deque(object):
    """Will create a instance of Deque."""
    def __init__(self, iterable=None):
        """Will init a new instance of the Stack class."""
        from dll import DoubleLinkedList
        self._dll = DoubleLinkedList()
        self._counter = 0

    def append(self, data):
        """Will push a new element into the Deque."""
        self._dll.append(data)
        self._counter += 1

    def pop_left(self):
        """Will remove the head value."""
        if self._dll.head:
            self._counter -= 1
        else:
            raise ValueError('no value left to pop')
        return self._dll.pop()

    def append_left(self, data):
        """Append a value to the head."""
        self._dll.push(data)
        self._counter += 1

    def pop(self):
        """Pop a value from the end."""
        if self._dll.tail:
            self._counter -= 1
        else:
            raise ValueError('no value left to pop')
        return self._dll.shift()

    def peek_left(self):
        """Will return the value that pop_left would return."""
        if self._dll.head:
            return self._dll.head.data
        else:
            raise ValueError('no value in the deque to peek')

    def peek(self):
        """Will return the value that pop would return."""
        if self._dll.head:
            return self._dll.tail.data
        else:
            raise ValueError('no value in the deof correct typeque to peek')

    def size(self):
        """Return the size of the deque."""
        return self._counter

    def __len__(self):
        """Will return the length of the deque using len()."""
        return self._counter
Ejemplo n.º 6
0
class Deque(object):
    """Deque data structure, can add and remove from both ends."""
    def __init__(self):
        """Initialize the deque."""
        self.deque = DoubleLinkedList()

    def append(self, value):
        """Add an item to the back of the deque."""
        self.deque.append(value)

    def append_left(self, value):
        """Add an item to the front of the deque."""
        self.deque.push(value)

    def pop(self):
        """Pop a value off of the back of the deque and return it."""
        return self.deque.shift()

    def pop_left(self):
        """Pop a value off of the front of the deque and return it."""
        return self.deque.pop()

    def peek(self):
        """Return the next value that would be poped at the end of deque."""
        try:
            return self.deque.tail.data
        except AttributeError:
            return None

    def peek_left(self):
        """Return the next value to pop in the deque."""
        try:
            return self.deque.head.data
        except AttributeError:
            return None

    def size(self):
        """Return the number of nodes in the deque."""
        return self.deque.__len__()
Ejemplo n.º 7
0
def random_list(count):
    numbers = DoubleLinkedList()
    for i in range(count, 0, -1):
        numbers.shift(randint(0, 10000))
    return numbers