Exemplo n.º 1
0
def sll_swap_node(sll: Singlellist, x, y) -> None:
    if x == y:
        return
    # S1: find prev_x, curr_x
    ptr = sll.head
    while ptr.next.value != x:
        ptr = ptr.next
    prev_x = ptr
    curr_x = ptr.next

    # S1: find prev_y, curr_y
    ptr = sll.head
    while ptr.next.value != y:
        ptr = ptr.next
    prev_y = ptr
    curr_y = ptr.next

    # S2: change prev
    if prev_x is None:  # x is the head
        sll.head = curr_y
    else:
        prev_x.next = curr_y

    if prev_y is None:  # y is the head
        sll.head = curr_x
    else:
        prev_y.next = curr_x

    # S3: change next
    temp = curr_x.next
    curr_x.next = curr_y.next
    curr_y.next = temp
Exemplo n.º 2
0
class LinkedQueue(Singlellist):
    """FIFO queue implementation using a single linked list for storage"""
    def __init__(self):
        """Create an empty queue"""
        self._data = Singlellist()

    def __len__(self):
        """Return the number of elements in the queue"""
        return len(self._data)

    def is_empty(self):
        """Return True if the queue is empty"""
        return len(self._data) == 0

    def first(self):
        """Return(but do not remove) the element at the front if the queue"""
        if self.is_empty():
            raise Empty("Queue is empty")
        return self._data.head.value

    def dequeue(self):
        """Remove and return the first element if the queue(i.e., FIFO).

        Raisee Empty exception if the dueue is empty"""
        if self.is_empty():
            raise Empty("Queue is empty")
        node = self._data.remove_first()
        return node.value

    def enqueue(self, e):
        """Add an element to the back of the queue"""
        self._data.add_last(e)
Exemplo n.º 3
0
 def test_contains(self):
     sll = Singlellist()
     self.assertNotIn(1, sll)
     sll = Singlellist([1, 2, 3])
     self.assertIn(1, sll)
     self.assertNotIn(0, sll)
     self.assertNotIn('0', sll)
Exemplo n.º 4
0
def reverse_sll_2(sll: Singlellist) -> Singlellist:
    if len(sll) == 1:
        return sll
    head = sll.remove_first()
    head.next = None
    reverse_sll_2(sll).tail.next = head
    sll.tail = head
    return sll
Exemplo n.º 5
0
def concatenating_linked_lists(
    l1: Singlellist,
    l2: Singlellist,
) -> Singlellist:
    result = Singlellist()
    result.head = l1.head
    l1.tail.next = l2.head
    return result
Exemplo n.º 6
0
 def test_remove_first(self):
     sll = Singlellist([1, 2, 3])
     sll.remove_first()
     self.assertEqual(sll[0], 2)
     sll.remove_first()
     self.assertEqual(sll[0], 3)
     sll.remove_first()
     self.assertEqual(sll.is_empty(), True)
Exemplo n.º 7
0
 def test_remove_all(self):
     sll = Singlellist([1, 1, 2, 3, 3])
     sll.remove_all(1)
     self.assertEqual(str(sll), "SLL[2, 3, 3]")
     sll.remove_all(3)
     self.assertEqual(str(sll), "SLL[2]")
     sll.remove_all(2)
     self.assertEqual(sll.is_empty(), True)
Exemplo n.º 8
0
def reverse_sll_loop(sll: Singlellist) -> Singlellist:
    if len(sll) == 1:
        return sll
    head = sll.head
    prev_node = None
    curr_node = sll.head
    while curr_node is not None:
        next_node = curr_node.next
        curr_node.next = prev_node
        prev_node = curr_node
        curr_node = next_node

    sll.head = sll.tail
    sll.tail = head
Exemplo n.º 9
0
 def test_add_last(self):
     sll = Singlellist()
     self.assertEqual(sll.tail, None)
     sll.add_last(1)
     self.assertEqual(sll.tail.value, 1)
     sll.add_last(2)
     self.assertEqual(sll.tail.value, 2)
Exemplo n.º 10
0
 def test_add_first(self):
     sll = Singlellist()
     self.assertEqual(sll.head, None)
     sll.add_first(1)
     self.assertEqual(sll.head.value, 1)
     sll.add_first(2)
     self.assertEqual(sll.head.value, 2)
Exemplo n.º 11
0
 def __init__(self):
     """Create an empty queue"""
     self._data = Singlellist()
