Esempio n. 1
0
def main():
    list1 = LinkedList()
    list1.add_to_front(2)
    list1.add_to_front(3)
    list1.add_to_back(4)
    list1.add(1)
    list1.add(5)
    print list1
    #for value in list1.enumerate():
    #print value

    # 1) 3=>2=>4=>1=>5=>None remove(4) 3=>2=>1=>5=>None
    list1.remove(4)
    print list1
    list1.remove(5)
    print list1
    # 2) 3=>2=>4=>1=>5=>None remove(5) 3=>2=>4=>1=>None
    list1.remove_front()
    # print list1
    # 3) 3=>2=>4=>1=>5=>None remove(3) 2=>4=>1=>5=>None
    # list1.remove_back()
    # print list1

    list1.clear()
    print list1
Esempio n. 2
0
 def test_linked_list_clear(self):
     ll = LinkedList()
     ll.add_to_head(7)
     ll.add_to_head(5)
     ll.add_to_head(3)
     ll.clear()
     self.assertEqual(None, ll.head)
     self.assertEqual(None, ll.tail)
     self.assertEqual(0, ll.count)
Esempio n. 3
0
    def test_detect_loop_start(self):
        ll = LinkedList()
        node1 = ListElement(3)
        node2 = ListElement(1)
        node3 = ListElement(10)
        ll.head = node1
        node1.next = node2
        node2.next = node3
        node3.next = node2
        eq_(ll.detect_loop_start(), node2)

        ll.clear()
        ll.head = ListElement(100)
        eq_(ll.detect_loop_start(), None)
Esempio n. 4
0
    def test_detect_loop(self):
        ll = LinkedList()
        node1 = ListElement(3)
        node2 = ListElement(1)
        node3 = ListElement(10)
        ll.head = node1
        node1.next = node2
        node2.next = node3
        node3.next = node2
        eq_(ll.detect_loop(), True)

        ll.clear()
        ll.head = ListElement(100)
        eq_(ll.detect_loop(), False)
Esempio n. 5
0
def test_clear() -> None:
    node_1_data = {"id": 1}
    node_2_data = {"id": 2}
    node_3_data = {"id": 3}
    node_4_data = {"id": 4}

    linked_list = LinkedList()
    linked_list.append(node_4_data)
    linked_list.append(node_3_data)
    linked_list.append(node_2_data)
    linked_list.append(node_1_data)

    linked_list.clear()

    assert linked_list.head is None
    assert linked_list.tail is None
Esempio n. 6
0
        lst.remove_first()

    elif menu == Menu.꼬리노드삭제:  # 맨 끝 노드 삭제
        lst.remove_last()

    elif menu == Menu.주목노드출력:  # 주목 노드 출력
        lst.print_current_node()

    elif menu == Menu.주목노드이동:  # 주목 노드를 한 칸 뒤로 이동
        lst.next()

    elif menu == Menu.주목노드삭제:  # 주목 노드 삭제
        lst.remove_current_node()

    elif menu == Menu.모든노드삭제:  # 모든 노드를 삭제
        lst.clear()

    elif menu == Menu.검색:  # 노드를 검색
        pos = lst.search(int(input('검색할 값을 입력하세요.: ')))
        if pos >= 0:
            print(f'그 값의 데이터는 {pos + 1}번째에 있습니다.')
        else:
            print('해당 데이터가 없습니다.')

    elif menu == Menu.멤버십판단:  # 멤버십 판단
        print('그 값의 데이터는 포함되어' +
              (' 있습니다.' if int(input('멤버십 판단할 값을 입력하세요.: ')) in
               lst else ' 있지 않습니다.'))

    elif menu == Menu.모든노드출력:  # 모든 노드 출력
        lst.print()
