Skip to content

Commit

Permalink
Merge pull request #1 from jdjones429/jdjones429-patch-1
Browse files Browse the repository at this point in the history
Update Doubly linked list.py
  • Loading branch information
jdjones429 authored Nov 6, 2024
2 parents 47fc66f + c7e6a95 commit 8e457df
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 8e457df

Please sign in to comment.