Beispiel #1
0
from SLinkedList import SLinkedList

# Test code
if __name__ == '__main__':
    List1 = SLinkedList()
    List2 = SLinkedList()

    List1.append(5)
    List1.append(7)
    List1.append(8)
    List1.add_atindex(val=10, index=1)
    print(List1)

    List2.append_list(val_list=[20, 55, 84, 37])
    print(List2)

    print(List1 + List2)
    print("--------------------")
    print(List1)
    print(List2)
    print(List2 + List1)
    print("--------------------")
    List1 += 3
    print(List1)
    List1 -= 3
    print(List1)
def kth_to_last_element(l_list, k):
    p1 = p2 = l_list.head

    for i in range(k):
        if p2 is None:
            return None
        p2 = p2.next

    while p2:
        p1 = p1.next
        p2 = p2.next

    return p1


s = SLinkedList()

nums = [1, 2, 8, 6, 5, 2, 7, 6, 5, 9, 2, 10]
nums = [11, 22, 33]

for num in nums:
    if not s.head:
        s.head = Node(num)
        curr = s.head

    else:
        n = Node(num)
        curr.next = n
        curr = n

while True:
Beispiel #3
0
        # on the same node, a cycle exists
        if slow == fast:
            return True

        # progress the slow pointer
        slow = slow.next

        # if fast
        if fast.next and fast.next.next:
            fast = fast.next.next
        else:
            return False


# -------------------------------------------------- #
s = SLinkedList()

s.head = Node(1)

n = s.head
for i in range(10):
    n.next = Node(i)
    # save node to create cycle
    if i == 3:
        loop_node = n
    n = n.next

# cycle last node back in list
n.next = loop_node

print(has_cycle(s.head))
import sys
sys.path.append(r"C:\Users\Sasha\Coding\Algorithms and Data Structures\Algorithms in Python\LinkedList")
from SLinkedList import SLinkedList


def has_cycle(head):
    """Floyd's Cycle-Finding Algorithm.
    Also known as Tortoise and Hare Algorithm."""
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow is fast:
            return True
    return False


L = SLinkedList()
for i in range(1,8): L.add_start(i)
tmp = L.head
while tmp.next: tmp = tmp.next
tmp.next = L.head.next.next.next.next
print(has_cycle(L.head))



Beispiel #5
0
from SLinkedList import SLinkedList

s = SLinkedList()
s.insertAtHead(2)
s.insertAtHead(3)
s.insertAtTail(10)
s.insertAtTail(12)
s.deleteFromTail()
s.deleteFromHead()
s.print()
 def __init__(self, *args, **kwargs):
     super(TestSLinkedListMethods, self).__init__(*args, **kwargs)
     self.x = SLinkedList()
class TestSLinkedListMethods(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(TestSLinkedListMethods, self).__init__(*args, **kwargs)
        self.x = SLinkedList()

    def test_empty(self):
        self.assertEqual(self.x.empty(), True)

    def test_push_back(self):
        self.x.push_back(10)
        self.x.push_back(20)
        self.x.push_back(30)

    def test_front_back(self):
        self.x.push_back(10)
        self.x.push_back(20)
        self.x.push_back(30)
        self.assertEqual(self.x.front(), 10)
        self.assertEqual(self.x.back(), 30)

    def test_insert(self):
        self.x.push_back(10)
        self.x.push_back(20)
        self.x.push_back(30)
        self.assertEqual(self.x.front(), 10)
        self.assertEqual(self.x.back(), 30)

    def test_find(self):
        self.x.push_back(10)
        self.x.push_back(20)
        self.x.push_back(30)
        self.assertEqual(self.x.find(30).value, 30)