Example #1
0
def main():
    first_list = LinkedList()
    second_list = LinkedList()
    for i in range(3):
        first_list.insert(i)
    for i in range(3):
        second_list.insert(i)
Example #2
0
    def test_merge(self):
        s_list_one = LinkedList()
        s_list_one.add_in_tail(Node(1))
        s_list_one.add_in_tail(Node(2))
        s_list_one.add_in_tail(Node(3))
        s_list_one.add_in_tail(Node(4))
        s_list_one.add_in_tail(Node(5))
        s_list_one.add_in_tail(Node(6))
        s_list_one.add_in_tail(Node(7))
        s_list_one.add_in_tail(Node(8))
        s_list_one.add_in_tail(Node(9))
        s_list_one.add_in_tail(Node(10))
        s_list_one.add_in_tail(Node(11))
        s_list_one.add_in_tail(Node(12))
        s_list_one.add_in_tail(Node(13))

        s_list_two = LinkedList()
        s_list_two.add_in_tail(Node(13))
        s_list_two.add_in_tail(Node(12))
        s_list_two.add_in_tail(Node(11))
        s_list_two.add_in_tail(Node(10))
        s_list_two.add_in_tail(Node(9))
        s_list_two.add_in_tail(Node(8))
        s_list_two.add_in_tail(Node(7))
        s_list_two.add_in_tail(Node(6))
        s_list_two.add_in_tail(Node(5))
        s_list_two.add_in_tail(Node(4))
        s_list_two.add_in_tail(Node(3))
        s_list_two.add_in_tail(Node(2))
        s_list_two.add_in_tail(Node(1))

        s_list_merged = merge_list(s_list_two, s_list_one)

        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))

        self.assertEqual(s_list_merged.len(), self.list_test.len())

        node = s_list_merged.head
        node_test = self.list_test.head
        while node is not None and node_test is not None:
            self.assertEqual(node.value, node_test.value)
            node = node.next
            node_test = node_test.next
Example #3
0
def main():
    linked_list = LinkedList(0)
    for i in range(10):
        linked_list.insert(i+1)
    linked_list.print_linked_list()
    linked_list.delete(3)
    linked_list.print_linked_list()
 def test_validdata(self):
     
     ll = LinkedList(5)
     ll.append_node(6)
     ll.append_node(10)
     ll.append_node(20)
     result_ll = ll.get_linkedlist()
     actual_list = [5,6,10,20]
     self.assertEqual(result_ll,actual_list)
    def test_delete_node(self):

        ll = LinkedList(5)
        ll.append_node(6)
        ll.append_node(10)
        ll.append_node(20)

        ll.delete_node(6)

        result_ll = ll.get_linkedlist()
        actual_list = [5,10,20]
        self.assertEqual(result_ll,actual_list)
    def test_insert_node_in_the_begining(self):

        ll = LinkedList(5)
        ll.append_node(6)
        ll.append_node(10)
        ll.append_node(20)
        
        ll.insert_node_in_the_begining(7)
        
        result_ll = ll.get_linkedlist()
        actual_list = [7,5,6,10,20]
        self.assertEqual(result_ll,actual_list)
Example #7
0
 def __init__(self):
     self.size = 0
     self.storage = LinkedList()
from single_linked_list import LinkedList, Node


def getCount(head_node):
    #code here
    cnt = 0
    temp = head_node
    while temp != None:
        cnt += 1
        temp = temp.next
    return cnt


if __name__ == '__main__':
    # t = int(input())
    # for cases in range(t):
    n = int(input())
    a = LinkedList()
    nodes = list(map(int, input().strip().split()))
    for data in nodes:
        node = Node(data)
        a.append(node)
    print(getCount(a.head))
    """

    if linked_list.is_empty():
        return False

    fast = linked_list.head.next
    slow = linked_list.head.next

    # 快慢指针查找中位数
    while fast is not None and fast.next is not None:
        fast = fast.next.next
        slow = slow.next




if __name__ == '__main__':
    # 创建一个链表对象
    linked_list = LinkedList()

    print('链表是否为空:%d' % linked_list.is_empty())

    linked_list.append('l')
    linked_list.append('e')
    linked_list.append('v')
    linked_list.append('e')
    linked_list.append('l')

    print('遍历单链表:')
    linked_list.traverse()
Example #10
0
from single_linked_list import LinkedList, Node

def getNth(head, k):
    # Code here
    idx = 0
    temp = head
    while idx != k-1:
        temp = temp.next
        idx += 1
    return temp.data

if __name__ == "__main__":
    T = int(input())
    while T > 0:
        llist = LinkedList()
        n, k = list(map(int, input().split()))
        nodes = list(map(int, input().split()))
        for data in reversed(nodes):
            llist.push(data)
        m = getNth(llist.head, k)
        print(m)
        T -= 1
Example #11
0
 def setUp(self):
     self.list = LinkedList()
     self.list_test = LinkedList()