def testAppend(self):
     ll = CircularLinkedList()
     ll.append(1)
     ll.append(2)
     self.assertEqual(ll.head.data, 1)
     self.assertIsNotNone(ll.head.next)
     self.assertEqual(ll.head.next.data, 2)
     self.assertEqual(ll.head, ll.head.next.next)
 def testReturnLastNode(self):
     ll = CircularLinkedList()
     self.assertEqual(ll.return_last_node(), None)
     ll.append(1)
     self.assertEqual(ll.return_last_node().data, 1)
     ll.append(2)
     self.assertEqual(ll.return_last_node().data, 2)
 def testRemoveNthElement(self):
     ll = CircularLinkedList()
     ll.append(1)
     ll.append(2)
     ll.append(3)
     self.assertEqual(ll.head.data, 1)
     ll.remove_nth_element(0)
     self.assertEqual(ll.head.data, 2)
     ll.remove_nth_element(1)
     self.assertEqual(ll.head.data, 2)
     self.assertEqual(ll.head, ll.head.next)
 def testDetectLoop(self):
     ll = CircularLinkedList()
     self.assertFalse(ll.detect_loop())
     ll.append(1)
     self.assertTrue(ll.detect_loop())
     ll.append(2)
     ll.append(3)
     self.assertTrue(ll.detect_loop())
 def testLength(self):
     ll = CircularLinkedList()
     self.assertEqual(ll.length(), 0)
     ll.append(1)
     ll.append(2)
     ll.append(3)
     self.assertEqual(ll.length(), 3)
    def testInsertMiddle(self):
        ll = CircularLinkedList()
        ll.insert_middle(0, 0)
        self.assertEqual(ll.head.data, 0)
        ll.insert_middle(1, 1)
        self.assertEqual(ll.head.next.data, 1)
        ele = 100
        pos = 0
        ll.insert_middle(100, pos)
        self.assertEqual(ll.head.data, 100)

        pos = 1
        ll.insert_middle(200, pos)
        self.assertEqual(ll.head.data, 100)
        self.assertEqual(ll.head.next.data, 200)
 def testDelete(self):
     ll = CircularLinkedList()
     ll.append(1)
     ll.append(2)
     ll.delete(2)
     self.assertEqual(ll.head.data, 1)
     self.assertEqual(ll.head, ll.head.next)
     ll.delete(1)
     self.assertIsNone(ll.head)
    def test_index_assignment_success(self):
        lst1 = list(range(10))
        lst2 = sorted(lst1)
        linkedlist = CircularLinkedList(lst1)
        for i in range(len(lst1)):
            linkedlist[i] = lst2[i]
            assert linkedlist[i] == lst2[i]

        assert list(linkedlist) == lst2
Example #9
0
 def test_len(self):
     llist = CircularLinkedList()
     self.assertEqual(len(llist), 0)
     llist.from_list([1, 2, 3])
     self.assertEqual(len(llist), 3)
     llist.append(9)
     self.assertEqual(len(llist), 4)
 def testCopyList(self):
     ll1 = CircularLinkedList()
     ll2 = ll1.copy_list()
     self.assertIsNone(ll2.head)
     ll1.append(1)
     ll1.append(2)
     ll2 = ll1.copy_list()
     self.assertNotEqual(ll1.head, ll2.head)  # Make sure, it's a deep copy
     self.assertEqual(ll1.head.data, ll2.head.data)
     self.assertEqual(ll1.head.next.data, ll2.head.next.data)
     self.assertEqual(ll2.head.next.next, ll2.head)
Example #11
0
def josephus_permutation(n):
    l = CircularLinkedList()

    for i in range(1, n + 1):
        l.append(i)

    cur = l.head
    while cur.next != cur:
        l.delete_node(cur.next)
        cur = cur.next

    return cur.val
 def test_append_start_empty(self):
     linkedlist = CircularLinkedList()
     for i in range(10):
         linkedlist.append(i)
Example #13
0
 def __iter__(self):
     try:
         self.lock.acquire()
         CircularLinkedList.__iter__(self)
     finally:
         self.lock.release()
