def swap_values(self, value_1, value_2):
        if self.is_empty():
            raise EmptyCollection()
        if value_1 != value_2:
            # TODO: this code can be improved to find value_1 and value_2 in one iteration
            previous_value_1, current_value_1 = self._get_nodes_based_on_value(
                value=value_1,
                head=self._head,
            )
            previous_value_2, current_value_2 = self._get_nodes_based_on_value(
                value=value_2,
                head=self._head,
            )

            if previous_value_1 is None:
                self._head = current_value_2
            else:
                previous_value_1.next_node = current_value_2

            if previous_value_2 is None:
                self._head = current_value_1
            else:
                previous_value_2.next_node = current_value_1

            tmp_next_node_for_value_2 = current_value_2.next_node
            current_value_2.next_node = current_value_1.next_node
            current_value_1.next_node = tmp_next_node_for_value_2

            if current_value_1.next_node is None:
                self._tail = current_value_1

            if current_value_2.next_node is None:
                self._tail = current_value_2
Ejemplo n.º 2
0
 def dequeue(self):
     if self.is_empty():
         raise EmptyCollection('Queue is empty')
     answer = self._data[self._front]
     self._data[self._front] = None
     self._front = (self._front + 1) % len(self._data)
     self._size -= 1
     return answer
Ejemplo n.º 3
0
    def get_last_operation(self) -> Any:
        if self.is_empty():
            raise EmptyCollection('Collection is empty')

        self._decrease_next_operation_index()
        last_operation = self._operations[self._index_of_next_operation]
        self._operations[self._index_of_next_operation] = None
        self._size -= 1

        return last_operation
 def dequeue(self):
     if self.is_empty():
         raise EmptyCollection('Queue is empty')
     oldhead = self._tail._next
     if self._size == 1:
         self._tail = None
     else:
         self._tail._next = oldhead._next
     self._size -= 1
     return oldhead._element
    def get_first_and_remove(self):
        if self.is_empty():
            raise EmptyCollection()

        value = self.first
        if len(self) == 1:
            self._head = None
            self._tail = None
        else:
            self._head = self._head.next_node
        self._size -= 1
        return value
Ejemplo n.º 6
0
 def first(self):
     if self.is_empty():
         raise EmptyCollection('Queue is empty')
     return self._data[self._front]
Ejemplo n.º 7
0
 def raise_exception_if_empty(self):
     if self.is_empty():
         raise EmptyCollection('This collection is empty')
 def first(self) -> Any:
     if self.is_empty():
         raise EmptyCollection('No values to get!')
     return self._head.value
 def first(self):
     if self.is_empty():
         raise EmptyCollection('Queue is empty')
     head = self._tail._next
     return head._element
 def _raise_if_empty(self):
     if self.is_empty():
         raise EmptyCollection()