Esempio n. 1
0
def main():
    ll1 = SinglyLinkedList()
    ll2 = SinglyLinkedList()
    for i in xrange(8):
        ll1.append(i)
    for i in [-1, 0, 2, 7, 8, 9]:
        ll2.append(i)

    merged_ll_head = merge_linked_lists(ll1.head, ll2.head)

    expected = [-1, 0, 0, 1, 2, 2, 3, 4, 5, 6, 7, 7, 8, 9]
    for num in expected:
        assert num == merged_ll_head.val
        merged_ll_head = merged_ll_head.next

    ll3 = SinglyLinkedList()
    ll4 = SinglyLinkedList()
    for i in xrange(8):
        ll3.append(i)
    for i in [-1, 0, 2, 7, 8, 9]:
        ll4.append(i)

    merged_ll_head = merge_linked_lists_r(ll3.head, ll4.head)

    expected = [-1, 0, 0, 1, 2, 2, 3, 4, 5, 6, 7, 7, 8, 9]
    for num in expected:
        assert num == merged_ll_head.val
        merged_ll_head = merged_ll_head.next
Esempio n. 2
0
    def __init__(self, *args, **kwargs):
        super(TestInsert, self).__init__(*args, **kwargs)

        self.test_list = random.sample(range(0, RANDMAX), RANDMAX // 2)
        self.test_SinglyLinkedList = SinglyLinkedList()
        for i in self.test_list:
            self.test_SinglyLinkedList.insertAtEnd(i)
Esempio n. 3
0
def createEmptyLinkedList():
    ''' Creates an empty linked list.'''
    
    print('Creating Empty Linked List')
    
    linkedList = SinglyLinkedList()

    return linkedList
Esempio n. 4
0
def main():
    ll = SinglyLinkedList()
    for i in xrange(15):
        ll.append(i)

    rev = reverse_linked_list(ll.head)

    for i in reversed(xrange(15)):
        assert i == rev.val
        rev = rev.next
def split(sll: SinglyLinkedList, pos):
    leftPart = SinglyLinkedList()
    rightPart = SinglyLinkedList()

    disconnectedHead1 = sll.head
    disconnectedHead2 = None

    curr = sll.head
    try:
        for _ in range(pos-1):
            curr = curr.next
        disconnectedHead2 = curr.next
        curr.next = None
    except AttributeError:
        warnings.warn('Position passed is out of bounds', RuntimeWarning)

    leftPart.head = disconnectedHead1
    rightPart.head = disconnectedHead2

    return(leftPart, rightPart)
Esempio n. 6
0
def createNewLinkedList():
    ''' Creates a new linked list: 1 -> 2 -> 3 -> 4 -> 5. '''

    print('Creating a new linked list.')

    linkedList = SinglyLinkedList()

    linkedList.append(1)
    linkedList.append(2)
    linkedList.append(3)
    linkedList.append(4)
    linkedList.append(5)

    linkedList.iterate()

    return linkedList
def join(*slls):
    joinedSLL = SinglyLinkedList()
    disconnectedHead = None
    curr = None
    for sll in slls:
        for data in sll:
            new_node = Node(data)
            if curr is None:
                disconnectedHead = new_node
                curr = disconnectedHead
            else:
                curr.next = new_node
                curr = curr.next

    joinedSLL.head = disconnectedHead
    return joinedSLL
Esempio n. 8
0
def main():
    ll = SinglyLinkedList()
    ll.append(1)
    ll.append(1)
    ll.append(2)
    ll.append(5)
    ll.append(8)
    ll.append(8)
    ll.append(9)

    root = sorted_linked_list_to_bst(ll.head)

    expected = iter([1, 1, 2, 5, 8, 8, 9])

    for num in inorder_traversal(root):
        assert num == expected.next()
Esempio n. 9
0
 def __init__(self):
     self.ins = SinglyLinkedList()
from LinkedList import SinglyLinkedList 
from LinkedList import ListNode
import random
import math

def delete_middle_node(node):
    node.data = node.next.data
    node.next = node.next.next

ll = SinglyLinkedList()
ll.append('A')
ll.append('B')
MidNode = ListNode('C')
ll.append(MidNode)
ll.append('D')
ll.append('E')

print(ll)
delete_middle_node(MidNode)
print(ll)
Esempio n. 11
0
# Evan Johanns
# 2/20/20
from LinkedList import SinglyLinkedList

lst = SinglyLinkedList()
print(lst)

lst.prepend(42)
lst.prepend(4)
print(lst)
Esempio n. 12
0
 def test_one(self):
     self.assertTrue(are_equal(generate_list([1]), SinglyLinkedList(1)))
Esempio n. 13
0
 def __init__(self):
     self.new_list = SinglyLinkedList()
 def setUp(self):
     self.result = []
     self.obj = SinglyLinkedList()
Esempio n. 15
0
 def test_equal(self):
     lst1 = SinglyLinkedList(1)
     lst2 = SinglyLinkedList(1)
     self.assertTrue(are_equal(lst1, lst2))
Esempio n. 16
0
    def __init__(self, *args, **kwargs):
        super(TestInsert, self).__init__(*args, **kwargs)

        self.test_list = []
        self.test_SinglyLinkedList = SinglyLinkedList()
Esempio n. 17
0
 def test_one_empty(self):
     self.assertFalse(are_equal(SinglyLinkedList(1), None))
Esempio n. 18
0
 def test_multiple(self):
     lst2 = SinglyLinkedList(1)
     lst2.next = SinglyLinkedList(2)
     self.assertTrue(are_equal(generate_list([1, 2]), lst2))
Esempio n. 19
0
 def test_unequal(self):
     lst1 = SinglyLinkedList(1)
     lst2 = SinglyLinkedList(2)
     self.assertFalse(are_equal(lst1, lst2))
Esempio n. 20
0
from LinkedList import SinglyLinkedList


sample = SinglyLinkedList()

sample.append(1)
sample.prepend(3)
sample.append(2)
sample.insert(1, 5)
sample.print()
print("index 1: ",sample.access(1))
sample.set_head(2)
sample.print()
Esempio n. 21
0
 def test_extend(self):
     lst1 = SinglyLinkedList(1)
     lst2 = SinglyLinkedList(1)
     lst2.next = SinglyLinkedList(2)
     self.assertFalse(are_equal(lst1, lst2))