Esempio n. 7
0
class TestLinkedList(unittest.TestCase):
    def setUp(self):
        self.l = LinkedList()

    def test_empty(self):
        ok_(self.l.is_empty())
        self.l.insert_tail(1)
        ok_(not self.l.is_empty())

    def test_clear(self):
        self.l.insert_tail(1)
        self.l.clear()
        eq_(self.l.head, None)

    def test_size(self):
        eq_(self.l.size, 0)
        self.l.insert_tail(1)
        eq_(self.l.size, 1)

    def test_insert_tail(self):
        self.l.insert_tail(1)
        eq_(self.l.head.data, 1)
        self.l.insert_tail(2)
        eq_(self.l.head.next.data, 2)

    def test_insert_head(self):
        self.l.insert_head(1)
        eq_(self.l.peek_head(), 1)

        self.l.insert_head(2)
        eq_(self.l.peek_head(), 2)
        eq_(self.l.head.next.data, 1)

    def test_remove_head(self):
        self.l.insert_tail(100)
        self.l.insert_tail(10)
        eq_(self.l.remove_head(), 100)
        eq_(self.l.remove_head(), 10)
        eq_(self.l.remove_head(), None)

    def test_remove_last(self):
        self.l.insert_tail(1)
        self.l.insert_tail(2)
        self.l.insert_tail(3)
        eq_(self.l.remove_last(), 3)
        eq_(self.l.remove_last(), 2)
        eq_(self.l.remove_last(), 1)
        eq_(self.l.remove_last(), None)

    def test_remove(self):
        self.l.insert_tail(1)
        self.l.insert_tail(2)
        self.l.insert_tail(3)
        eq_(self.l.remove(2), 2)
        eq_(self.l.head.data, 1)
        eq_(self.l.head.next.data, 3)
        eq_(self.l.remove(3), 3)
        eq_(self.l.head.data, 1)
        eq_(self.l.head.next, None)

    def test_search(self):
        eq_(self.l.search(10), False)
        self.l.insert_tail(10)
        self.l.insert_head(-1)
        eq_(self.l.search(10), True)
        eq_(self.l.search(-1), True)
        eq_(self.l.search('a'), False)

    def test_find_middle(self):
        self.l.insert_tail(1)
        self.l.insert_tail(2)
        self.l.insert_tail(3)
        self.l.insert_tail(4)
        self.l.insert_tail(5)
        # odd case
        eq_(self.l.find_middle(), 3)

        # even case: 1->2->3->4
        self.l.remove_last()
        eq_(self.l.find_middle(), 2)

        # 1
        [self.l.remove_last() for _ in range(3)]
        eq_(self.l.find_middle(), 1)

        # empty
        self.l.remove_head()
        eq_(self.l.find_middle(), None)

    def test_remove_nth_from_end(self):
        ll = LinkedList()
        ll.insert_tail(1)
        ll.insert_tail(2)
        ll.insert_tail(3)
        ll.head = remove_nth_from_end(ll.head, 2)
        eq_(ll.head.data, 1)
        eq_(ll.head.next.data, 3)
        ll.head = remove_nth_from_end(ll.head, 1)
        eq_(ll.head.data, 1)
        eq_(ll.head.next, None)

    def test_detect_loop(self):
        ll = LinkedList()
        node1 = ListElement(3)
        node2 = ListElement(1)
        node3 = ListElement(10)
        ll.head = node1
        node1.next = node2
        node2.next = node3
        node3.next = node2
        eq_(ll.detect_loop(), True)

        ll.clear()
        ll.head = ListElement(100)
        eq_(ll.detect_loop(), False)

    def test_detect_loop_start(self):
        ll = LinkedList()
        node1 = ListElement(3)
        node2 = ListElement(1)
        node3 = ListElement(10)
        ll.head = node1
        node1.next = node2
        node2.next = node3
        node3.next = node2
        eq_(ll.detect_loop_start(), node2)

        ll.clear()
        ll.head = ListElement(100)
        eq_(ll.detect_loop_start(), None)

    def test_reverse_list(self):
        ll = LinkedList()
        [ll.insert_tail(i) for i in range(1, 4)]
        reversed = ll.reverse_list()
        eq_(reversed.data, 3)
        eq_(reversed.next.data, 2)
        eq_(reversed.next.next.data, 1)
        eq_(reversed.next.next.next, None)

        ll = LinkedList()
        ll.insert_tail(1)
        reversed = ll.reverse_list()
        eq_(reversed.data, 1)
        eq_(reversed.next, None)

        ll = LinkedList()
        reversed = ll.reverse_list()
        eq_(reversed, None)

    def test_remove_target_node(self):
        ll = LinkedList()
        ll.insert_tail(1)
        ll.insert_tail(2)
        ll.insert_tail(3)
        ll.head = remove_target_node(ll.head, 2)
        eq_(ll.head.data, 1)
        eq_(ll.head.next.data, 3)
        eq_(ll.head.next.next, None)

        ll = LinkedList()
        ll.insert_tail(3)
        ll.insert_tail(3)
        ll.head = remove_target_node(ll.head, 3)
        eq_(ll.head, None)

        ll = LinkedList()
        ll.insert_tail(1)
        ll.head = remove_target_node(ll.head, 10)
        eq_(ll.head.data, 1)
        eq_(ll.head.next, None)

    def test_odd_even_order(self):
        ll = LinkedList()
        ll.insert_tail(1)
        ll.insert_tail(2)
        ll.insert_tail(3)
        ll.head = odd_even_order(ll.head)
        eq_(ll.head.data, 1)
        eq_(ll.head.next.data, 3)
        eq_(ll.head.next.next.data, 2)
        eq_(ll.head.next.next.next, None)

        ll = LinkedList()
        ll.insert_tail(1)
        ll.head = odd_even_order(ll.head)
        eq_(ll.head.data, 1)
        eq_(ll.head.next, None)

        ll = LinkedList()
        ll.insert_tail(1)
        ll.insert_tail(2)
        ll.insert_tail(3)
        ll.insert_tail(4)
        ll.head = odd_even_order(ll.head)
        eq_(ll.head.data, 1)
        eq_(ll.head.next.data, 3)
        eq_(ll.head.next.next.data, 2)
        eq_(ll.head.next.next.next.data, 4)
        eq_(ll.head.next.next.next.next, None)

        eq_(ll.head.data, 1)

    def test_rotate(self):
        ll = LinkedList()
        ll.insert_tail(1)
        ll.insert_tail(2)
        ll.insert_tail(3)
        ll.insert_tail(4)
        ll.insert_tail(5)
        ll.rotate(2)
        eq_(ll.head.data, 4)

        ll = LinkedList()
        ll.insert_tail(1)
        ll.rotate(3)

    def test_rotate2(self):
        ll = LinkedList()
        ll.insert_tail(1)
        ll.insert_tail(2)
        ll.insert_tail(3)
        ll.insert_tail(4)
        ll.insert_tail(5)
        ll.rotate2(7)
        eq_(ll.head.data, 4)

        ll = LinkedList()
        ll.insert_tail(1)
        ll.rotate2(3)
Esempio n. 8
0
# extending a linked list
ll2 = LinkedList()
print(f"unextended ll2: {ll2}")
ll2.extend([1, 2, 3])
ll2.extend((4, 5, 6))
ll2.extend({7, 8, 9})
print(f"extended ll2: {ll2}\n")  # [1, 2, 3, 4, 5, 6, 8, 9, 7]

# sorting a linked list
ll3 = LinkedList((1, 4, 2, 3))
print(f"unsorted ll3: {ll3}")  # [1, 4, 2, 3]
ll3.sort()
print(f"sorted ll3: {ll3}\n")  # [1, 2, 3, 4]

# popping and inserting
ll4 = LinkedList([3, 4, 5])
print(f"ll4: {ll4}")  # [3, 4, 5]
popped_item = ll4.pop(0)
print(f"ll4 without index 0: {ll4}")
ll4.insert(popped_item)
print(f"ll4 after inseting popped_item: {ll4}\n")

# general methods
ll = LinkedList([1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(f"object at the 4th index of ll: {ll.get(4)}")
print(f"# of instances of 1: {ll.count(1)}")
del ll[0]
print(f"ll after deleting object at index 0: {ll}")
ll.clear()
print(f"ll after clearing it: {ll}")