Ejemplo n.º 1
0
 def __init__(self, m=8, hashfunc=None):
     self.m = m
     self.t = [ll.LinkedList() for i in range(m)]
     self.n = 0
     self.hashfunc = hashfunc
     if hashfunc is None:
         self.hashfunc = lambda x: hash(x) % self.m
Ejemplo n.º 2
0
 def test_insert(self):
     linked_list = ll.LinkedList()
     linked_list.append(0)
     linked_list.append(2)
     linked_list.insert(1, 1)
     self.assertEqual(linked_list.len(), 3)
     self.assertEqual(linked_list.head.next.value, 1)
Ejemplo n.º 3
0
def addTwoLinkedList(l1, l2):
    l1Pointer = l1.head
    l2Pointer = l2.head
    sumLL = ll.LinkedList()
    currentCarry = 0
    nextCarry = 0
    if l1.size() > l2.size():
        # pad 0s
        while l2.size() < l1.size():
            l2.insert(0)
    if l1.size() < l2.size():
        while l2.size() > l1.size():
            l1.insert(0)
    while l1Pointer != None:
        cur_sum = l1Pointer.getData() + l2Pointer.getData()
        if cur_sum >= 10:
            cur_sum = cur_sum % 10
            nextCarry = 1
        else:
            nextCarry = 0

        sumLL.insert(carry + cur_sum)

        l1Pointer = l1Pointer.getNext()
        l2Pointer = l2Pointer.getNext()

    return sumLL
Ejemplo n.º 4
0
 def test_append(self):
     linked_list = ll.LinkedList()
     linked_list.append(0)
     self.assertEqual(linked_list.head.value, 0)
     self.assertEqual(linked_list.len(), 1)
     linked_list.append(1)
     next_node = linked_list.head.next
     self.assertEqual(linked_list.head.value, 0)
     self.assertEqual(linked_list.len(), 2)
     self.assertEqual(next_node.value, 1)
Ejemplo n.º 5
0
 def test_index(self):
     linked_list = ll.LinkedList()
     # [0, 3, 2, 1]
     linked_list.append(0)
     linked_list.append(3)
     linked_list.append(2)
     linked_list.append(1)
     i = linked_list.index(0)
     self.assertEqual(i, 0)
     i = linked_list.index(3, 0, 1)
     self.assertEqual(i, 1)
     i = linked_list.index(3, 1, 2)
     self.assertEqual(i, 1)
Ejemplo n.º 6
0
 def test_remove(self):
     linked_list = ll.LinkedList()
     # [0, 1, 2, 1]
     linked_list.append(0)
     linked_list.append(1)
     linked_list.append(2)
     linked_list.append(1)
     linked_list.remove(2)
     self.assertEqual(linked_list.len(), 3)
     self.assertEqual(linked_list.to_list(), [0, 1, 1])
     linked_list.remove(1)
     self.assertEqual(linked_list.len(), 2)
     self.assertEqual(linked_list.to_list(), [0, 1])
Ejemplo n.º 7
0
 def test_reverse(self):
     linked_list = ll.LinkedList()
     # [0, 3, 2, 1]
     linked_list.append(0)
     linked_list.append(3)
     linked_list.append(2)
     linked_list.append(1)
     before = linked_list.to_list()
     linked_list.reverse()
     after = linked_list.to_list()
     self.assertEqual([1, 2, 3, 0], after)
     linked_list.reverse()
     after_after = linked_list.to_list()
     self.assertEqual(before, after_after)
Ejemplo n.º 8
0
 def test_count(self):
     linked_list = ll.LinkedList()
     # [0, 3, 2, 1, 0]
     linked_list.append(0)
     linked_list.append(3)
     linked_list.append(2)
     linked_list.append(1)
     linked_list.append(0)
     c = linked_list.count(0)
     self.assertEqual(c, 2)
     c = linked_list.count(3)
     self.assertEqual(c, 1)
     c = linked_list.count(None)
     self.assertEqual(c, 0)
Ejemplo n.º 9
0
 def test_copy(self):
     linked_list = ll.LinkedList()
     # [0, 3, 2, 1]
     linked_list.append(0)
     linked_list.append(3)
     linked_list.append(2)
     linked_list.append(1)
     before = linked_list.to_list()
     after_ll = linked_list.copy()
     after = after_ll.to_list()
     self.assertEqual(before, after)
     linked_list.append(0)
     before = linked_list.to_list()
     after = after_ll.to_list()
     self.assertEqual([0, 3, 2, 1, 0], before)
     self.assertEqual([0, 3, 2, 1], after)
Ejemplo n.º 10
0
 def test_pop(self):
     linked_list = ll.LinkedList()
     # [0, 3, 2, 1]
     linked_list.append(0)
     linked_list.append(3)
     linked_list.append(2)
     linked_list.append(1)
     pop = linked_list.pop()
     self.assertEqual(pop, 1)
     self.assertEqual(linked_list.len(), 3)
     pop = linked_list.pop(1)
     self.assertEqual(pop, 3)
     self.assertEqual(linked_list.len(), 2)
     linked_list.pop()
     linked_list.pop()
     try:
         linked_list.pop()
     except Exception as e:
         self.assertEqual(str(e), "Cannot pop empty LinkedList")
Ejemplo n.º 11
0
 def test_create_list(self):
     linked_list = ll.LinkedList()
     self.assertIsNone(linked_list.head)
     self.assertEqual(linked_list.length, 0)
Ejemplo n.º 12
0
#######################################################################################
def removeDuplicatesLinkedList(head_node):
    hashtable = {}
    while head_node != None:
        data = head_node.getData()
        if data in hashtable:
            prev.setNext(head_node.getNext())
        else:
            prev = head_node
        head_node = head_node.getNext()
        hashtable[data] = 1


if q5test:
    linked_list = ll.LinkedList()
    linked_list.insert(3)
    linked_list.insert(3)
    linked_list.insert(3)
    linked_list.insert(4)
    linked_list.insert(5)
    removeDuplicatesLinkedList(linked_list.head)
    print 'NEW', linked_list.size()
    print linked_list.head.getData()
    print linked_list.head.getNext().getData()
    print linked_list.head.getNext().getNext().getData()


def findKthElementFromLast(linked_list, n):
    p1 = linked_list.head
    p2 = linked_list.head
Ejemplo n.º 13
0
 def __init__(self):
     self.data = ll.LinkedList()