Exemple #1
0
class QueueLinkedList:
    def __init__(self):
        self.lst = None

    def createQueue(self):
        self.lst = SingleLinkedList()

    def enqueque(self, value):
        if self.lst is not None:
            self.lst.insert(value)
        else:
            print("Queue not created")

    def dequeue(self):
        if not self.isEmpty():
            tmp = self.lst.head
            self.lst.head = self.lst.head.next
            return tmp
        raise ValueError("Empty list")

    def peek(self):
        if not self.isEmpty():
            return self.lst.head.value
        raise ValueError("Empty list")

    def isEmpty(self):
        return self.lst is None or self.lst.head is None

    def deleteQueue(self):
        self.lst = None
    def test_search_whenItemIsAvailable_returnsNode(self):
        sl = SingleLinkedList()
        sl.add(1)
        sl.add(2)

        item1 = sl.search(1)
        item2 = sl.search(2)
        self.assertTrue(item1.value == 1)
        self.assertTrue(item2.value == 2)
    def test_reverse_whenListReversed_returnsAppropriateHeadAndTail(self):
        sl = SingleLinkedList()
        sl.add(1)
        sl.add(2)
        sl.add(3)

        sl.reverse()
        self.assertEqual(sl.head.value, 3)
        self.assertEqual(sl.tail.value, 1)
Exemple #4
0
def sumForward(l1, l2):
    if (l1 is None or l1.head is None):
        return l2
    if (l2 is None or l2.head is None):
        return l1

    p1 = l1.head
    p2 = l2.head

    # Sum the heads

    value = int(p1.data + p2.data) % 10
    carry = int(p1.data + p2.data) // 10

    sum = SingleLinkedList(Node(value))

    # Loop over p1

    while not p1.next is None:
        if not p2.next is None:
            value = int(p1.next.data + p2.next.data + carry) % 10
            carry = int(p1.next.data + p2.next.data + carry) // 10
            sum.appendToTail(value)
            p2 = p2.next
        else:
            value = int(p1.next.data + carry) % 10
            carry = int(p1.next.data + carry) // 10
            sum.appendToTail(value)
        p1 = p1.next

    # p1 is done. Check if there is anything left in p2 to be added.

    while not p2.next is None:
        value = int(p2.next.data + carry) % 10
        carry = int(p2.next.data + carry) // 10
        sum.appendToTail(value)
        p2 = p2.next

    # Add the carry

    if carry > 0:
        sum.appendToTail(carry)

    return sum
    def test_remove_whenElemIsTail_returnsRemovedNode(self):
        sl = SingleLinkedList()
        sl.add(1)
        sl.add(2)
        sl.add(3)
        removed = sl.remove(3)

        self.assertEqual(removed.value, 3)
    def test_remove_whenElemInMiddle_returnsRemovedNode(self):
        sl = SingleLinkedList()
        sl.add(11)
        sl.add(32)
        sl.add(44)
        sl.add(99)
        removedNode = sl.remove(44)

        self.assertTrue(removedNode.value, 44)
Exemple #7
0
def partition(sll, x):
    sll_gx = None
    n = sll.head
    # Check for single node list
    if (n.next == None):
        return sll

    if n.data >= x:
        sll.deleteNode(n.data)
        sll_gx.appendToTail(n.data)

    while (n.next != None):
        if (n.next.data >= x):
            new_node = Node(n.next.data)

            #This already skips the n.next if ends up deleted
            sll.deleteNode(n.next.data)
            if sll_gx == None:
                sll_gx = SingleLinkedList(new_node)
            else:
                sll_gx.appendToTail(new_node.data)
        else:
            n = n.next

    sll.printList()
    sll_gx.printList()

    sll.attach(sll_gx.head)

    sll.printList()

    return sll
    def test_add_updatesTailCorrectly(self):
        sl = SingleLinkedList()
        sl.add(1)
        sl.add(2)
        sl.add(100)

        self.assertEqual(sl.tail.value, 100)
Exemple #9
0
def main():
    n = Node(3)
    sll = SingleLinkedList(n)
    buffer = [5, 8, 5, 10, 2, 1]

    for b in buffer:
        sll.appendToTail(b)

    sll.printList()
    print("Partioning...")
    partition(sll, 5)
Exemple #10
0
                l1 = l1.next
            else:
                l3.next = l2
                l2 = l2.next
            l3 = l3.next
        if l1:
            l3.next = l1

        if l2:
            l3.next = l2

        return dummy.next


if __name__ == '__main__':
    test_list = SingleLinkedList()

    for i in range(1, 2):
        test_list.append(i)

    head = test_list.head

    opt_list = Solution()

    # ret_head = opt_list.removeNthFromEnd(head, 1)

    # ret_head = opt_list.reverseList(head)

    # ret_head = opt_list.swapPairs(head)

    l1 = SingleLinkedList()
    def test_search_whenItemIsNotAvailable_returnsFalse(self):
        sl = SingleLinkedList()
        sl.add(1)
        item2 = sl.search(2)

        self.assertEqual(item2, False)
Exemple #12
0
def main():

    n = Node(0)
    sll = SingleLinkedList(n)
    sll.appendToTail(1)
    sll.appendToTail(2)
    sll.appendToTail(3)
    for i in range(4):
        sll.appendToTail(0)

    for i in range(3):
        sll.appendToTail(3)
    for i in range(6):
        sll.appendToTail(0)
    for i in range(2):
        sll.appendToTail(6)

    sll.printList()
    removeDups(sll)

    print("After duplicate removal")
    sll.printList()

    for i in range(4):
        sll.appendToTail(0)

    for i in range(3):
        sll.appendToTail(3)
    for i in range(6):
        sll.appendToTail(0)
    for i in range(2):
        sll.appendToTail(6)

    print("After duplicate removal no buff")
    removeDups_noBuf(sll)
    sll.printList()
 def test_search_whenHeadIsNone_returnsFalse(self):
     sl = SingleLinkedList()
     returnValue = sl.search(1)
     self.assertEqual(False, returnValue)
    def test_add_addingFirstElem_savesAsHead(self):
        sl = SingleLinkedList()
        sl.add(1)

        self.assertEqual(sl.head.value, 1)
Exemple #15
0
def sumBackward(l1, l2):

    if (l1 is None or l1.head is None):
        return l2
    if (l2 is None or l2.head is None):
        return l1

    length1 = GetSize(l1)
    length2 = GetSize(l2)

    print("Sizes: ", length1, length2)

    diff = length1 - length2

    if (diff > 0):
        for i in range(diff):
            l2.appendToHead(0)
    elif (diff < 0):
        for i in range(abs(diff)):
            l1.appendToHead(0)

    #l1.printList()
    #l2.printList()

    p1 = l1.head
    p2 = l2.head

    #Create a sum linked list holding 0

    sum = SingleLinkedList(Node(0))

    #p3 points to the end of the list all the time.
    p3 = sum.head

    value = int(p1.data + p2.data) % 10
    carry = int(p1.data + p2.data) // 10

    # Add the element and update p3
    p3 = attachElement(p3, value, carry)

    # Run through p1
    while not p1.next is None:
        if (not p2.next is None):
            value = int((p1.next.data + p2.next.data)) % 10
            carry = int(p1.next.data + p2.next.data) // 10
            p3 = attachElement(p3, value, carry)
            p2 = p2.next
        else:
            p3 = attachElement(p3, p1.next.data, carry)
            #At this point carry will be always 0
            carry = 0

        p1 = p1.next

    # Now we just need to attach p2 in the case p1 was smaller

    while (not p2.next is None):
        p3 = attachElement(p3, p2.next.data, carry)
        carry = 0
        p2 = p2.next

    #Check if sum head is 0. If is 0, then remove the head. Will return an empty list, then

    if (sum.head.data == 0):
        sum.deleteNode(0)

    return sum
from LinkedList import SingleLinkedList

items = SingleLinkedList()
items.add_first(1)
items.add_last(2)
items.add_last(3)
items.add_last(4)
items.add_last(5)

items.print_out()
print(items.indexOf(2))

items.delete_first()
items.print_out()

items.delete_last()
items.print_out()

items.reverse()
items.print_out()

print(items.get_kth_from_end(2))
    def test_remove_whenElemNotPresent_returnsFalse(self):
        sl = SingleLinkedList()

        removed = sl.remove(1)

        self.assertFalse(removed)
    def test_add_addingSecondElem_hasLengthOfTwo(self):
        sl = SingleLinkedList()
        sl.add(1)
        sl.add(2)

        self.assertEqual(len(sl), 2)
Exemple #19
0
 def createQueue(self):
     self.lst = SingleLinkedList()
Exemple #20
0
def main():

    ll1 = SingleLinkedList(Node(7))
    ll1.appendToTail(1)
    ll1.appendToTail(6)

    ll2 = SingleLinkedList(Node(5))
    ll2.appendToTail(9)
    ll2.appendToTail(2)

    ll1.printList()
    printForward(ll1)

    ll2.printList()
    printForward(ll2)

    sum = sumForward(ll1, ll2)

    ll1 = SingleLinkedList(Node(1))
    ll2 = SingleLinkedList(Node(9))
    ll2.appendToTail(9)

    sum = sumForward(ll1, ll2)
    printForward(sum)

    ll1 = SingleLinkedList(Node(6))
    ll1.appendToTail(1)
    ll1.appendToTail(7)

    ll2 = SingleLinkedList(Node(2))
    ll2.appendToTail(9)
    ll2.appendToTail(5)

    printBackward(ll1)
    printBackward(ll2)

    print("Summing backwards")
    sum = sumBackward(ll1, ll2)
    printBackward(sum)

    print("Special Case")
    ll1 = SingleLinkedList(Node(1))
    ll1.appendToTail(2)
    ll1.appendToTail(3)
    ll1.appendToTail(4)

    ll2 = SingleLinkedList(Node(5))
    ll2.appendToTail(6)
    ll2.appendToTail(7)

    sum = sumBackward(ll1, ll2)
    printBackward(sum)