Example #14
0
 def add(self, data):
     try:
         self.lock.acquire()
         CircularLinkedList.add(self, data)
     finally:
         self.lock.release()
 def testSearch(self):
     ll = CircularLinkedList()
     ll.append(1)
     ll.append(2)
     self.assertTrue(ll.search(1))
     self.assertFalse(ll.search(100))
Example #16
0
 def peek(self):
     try:
         self.lock.acquire()
         return CircularLinkedList.peek(self)
     finally:
         self.lock.release()
Example #17
0
 def get(self, index):
     try:
         self.lock.acquire()
         return CircularLinkedList.get(self, index)
     finally:
         self.lock.release()
Example #18
0
 def clear(self):
     try:
         self.lock.acquire()
         CircularLinkedList.clear(self)
     finally:
         self.lock.release()
Example #19
0
 def index_of(self, data):
     try:
         self.lock.acquire()
         return CircularLinkedList.index_of(self, data)
     finally:
         self.lock.release()
Example #20
0
 def remove_index(self, index):
     try:
         self.lock.acquire()
         return CircularLinkedList.remove_index(self, index)
     finally:
         self.lock.release()
Example #21
0
 def print_list(self):
     try:
         self.lock.acquire()
         CircularLinkedList.print_list(self)
     finally:
         self.lock.release()
Example #22
0
from circular_linked_list import CircularLinkedList
from doubly_linked_list import DoublyLinkedList

print('Circular linked list')

circular_list = CircularLinkedList()
circular_list.insert_first(1)
circular_list.insert_first(2)
circular_list.insert_first(3)
circular_list.insert_last(4)
circular_list.insert_last(5)
circular_list.delete_first()
circular_list.display_list()

print('Doubly linked list')

doubly_linked_list = DoublyLinkedList()
doubly_linked_list.insert_first(10)
doubly_linked_list.insert_first(20)
doubly_linked_list.insert_first(30)
doubly_linked_list.insert_last(40)
doubly_linked_list.insert_last(50)
doubly_linked_list.delete_last()
doubly_linked_list.delete_last()
doubly_linked_list.delete_first()
doubly_linked_list.insert_first(60)
doubly_linked_list.insert_last(70)
doubly_linked_list.insert_after(10, 80)
doubly_linked_list.insert_after(20, 90)
doubly_linked_list.insert_after(70, 100)
doubly_linked_list.delete_last()
Example #23
0
 def contains(self, data):
     try:
         self.lock.acquire()
         return CircularLinkedList.contains(self, data)
     finally:
         self.lock.release()
Example #24
0
 def slice(self, size):
     try:
         self.lock.acquire()
         return CircularLinkedList.slice(self, size)
     finally:
         self.lock.release()
 def test_contains_negative(self):
     linkedlist = CircularLinkedList([1, 2, 3, 4])
     assert 99 not in linkedlist
 def testInitLinkedList(self):
     ll = CircularLinkedList()
     self.assertIsNone(ll.head)
Example #27
0
 def __init__(self):
     CircularLinkedList.__init__(self)
     self.lock = RLock()
Example #28
0
 def remove(self, data):
     try:
         self.lock.acquire()
         return CircularLinkedList.remove(self, data)
     finally:
         self.lock.release()
from circular_linked_list import Node
from circular_linked_list import CircularLinkedList


def is_circular_linked_list(l):
    cur = l

    while cur and cur.next != l:
        cur = cur.next

    if cur:
        return True
    else:
        return False


if __name__ == "__main__":
    linear_list = Node(0)
    cur = linear_list
    for num in [1, 2, 3, 4, 5]:
        cur.next = Node(num)
        cur = cur.next

    circular_list = CircularLinkedList()
    circular_list.list_from_array([1, 2, 3, 4, 5])

    print(is_circular_linked_list(linear_list))
    print(is_circular_linked_list(circular_list.head))
Example #30
0
 def list_size(self):
     try:
         self.lock.acquire()
         return CircularLinkedList.list_size(self)
     finally:
         self.lock.release()