Exemple #1
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
Exemple #2
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 #3
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)
Exemple #4
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()