Skip to content

feat(datastructures): add SelfOrganizingLinkedList implementation and tests - #7575

Open
iamcodinghere22 wants to merge 20 commits into
TheAlgorithms:masterfrom
iamcodinghere22:feat/self-organizing-list
Open

feat(datastructures): add SelfOrganizingLinkedList implementation and tests#7575
iamcodinghere22 wants to merge 20 commits into
TheAlgorithms:masterfrom
iamcodinghere22:feat/self-organizing-list

Conversation

@iamcodinghere22

Copy link
Copy Markdown
Contributor

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

  • Added SelfOrganizingLinkedList.java under com.thealgorithms.datastructures.lists.
  • Added comprehensive unit test coverage in SelfOrganizingLinkedListTest.java verifying search, insertion, edge cases (empty list, non-existent elements), and move-to-front behavior.

Testing

  • All unit tests in SelfOrganizingLinkedListTest.java pass locally (mvn test -Dtest=SelfOrganizingLinkedListTest).
  • Verified full build and test suite run successfully with JaCoCo code coverage.

Checklist

  • Code follows project coding guidelines.
  • Unit tests have been added for new functionality.
  • No duplicate code or classes introduced.

@codecov-commenter

codecov-commenter commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.28571% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.61%. Comparing base (56e2699) to head (67df287).

Files with missing lines Patch % Lines
...datastructures/lists/SelfOrganizingLinkedList.java 94.28% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7575      +/-   ##
============================================
+ Coverage     80.59%   80.61%   +0.01%     
- Complexity     7483     7495      +12     
============================================
  Files           815      816       +1     
  Lines         24060    24095      +35     
  Branches       4736     4741       +5     
============================================
+ Hits          19390    19423      +33     
- Misses         3907     3909       +2     
  Partials        763      763              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@iamcodinghere22

Copy link
Copy Markdown
Contributor Author

Hi @DenizAltunkapan @yanglbme @alxkm,
All checks have passed successfully and the branch is ready.
Could one of you please review and approve this PR so it can be merged?
Thanks

@DenizAltunkapan DenizAltunkapan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

*
* @param <E> the type of element held in this node
*/
class LinkedList<E> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling the node class LinkedList is confusing, it's a node, not a list, and it clashes with java.util.LinkedList. The repo already has a convention for this (SinglyLinkedListNode). Could you rename it to Node and make it a static nested class inside SelfOrganizingLinkedList? That also keeps it to one top-level class per file, which is what the other list files do.


/** Returns true if the list contains no elements. */
public boolean isEmpty() {
return head == null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codecov flags two uncovered lines, these getters (getSize / isEmpty) look like the culprits. Worth a small test so the patch is fully covered.

assertTrue(list.search(30));

// '30' should now be the new head
assertEquals(30, list.getHeadValue());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests only check getHeadValue, so they confirm the searched element ends up at the front but never that the rest of the list survives. A broken move-to-front (lost node, cycle, wrong order) would still pass all of these. Could you add a test that walks the whole list after a search and asserts the full order plus unchanged size? A duplicate-value case would be good too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants