4. Linked lists

4.1. Testy

  • Reprezentacja,

  • Wstawianie wartości na danej pozycji w liscie,

  • pozycja “0”,

  • pozycja n

  • Odwracanie listy

4.2. Implementacja testów

[25]:
## Implementacja testów
import unittest

class TestLinkedList(unittest.TestCase):
    """Test linked list"""
    def setUp(self):
        self.linked_list = Linked(1)
        linked2 = Linked(2)
        linked3 = Linked(3)
        linked2.next = linked3
        self.linked_list.next = linked2

    def test_repr(self):
        self.assertRegex(repr(self.linked_list), '<Linked with value:.*')
    def test_insert_at_position_0(self):
        new_linked = self.linked_list.insert_at(0, 4)

        cur = new_linked
        results = []
        while cur:
            results.append(cur.data)
            cur = cur.next
        self.assertEqual(sorted(results), [1, 2, 3, 4])
        self.assertEqual(results, [4, 1, 2, 3])

    def test_insert_at_postion_1(self):
        self.linked_list.insert_at(1, 4)
        cur = self.linked_list
        results = []
        while cur:
            results.append(cur.data)
            cur = cur.next
        self.assertEqual(results, [1, 4, 2, 3])

    def test_insert_at_position_2(self):
        self.linked_list.insert_at(2, 4)
        cur = self.linked_list
        results = []
        while cur:
            results.append(cur.data)
            cur = cur.next
        self.assertEqual(results, [1, 2, 4, 3])

    def test_insert_at_position_3(self):
        self.linked_list.insert_at(3, 4)
        cur = self.linked_list
        results = []
        while cur:
            results.append(cur.data)
            cur = cur.next
        self.assertEqual(results, [1, 2, 3, 4])

4.2.1. Odpalenie testów

[26]:
if __name__ == '__main__':
    unittest.main(argv=['first-arg-is-ignored'], exit=False)
.....
----------------------------------------------------------------------
Ran 5 tests in 0.008s

OK

4.3. Implementacja list

[20]:
class Linked:
    def __init__(self, data):
        self.data = data
        self.next = None
    def __repr__(self):
        return '<{} with value: {}'.format(self.__class__.__name__, self.data)

    def insert_at(self, pos, data):
        if self.data == self.next == None:
            self.data = data
        cur = self
        tmp_pos = 0

        if pos == 0:
            el = Linked(data)
            el.next = self
            return el

        while cur and tmp_pos < pos:
            tmp_pos += 1
            if tmp_pos == pos:
                el = Linked(data)
                el.next = cur.next
                cur.next = el
            else:
                cur = cur.next

        return self
[1]:
class Element:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.len = 1
    def insert(self, val):
        cur = self
        while cur.next is not None:
            cur = cur.next
        cur.next = Element(val)
        self.len += 1

    def __repr__(self):
        next_val = None
        if hasattr(self.next, 'val'):
            next_val = self.next.val
        return '<{} {} long with value: {} and next element {}'.format(self.__class__.__name__, self.len, self.val, next_val)
    def insert_first(self, val):
        head = Element(val)
        head.next = self
        self.len += 1

        return head
    def deleteNode(self, position):
        self[position-1].next = self[position+1]
        self.len -= 1
        return self[position]

    def __iter__(self):
        self.n = 0
        self.cur = self

        return self

    def __next__(self):
        if self.n < self.len:
            ret_val = self.cur
            self.n += 1
            self.cur = self.cur.next
            return ret_val
        else:
            raise StopIteration

    def __getitem__(self, index):
        cur_iter = iter(self)
        cur_item = None

        for i, item in enumerate(cur_iter):

            if i < index:
                continue
            elif i == index:
                cur_item = item
            else:
                break
        return cur_item


    def __len__(self):
        return self.len
[ ]: