Esempio n. 1
0
def alternateSegregate(l):
    if l.head is None:
        return
    first, second = LinkedList(), LinkedList()
    lastNode = l.head
    while lastNode.next:
        lastNode = lastNode.next
    current = l.head
    lNode = lastNode
    prev = None
    while current != lastNode:
        next = current.next
        current.next = next.next
        lNode.next = next
        next.next = None
        lNode = lNode.next
        prev = current
        current = current.next
        if lNode == lastNode:
            break
    first.head = l.head
    second.head = current if lastNode == lNode else current.next
    if lastNode != lNode:
        current.next = None
    else:
        print current.data
        prev.next = None
    return [first, second]
    def test_len_3_elements(self):
        linked_list = LinkedList()
        linked_list.add_in_tail(self.n1)
        linked_list.add_in_tail(self.n2)
        linked_list.add_in_tail(self.n3)

        self.assertEqual(linked_list.len(), 3)
Esempio n. 3
0
def mergeKSortedLists(lists):
    while len(lists) > 1:
        p, q = lists.pop(0), lists.pop(0)
        lists.append(merge(p, q))
    l = LinkedList()
    l.head = lists[0]
    return l
def test_merge():
    print("Merging, expect to see 2, 3, 5, 10, 15, 20")

    # initialize the given first list
    first_head = Node(5)
    first_list = LinkedList(first_head)
    first_list.add_end(10)
    first_list.add_end(15)

    # initialize the given second list
    second_head = Node(2)
    second_list = LinkedList(second_head)
    second_list.add_end(3)
    second_list.add_end(20)

    new_list = first_list.sorted_merge(second_list)
    new_list.to_string()
    def test_clean(self):
        linked_list = LinkedList()
        linked_list.add_in_tail(self.n1)
        linked_list.add_in_tail(self.n2)
        linked_list.add_in_tail(self.n3)
        linked_list.clean()

        self.assertEqual(linked_list.len(), 0)
    def test_delete_all_1(self):
        linked_list = LinkedList()
        linked_list.add_in_tail(self.n1)
        linked_list.add_in_tail(self.n2)

        linked_list.delete(12, True)
        self.assertEqual(linked_list.head, None)
        self.assertEqual(linked_list.tail, None)
    def test_delete_5(self):
        linked_list = LinkedList()
        linked_list.add_in_tail(self.n1)
        linked_list.add_in_tail(self.n2)

        linked_list.delete(12)
        self.assertEqual(linked_list.head, self.n2)
        self.assertEqual(linked_list.tail, self.n2)
Esempio n. 8
0
def sort(l):
    a = LinkedList()
    node = l.head
    while node:
        temp = node.next
        node.next = None
        sortedInsert(a, node)
        node = temp
    l.head = a.head
    def test_find_all_5(self):
        linked_list = LinkedList()
        linked_list.add_in_tail(self.n1)
        linked_list.add_in_tail(self.n2)
        linked_list.add_in_tail(self.n3)
        linked_list.add_in_tail(self.n4)
        linked_list.add_in_tail(self.n5)

        self.assertEqual(linked_list.find_all(12), [self.n1, self.n2, self.n4, self.n5])
    def test_insert_2(self):
        linked_list = LinkedList()
        linked_list.add_in_tail(self.n1)
        linked_list.add_in_tail(self.n2)
        linked_list.add_in_tail(self.n3)

        linked_list.insert(self.n3, self.n4)

        self.assertEqual(self.n3.next, self.n4)
        self.assertEqual(linked_list.tail, self.n4)
    def test_insert_3(self):
        linked_list = LinkedList()
        linked_list.add_in_tail(self.n1)
        linked_list.add_in_tail(self.n2)
        linked_list.add_in_tail(self.n3)

        linked_list.insert(self.n2, self.n6)

        self.assertEqual(self.n2.next, self.n6)
        self.assertEqual(self.n6.next, self.n3)
    def test_delete_4(self):
        linked_list = LinkedList()
        linked_list.add_in_tail(self.n3)
        linked_list.add_in_tail(self.n4)
        linked_list.add_in_tail(self.n6)
        
        linked_list.delete(12)

        self.assertEqual(linked_list.head, self.n3)
        self.assertEqual(self.n3.next, self.n6)
        self.assertEqual(linked_list.tail, self.n6)
def intersectionOfSortedLists(a, b):
    result = LinkedList()
    while a and b:
        if a.data == b.data:
            result.append(a.data)
            a, b = a.next, b.next
        elif a.data > b.data:
            b = b.next
        else:
            a = a.next
    return result
def initialize():
    head = Node(-1)
    linked = LinkedList(head)

    # Create continuous node to head
    for i in range(0, 5):
        linked.add_end(i)

    # Printing out the result
    print("Print Linked List Iterate Result")
    # expected to show) -1, 0, 1, 2, 3, 4
    linked.to_string()

    return head, linked
    def test_delete_all_3(self):
        linked_list = LinkedList()
        linked_list.add_in_tail(self.n1)
        linked_list.add_in_tail(self.n2)
        linked_list.add_in_tail(self.n3)
        linked_list.add_in_tail(self.n4)
        linked_list.add_in_tail(self.n5)
        linked_list.add_in_tail(self.n6)
        linked_list.add_in_tail(self.n7)
        linked_list.add_in_tail(self.n8)


        linked_list.delete(12, True)
        self.assertEqual(linked_list.head, self.n3)
        self.assertEqual(linked_list.tail, self.n6)
        self.assertEqual(self.n3.next, self.n6)
Esempio n. 16
0
def add(a, b):
    result = LinkedList()
    lastNode = None
    carry = 0
    while a and b:
        sum = a.data + b.data + carry
        if lastNode:
            lastNode.next = Node(sum % 10)
            lastNode = lastNode.next
        else:
            result.head = Node(sum % 10)
            lastNode = result.head
        carry = sum / 10
        a, b = a.next, b.next
    while a:
        sum = a.data + carry
        if lastNode:
            lastNode.next = Node(sum % 10)
            lastNode = lastNode.next
        else:
            result.head = Node(sum % 10)
            lastNode = result.head
        carry = sum / 10
        a = a.next
    while b:
        sum = b.data + carry
        if lastNode:
            lastNode.next = Node(sum % 10)
            lastNode = lastNode.next
        else:
            result.head = Node(sum % 10)
            lastNode = result.head
        carry = sum / 10
        b = b.next
    if carry == 1:
        if lastNode:
            lastNode.next = Node(1)
        else:
            result.head = Node(1)
    return result
def load_network(graph, network):
    """
    Loads a network from a given file or object(if saved earlier)
    :param graph: Graph Object
    :param network: network file
    :return: None
    """
    network = open(network, "r")  # open given file
    for line in network:  # iterate over whole file line by line
        line = line.rstrip().lstrip(
        )  # removes any white spaces after or before the node value
        if ':' not in line:  # if line has node to add
            graph.list.add_last(LinkedList(line))  # add node to the grpah
        else:  # if line has nodes to connect
            vertices = line.split(
                ':'
            )  # makes list of two nodes by spliting the line on delimiter
            vertices[0] = vertices[0].rstrip().lstrip()  #
            vertices[1] = vertices[1].rstrip().lstrip()
            graph.connect(vertices[0],
                          vertices[1])  # make a connection between node
    network.close()  # close the open file
def merge(a, b):
    l = LinkedList()
    while a and b:
        if a.data <= b.data:
            temp = a.next
            a.next = l.head
            l.head = a
            a = temp
        else:
            temp = b.next
            b.next = l.head
            l.head = b
            b = temp
    while a:
        temp = a.next
        a.next = l.head
        l.head = a
        a = temp
    while b:
        temp = b.next
        b.next = l.head
        l.head = b
        b = temp
    return l
Esempio n. 19
0
def add(a, b):
    l1 = a.size()
    l2 = b.size()
    p = a.head if l1 > l2 else b.head
    q = a.head if l1 <= l2 else b.head
    d = abs(l1 - l2)
    s = []
    while d > 0:
        s.append([p.data, 0])
        p = p.next
        d -= 1
    while p and q:
        s.append([(p.data + q.data) % 10, (p.data + q.data) / 10])
        p = p.next
        q = q.next
    l = LinkedList()
    carry = 0
    while len(s) > 0:
        node = s.pop()
        l.push((node[0] + carry) % 10)
        carry = node[1] + (node[0] + carry) / 10
    if carry > 0:
        l.push(carry)
    return l
Esempio n. 20
0
from list import Node


def mergeRecursive(a, b):
    if a is None:
        return b
    if b is None:
        return a
    if a.data <= b.data:
        a.next = mergeRecursive(a.next, b)
        return a
    else:
        b.next = mergeRecursive(a, b.next)
        return b


l = LinkedList()
l.append(1)
l.append(2)
l.append(4)
l.printList()
s = LinkedList()
s.append(3)
s.append(4)
s.append(5)
s.printList()

m = LinkedList()
m.head = mergeRecursive(l.head, s.head)
m.printList()
Esempio n. 21
0
    s = []
    while d > 0:
        s.append([p.data, 0])
        p = p.next
        d -= 1
    while p and q:
        s.append([(p.data + q.data) % 10, (p.data + q.data) / 10])
        p = p.next
        q = q.next
    l = LinkedList()
    carry = 0
    while len(s) > 0:
        node = s.pop()
        l.push((node[0] + carry) % 10)
        carry = node[1] + (node[0] + carry) / 10
    if carry > 0:
        l.push(carry)
    return l


n1 = LinkedList()
s1 = "1999"
s2 = "1001"
for i in s1:
    n1.append(int(i))
n1.printList()
n2 = LinkedList()
for i in s2:
    n2.append(int(i))
n2.printList()
add(n1, n2).printList()
    def test_len_0_elements(self):
        linked_list = LinkedList()

        self.assertEqual(linked_list.len(), 0)
    def test_insert_1(self):
        linked_list = LinkedList()

        linked_list.insert(None, self.n1)
        self.assertEqual(linked_list.head, self.n1)
        self.assertEqual(linked_list.tail, self.n1)
Esempio n. 24
0
print("\_____/_|_| |_|_|\_\___|\__,_| \_____/_|___/\__|")

menu = """
    ╔═══ MENU ════════════════════════╗
    ║    1: Insert begin              ║
    ║    2: Insert end                ║
    ║    3: Insert position           ║
    ║    4: Delete begin              ║
    ║    5: Delete end                ║
    ║    6: Delete in position        ║
    ║    7: Insert random values      ║
    ║    0: Exit                      ║
    ╚═════════════════════════════════╝
"""

llist = LinkedList()

if __name__ == "__main__":

    while True:
        print(menu)

        option = int(input("Select an option: "))

        if option == 0:
            break

        elif option == 1:
            value = int(input("Type a value to insert at the beginning: "))
            llist.insert_begin(value)
    def test_find_all_4(self):
        linked_list = LinkedList()
        linked_list.add_in_tail(self.n1)

        self.assertEqual(linked_list.find_all(874), [])
Esempio n. 26
0
    a = [0] * 3
    current = node
    while current:
        a[current.data] += 1
        current = current.next
    current = node
    i = 0
    while current and i < 3:
        current.data = i
        current = current.next
        a[i] -= 1
        if a[i] == 0:
            i += 1


l = LinkedList()
l.append(1)
l.append(0)
l.append(2)
l.append(2)
l.append(1)
l.append(1)
l.append(1)
l.append(0)
l.append(0)
l.append(0)
l.append(2)
l.append(1)
l.append(1)
l.append(2)
l.append(1)
Esempio n. 27
0
from list import LinkedList
from node import Node

print "\nCreating LinkedList from python list"
my_list = LinkedList([Node(2), None, Node(0), Node(0), Node(4)])
my_list.to_string()

print "\nAdding single node"
my_list.add_node(Node(5))
my_list.to_string()

print "\nAdding nodes from python tuple"
my_list.add_nodes((Node(0), Node(20), Node(-1)))
my_list.to_string()
    def test_find_all_2(self):
        linked_list = LinkedList()

        self.assertEqual(linked_list.find_all(57), [])
Esempio n. 29
0
    if l1 > l2:
        d = l1 - l2
        while d > 0:
            p = p.next
            d -= 1
    elif l2 < l1:
        d = l1 - l2
        while d > 0:
            q = q.next
            d -= 1
    while p != q:
        p, q = p.next, q.next
    return p.data


a = LinkedList()
b = LinkedList()

for i in range(8):
    a.append(i)

for j in range(8, 10):
    b.append(j)

b.head.next.next = a.head.next.next.next.next.next

a.printList()
b.printList()

print intersectionPoint(a, b)
    def test_find_all_3(self):
        linked_list = LinkedList()
        linked_list.add_in_tail(self.n1)

        self.assertEqual(linked_list.find_all(12), [self.n1])