feat(datastructures): add SelfOrganizingLinkedList implementation and tests - #7575
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7575 +/- ##
============================================
+ Coverage 80.59% 80.62% +0.02%
- Complexity 7482 7496 +14
============================================
Files 815 816 +1
Lines 24062 24097 +35
Branches 4736 4741 +5
============================================
+ Hits 19393 19428 +35
Misses 3906 3906
Partials 763 763 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Hi @DenizAltunkapan @yanglbme @alxkm, |
DenizAltunkapan
left a comment
There was a problem hiding this comment.
Nice addition, a self-organizing list with move-to-front is a good fit here and there's no existing one. The algorithm itself looks correct. A few things to sort out before this can go in, left inline.
|
@DenizAltunkapan Node Renaming: Renamed LinkedList to Node and encapsulated it as a private static class Node nested within SelfOrganizingLinkedList. Eliminates collision with java.util.LinkedList and keeps the file to a single top-level class per repository convention. Getters (getSize & isEmpty): Added assertions for getSize() and isEmpty() across initial, single-element, and multi-element states to achieve full coverage on Codecov. Full List Integrity & Duplicate Handling: Added test cases that walk the entire list order after Move-To-Front search operations to ensure no nodes are dropped and no cycles are created. Added test coverage for list behavior when duplicate values exist. |
DenizAltunkapan
left a comment
There was a problem hiding this comment.
@iamcodinghere22 thanks!
This PR implements the Self-Organizing Linked List data structure using the "Move To Front(MTF)" heuristic.
In this implementation, whenever an element is accessed/searched, it is dynamically moved to the head of the list. This optimizes access time for frequently requested items by keeping them near the front, achieving an {O(1)} best-case lookup time for repeated accesses.
Changes Included
SelfOrganizingLinkedList.javaundercom.thealgorithms.datastructures.lists.SelfOrganizingLinkedListTest.javaverifying search, insertion, edge cases (empty list, non-existent elements), and move-to-front behavior.Testing
SelfOrganizingLinkedListTest.javapass locally (mvn test -Dtest=SelfOrganizingLinkedListTest).Checklist