Example #1
0
    curr = head
    end = True
    while curr.next:
        if curr.data >= node.data:
            end = False
            break
        prev = curr
        curr = curr.next
    if not prev: head = node

    if end:
        curr.next = node
    else:
        node.next = curr
        if prev: prev.next = node
    return head

if __name__ == '__main__':
    linked_list = LinkedList()
    llist = [1, 2, 3, 5, 6, 7, 8, 9]
    linked_list.create_linked_list(input_list=llist)
    print("Linked list before")
    linked_list.print_list()
    current_head = linked_list.head
    node4 = Node(data=4)

    current_head = insert_node(head=current_head, node=node4)
    linked_list.head = current_head
    print("Linked list after")
    linked_list.print_list()
Example #2
0
        prev = cur
        cur = temp
    return prev


def reverse_rec(head):
    def recursive(node, prev):
        if not node:
            return prev
        temp = node.next
        node.next = prev
        return recursive(temp, node)

    return recursive(head, None)


if __name__ == "__main__":
    for func in [reverse, reverse_rec]:
        head = Node(1)
        head.append_tail(2)
        head.append_tail(3)
        assert str(head) == "1->2->3"
        head = func(head)
        assert str(head) == "3->2->1"
        head.append_tail(0)
        head = func(head)
        assert str(head) == "0->1->2->3"
        head = head.append_head(-1)
        head = func(head)
        assert str(head) == "3->2->1->0->-1"
from linked_list import Node
from stack import Stack

## Stack Test Cases
## Set up seme Elements
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)

# Start setting up a Stack
stack = Stack(node1)

# Test stack functionality
stack.push(node2)
stack.push(node3)
print(stack.pop().value)
print(stack.pop().value)
print(stack.pop().value)
print(stack.pop())
stack.push(node4)
print(stack.pop().value)
Example #4
0
 def test_size(self):
     self.assertEqual(size(Node(1, Node(2, None))), 2)
     self.assertEqual(size(None), 0)
     self.assertEqual(size(Node(1, Node(2, Node(3, None)))), 3)
 def push_front(self, data):
     node = Node(data)
     self.list.prepend(node)
Example #6
0
 def test_search(self):
     self.assertEqual(search(Node(1, Node(2, None)), 1), 0)
     self.assertEqual(search(Node(1, Node(2, None)), 70), None)
     self.assertEqual(search(Node(20, Node(50, None)), 50), 1)
Example #7
0
 def test_remove(self):
     self.assertEqual(remove(Node(1, Node(2, None)), 10),
                      Node(1, (Node(2, None))))
     self.assertEqual(remove(None, 20), None)
     self.assertEqual(remove(Node(1, Node(2, Node(3, None))), 2),
                      Node(1, Node(3, None)))
Example #8
0
def pad_with_zeros(head, count):
    for i in range(count):
        node = Node(0)
        node.next, head = head, node  # insert at front
    return head
import sys
import random
from linked_list import LinkedList, Node
from print_nodes import print_nodes

if __name__ == "__main__":
    n = int(sys.argv[1])
    items = []
    for _ in range(n):
        items.append(random.randrange(150))

    print(items)
    ll = LinkedList()
    for item in items:
        if ll.length == 0:
            pos = 0
        else:
            pos = random.randrange(ll.length)
        print("inserting at index: {}".format(pos))
        ll.insert(Node(item), pos)
        print_nodes(ll)
Example #10
0
def test_node_init():
    n = Node(3)

    assert n.val == 3
    assert n.next is None
Example #11
0
    if length == 1:
        return node.next
    if length == 2:
        if node.value == node.next.value:
            return node.next.next
        else:
            return -1
    last_node = visit(node.next, length - 2)
    if last_node != -1:
        if node.value == last_node.value:
            return last_node.next
    return -1


def palindrome(head):
    length = 0
    node = head
    while node:
        length += 1
        node = node.next
    print visit(head, length)


head = Node('r')
head.next = Node('a')
head.next.next = Node('d')
head.next.next.next = Node('a')
head.next.next.next.next = Node('r')

