Skip to content

Commit

Permalink
Update Doubly linked list.py
Browse files Browse the repository at this point in the history
Added linked list detect cycle algorithm, and also updated the program instructions and options to include this new option.
  • Loading branch information
jdjones429 authored Nov 6, 2024
1 parent 47fc66f commit c7e6a95
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion code/data_structures/src/DoubleLinkedList/Doubly linked list.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,18 @@ def Delete(self, data):
current.prev = prev.next
def Empty(self):
return self.ptr == None
def HasCycle(self):
slow = self.ptr
fast = self.ptr
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
dll = DLL()
while True:
choice = int(input("\n1.Insert\n2.Delete\n3.Check Empty\n4.Exit\nEnter Choice : "))
choice = int(input("\n1.Insert\n2.Delete\n3.Check Empty\n4.Detect Cycle\n5.Exit\nEnter Choice : "))
if choice == 1:
value = int(input("Enter element to Insert : "))
dll.Insert(value)
Expand All @@ -71,5 +80,10 @@ def Empty(self):
elif choice == 3:
print(dll.Empty())
elif choice == 4:
if dll.HasCycle():
print("Cycle detected in the linked list")
else:
print("No cycle detected in the linked list")
elif choice == 5:
break
print("Program End")

0 comments on commit c7e6a95

Please sign in to comment.