Skip to content

Commit c779b9b

Browse files
committed
feat: add cycle detect and get mid
1 parent 796f076 commit c779b9b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

header/forward_list.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,27 @@ class forward_lists{
369369
Allocator get_allocator()const noexcept{
370370
return alloc;
371371
}
372+
bool check_cycle()const noexcept{
373+
Node* slow = head->next;
374+
Node* fast = head->next;
375+
while(slow && fast && fast->next != nullptr){
376+
slow = slow->next;
377+
fast = fast->next->next;
378+
if(fast == slow){
379+
return true;
380+
}
381+
}
382+
return false;
383+
}
384+
T get_middle()const noexcept{
385+
Node* slow = head->next;
386+
Node* fast = head->next;
387+
while(slow && fast && fast->next){
388+
slow = slow->next;
389+
fast = fast->next->next;
390+
}
391+
return slow->data;
392+
}
372393
public:
373394
/**
374395
* @brief method untuk insertion val pada pos front

0 commit comments

Comments
 (0)