palindrome(head)  # None if palindrome, -1 otherwise
Example #12
0
    fast_ptr = 2 * slow_ptr
    x + 2y + z = 2 * (x + y)
    => x = z
    which is basically distance each pointer travels from the current position to loop starting
    And this is represented in the loop below 
    """

    slow_ptr = head
    while (slow_ptr != fast_ptr):
        slow_ptr = slow_ptr.next
        fast_ptr = fast_ptr.next
    return slow_ptr


if __name__ == '__main__':
    node1 = Node(data=1)
    node2 = Node(data=2)
    node3 = Node(data=3)
    node4 = Node(data=4)
    node5 = Node(data=5)
    node6 = Node(data=6)
    node7 = Node(data=7)
    node8 = Node(data=8)
    node9 = Node(data=9)

    # creating circcular linked list`
    node1.next = node2
    node2.next = node3
    node3.next = node4
    node4.next = node5
    node5.next = node6
Example #13
0
from linked_list import Node

head1 = Node(None)
node_1 = Node(1)
node_2 = Node(2)
node_3 = Node(4)
tail = Node(None)
head1.next = node_1
node_1.next = node_2
node_2.next = node_3
node_3.next = tail

head2 = Node(None)
node_1 = Node(1)
node_2 = Node(3)
node_3 = Node(4)
tail = Node(None)
head2.next = node_1
node_1.next = node_2
node_2.next = node_3
node_3.next = tail


def mergeTwolist(l1, l2):
    if (not l1) or (l2 and (l1.data > l2.data)):
        l1, l2 = l2, l1
    if l1:
        l1.next = mergeTwolist(l1.next, l2)
Example #14
0
        ll.push(Node(p2.data))
        p2 = p2.next
    return ll


if __name__ == "__main__":
    n, m = int(sys.argv[1]), int(sys.argv[2])
    items1 = []
    for _ in range(n):
        items1.append(random.randrange(151))

    items2 = []
    for _ in range(m):
        items2.append(random.randrange(151))

    items1.sort(reverse=True)
    items2.sort(reverse=True)

    l1 = LinkedList()
    for item in items1:
        l1.push(Node(item))

    l2 = LinkedList()
    for item in items2:
        l2.push(Node(item))

    print_nodes(l1)
    print_nodes(l2)
    ll = merge(l1, l2)
    print()
    print_nodes(ll)
Example #15
0
 def test_insert(self):
     self.assertEqual(insert(Node(1, Node(2, None)), 3, 1),
                      Node(1, Node(3, Node(2, None))))
     self.assertEqual(insert(Node(1, Node(2, None)), 50, 0),
                      Node(50, Node(1, Node(2, None))))
     self.assertRaises(IndexError, insert, None, 2, 7)
     self.assertEqual(insert(Node(2, None), 2, 1), Node(2, Node(2, None)))
def test_node_instance():
    """Test new instance of Node obj."""
    from linked_list import Node
    assert type(Node()) == Node
Example #17
0
 def test_get(self):
     self.assertEqual(get(Node(1, Node(2, None)), 1), 2)
     self.assertEqual(get(Node(20, Node(12, None)), 0), 20)
     self.assertRaises(IndexError, get, Node(1, Node(2, None)), 7)
Example #18
0
 def test_init(self):
     data = 'ABC'
     node = Node(data)
     # Initializer should add instance properties
     assert node.data is data
     assert node.next is None
Example #19
0
 def test_contains(self):
     self.assertTrue(contains(Node(1, Node(2, None)), 1))
     self.assertFalse(contains(Node(1, Node(92, None)), 70))
     self.assertTrue(contains(Node(20, Node(50, None)), 50))
Example #20
0
def test_node_creation():
    """Test new node."""
    from linked_list import Node
    new_node = Node("word")
    assert isinstance(new_node, Node)
    assert new_node.data == "word"
Example #21
0
 def test_pop(self):
     self.assertEqual(pop(Node(1, Node(2, Node(3, None))), 2),
                      (Node(1, Node(2, None)), 3))
     self.assertRaises(IndexError, pop, None, 7)
     self.assertEqual(pop(Node(1, Node(2, Node(3, None))), 0),
                      (Node(2, Node(3, None)), 1))
Example #22
0
def test_get_data():
    """Test get_data method."""
    from linked_list import Node
    new_node = Node("word")
    assert new_node.get_data() == "word"
Example #23
0
 def test_str(self):
     self.assertEqual(str(Node(3, None)), "Node(3, None)")
     self.assertEqual(str(None), "None")
     self.assertEqual(str(Node(8, Node(7, None))), "Node(8, Node(7, None))")
Example #24
0
def test_get_next():
    """Test get_next method."""
    from linked_list import Node
    new_node = Node("word", "next")
    assert new_node.get_next() == "next"
 def push_back(self, data):
     node = Node(data)
     self.list.append(node)
Example #26
0
def test_set_next():
    """Test get_next method."""
    from linked_list import Node
    new_node = Node("word", "chimichanga")
    new_node.set_next("next")
    assert new_node.get_next() == "next"
            stack1.append(p)
            p = p.next
        while q is not None:
            stack2.append(q)
            q = q.next
        p, q = stack1.pop(), stack2.pop()
        # 公共节点在末尾
        if p == q:
            return p
        while len(stack1) != 0 and len(stack2) != 0:
            p, q = stack1.pop(), stack2.pop()
            if p != q:
                return p.next
        return None

node1, node2, node3, node4, node5, node6, head1 = Node(1), Node(2), Node(3), Node(4), Node(5), Node(6), Node(7)
head1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5

s = Solution()
# 公共节点在中间
# head2 = Node(8)
# head2.next = node3

# 公共节点在开头
# head2 =head1

# 公共节点在末尾
head2 = Node(8)
Example #28
0
    return curr


def reverse_list2(head):
    if not head:
        return

    prev, curr = None, head
    while True:
        nnext = curr.next
        curr.next = prev
        if nnext:
            curr = nnext
            prev = curr
        else:
            return curr


if __name__ == '__main__':
    import random
    from linked_list import Node
    l = []
    for i in range(11):
        l.append(random.randint(0, 100))

    head = Node.new(l)
    head.bianli()

    reverse_list(head)
    reverse_list(Node(23))
 def test_init(self):
     data = 'ABC'
     node = Node(data)
     assert node.data is data
     assert node.next is None
Example #30
0
def test_node_has_given_attribute_values():
    """Test if new Node has given attribute values."""
    from linked_list import Node
    test_node = Node(5, 5)
    assert test_node.data and test_node.next == 5