Exemplo n.º 12
0
def count_num(sll: Singlellist) -> int:
    if sll.head is None:
        return 0
    sll.remove_first()
    return 1 + count_num(sll)
Exemplo n.º 13
0
 def test_search(self):
     sll = Singlellist([1, 2, 3])
     self.assertTrue(1 in sll)
     self.assertFalse(0 in sll)
Exemplo n.º 14
0
 def test_change_all(self):
     sll = Singlellist([1, 1, 2, 3, 3])
     sll.change_all(1, 0)
     self.assertEqual(str(sll), "SLL[0, 0, 2, 3, 3]")
     sll.change_all(3, 4)
     self.assertEqual(str(sll), "SLL[0, 0, 2, 4, 4]")
Exemplo n.º 15
0
 def test_change(self):
     sll = Singlellist([1, 2, 3])
     sll.change(1, 0)
     self.assertEqual(sll.head.value, 0)
     sll.change(3, 4)
     self.assertEqual(sll.tail.value, 4)
Exemplo n.º 16
0
class LinkedStack(Singlellist):
    """
    LIFO Srtack implementation using Python list as underlyinh storage.
    """
    def __init__(self):
        """Create and empty stack.
        """
        self._data = Singlellist()

    def __len__(self):
        """Return the number of elements in the stack.
        Time Complexity: O(1)
        """
        return len(self._data)

    def __str__(self):
        """
        Show the stack properly.
        Time Complexity: O(n)
        """
        if self.is_empty():
            s1 = '| ' + "".center(5) + ' |' + '\n'
            s2 = '-' * 9
            return s1 + s2
        else:
            s = []
            for i in range(len(self._data) - 1, -1, -1):
                ele = self._data[i]
                s1 = '| ' + ele.__repr__().center(5) + ' |' + '\n'
                s2 = '-' * 9 + '\n'
                s.append(s1 + s2)
            return ''.join(s)

    def is_empty(self):
        """Return True if the stack is empty
        Time Complexity: O(1)
        """
        return len(self._data) == 0

    def push(self, e):
        """Add element to the top of the stack
        Time Complexity: O(1)
        Note: "*" in here means amortization
        """
        self._data.add_last(e)

    def top(self):
        """
        Return (but not remove) at the top of the stack.
        Raise Empty exception if the stack in empty.
        Time Complexity: O(1)
        """
        if self.is_empty():
            raise Empty("Stack in empty!")
        return self._data.tail.value

    def pop(self):
        """
        Remove and return the element from the top of the stack(LIFO)
        Raise Empty exception if the stack is empty.
        Time Complexity: O(1)
        """
        if self.is_empty():
            raise Empty("Stack is empty!")
        ele = self._data.tail.value
        self._data.remove_last()
        return ele
Exemplo n.º 17
0
 def test_remove(self):
     sll = Singlellist([1, 2, 3])
     sll.remove(1)
     self.assertEqual(sll.head.value, 2)
     sll.remove(3)
     self.assertEqual(sll.tail.value, 2)
Exemplo n.º 18
0
 def test_remove_last(self):
     sll = Singlellist([1, 2, 3])
     sll.remove_last()
     self.assertEqual(sll[1], 2)
Exemplo n.º 19
0
 def test_init_items(self):
     sll = Singlellist([1, 2, 3])
     self.assertEqual(len(sll), 3)
     self.assertEqual(sll.head.value, 1)
     self.assertEqual(sll.tail.value, 3)
     self.assertEqual(str(sll), 'SLL[1, 2, 3]')
Exemplo n.º 20
0
 def __init__(self):
     """Create and empty stack.
     """
     self._data = Singlellist()
Exemplo n.º 21
0
 def test_insert_after(self):
     sll = Singlellist([1, 2, 3])
     sll.insert_after(1, 11)
     self.assertEqual(sll[1], 11)
     sll.insert_after(3, 33)
     self.assertEqual(sll[4], 33)
Exemplo n.º 22
0
def reverse_sll_1(sll: Singlellist) -> Singlellist:
    if len(sll) == 1:
        return sll
    head = sll.remove_first()
    reverse_sll_1(sll).add_last(head.value)
    return sll
Exemplo n.º 23
0
 def test_empty(self):
     sll = Singlellist()
     self.assertEqual(sll.is_empty(), True)
     sll.add_first(1)
     self.assertEqual(sll.is_empty(), False)