-
Notifications
You must be signed in to change notification settings - Fork 21.2k
feat(datastructures): add SelfOrganizingLinkedList implementation and tests #7575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DenizAltunkapan
merged 27 commits into
TheAlgorithms:master
from
iamcodinghere22:feat/self-organizing-list
Aug 21, 2026
+224
−0
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
0c85933
Add SquareFreeInteger to maths
iamcodinghere22 488fa21
fix clang-format issues
iamcodinghere22 c83cd4f
add newline
iamcodinghere22 2156b67
Add new test file in test
iamcodinghere22 3e5e1ab
modified
iamcodinghere22 d3c1360
delete
iamcodinghere22 28bfe86
Add DisariumNumbers with test
iamcodinghere22 90c0e87
Merge branch 'master' into master
DenizAltunkapan 259db0d
feat(datastructures): add SelfOrganizingLinkedList implementation and…
iamcodinghere22 4809f82
fix checkstyle formatting errors in SelfOrganizingLinkedList includin…
iamcodinghere22 88d9ba2
Add new Line as per the format
iamcodinghere22 6e39000
fix(datastructures): prevent null dereference in SelfOrganizingLinked…
iamcodinghere22 beb01b2
format correction
iamcodinghere22 5b513e6
format
iamcodinghere22 2905459
build format
iamcodinghere22 7d05132
"
iamcodinghere22 c71cec2
format finally
iamcodinghere22 3db080e
maybe
iamcodinghere22 3cab177
done
iamcodinghere22 67df287
Merge branch 'master' into feat/self-organizing-list
iamcodinghere22 3a95d85
make corrections and add tests
iamcodinghere22 bdecac0
format
iamcodinghere22 f27285e
build correction
iamcodinghere22 64911e3
done
iamcodinghere22 1ebe273
pmd done
iamcodinghere22 53aaad2
Merge branch 'master' into feat/self-organizing-list
iamcodinghere22 bf9575d
Merge branch 'master' into feat/self-organizing-list
DenizAltunkapan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
src/main/java/com/thealgorithms/datastructures/lists/SelfOrganizingLinkedList.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package com.thealgorithms.datastructures.lists; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * A Self-Organizing Linked List implementation using the Move-To-Front (MTF) strategy. | ||
| * When an element is searched, it is automatically moved to the head of the list | ||
| * to optimize subsequent lookups. | ||
| * | ||
| * @param <E> the type of elements held in this list | ||
| */ | ||
| public class SelfOrganizingLinkedList<E> { | ||
|
|
||
| /** | ||
| * Node structure for the self-organizing linked list. | ||
| * | ||
| * @param <E> the type of element held in this node | ||
| */ | ||
| private static class Node<E> { | ||
| E value; | ||
| Node<E> next; | ||
|
|
||
| Node(E value) { | ||
| this.value = value; | ||
| this.next = null; | ||
| } | ||
| } | ||
|
|
||
| private Node<E> head; | ||
| private int size; | ||
|
|
||
| public SelfOrganizingLinkedList() { | ||
| this.size = 0; | ||
| this.head = null; | ||
| } | ||
|
|
||
| /** | ||
| * Inserts a new value at the end of the list. | ||
| * | ||
| * @param value the element to add | ||
| */ | ||
| public void insert(E value) { | ||
| Node<E> newNode = new Node<>(value); | ||
| if (head == null) { | ||
| head = newNode; | ||
| } else { | ||
| Node<E> temp = head; | ||
| while (temp.next != null) { | ||
| temp = temp.next; | ||
| } | ||
| temp.next = newNode; | ||
| } | ||
| size++; | ||
| } | ||
|
|
||
| /** | ||
| * Searches for a value in the list. | ||
| * If found, moves the node to the front (head) of the list. | ||
| * | ||
| * @param key the value to search for | ||
| * @return true if the element is present, false otherwise | ||
| */ | ||
| public boolean search(E key) { | ||
| if (head == null) { | ||
| return false; | ||
| } | ||
| // If the key is already at the head, no pointers need to be rewired | ||
| if (Objects.equals(head.value, key)) { | ||
| return true; | ||
| } | ||
|
|
||
| Node<E> prev = head; | ||
| Node<E> curr = head.next; | ||
|
|
||
| while (curr != null && !Objects.equals(curr.value, key)) { | ||
| prev = curr; | ||
| curr = curr.next; | ||
| } | ||
|
|
||
| if (curr == null) { | ||
| return false; | ||
| } | ||
|
|
||
| // Unlink curr from its current position and move it to head | ||
| prev.next = curr.next; | ||
| curr.next = head; | ||
| head = curr; | ||
| return true; | ||
| } | ||
|
|
||
| /** Gets the current head value of the list. */ | ||
| public E getHeadValue() { | ||
| return head != null ? head.value : null; | ||
| } | ||
|
|
||
| /** Returns the size of the list. */ | ||
| public int getSize() { | ||
| return size; | ||
| } | ||
|
|
||
| /** Returns true if the list contains no elements. */ | ||
| public boolean isEmpty() { | ||
| return size == 0; | ||
| } | ||
| } |
119 changes: 119 additions & 0 deletions
119
src/test/java/com/thealgorithms/datastructures/lists/SelfOrganizingLinkedListTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package com.thealgorithms.datastructures.lists; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class SelfOrganizingLinkedListTest { | ||
|
|
||
| private SelfOrganizingLinkedList<Integer> list; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| list = new SelfOrganizingLinkedList<>(); | ||
| } | ||
|
|
||
| @Test | ||
| void testEmptyListAndGetters() { | ||
| assertTrue(list.isEmpty()); | ||
| assertEquals(0, list.getSize()); | ||
| assertNull(list.getHeadValue()); | ||
| assertFalse(list.search(10)); | ||
| } | ||
|
|
||
| @Test | ||
| void testInsertAndSizeState() { | ||
| assertTrue(list.isEmpty()); | ||
| list.insert(10); | ||
| assertFalse(list.isEmpty()); | ||
| assertEquals(1, list.getSize()); | ||
|
|
||
| list.insert(20); | ||
| assertEquals(2, list.getSize()); | ||
| } | ||
|
|
||
| @Test | ||
| void testMoveMiddleElementToFrontPreservesFullListStructure() { | ||
| list.insert(10); | ||
| list.insert(20); | ||
| list.insert(30); | ||
| list.insert(40); | ||
|
|
||
| // Initial order: [10, 20, 30, 40] | ||
| assertTrue(list.search(30)); | ||
|
|
||
| // Expected order: [30, 10, 20, 40] | ||
| assertEquals(4, list.getSize()); | ||
| assertEquals(30, list.getHeadValue()); | ||
|
|
||
| // Sequential head tracking to verify middle and tail pointers didn't break | ||
| assertTrue(list.search(10)); // [10, 30, 20, 40] | ||
| assertEquals(10, list.getHeadValue()); | ||
|
|
||
| assertTrue(list.search(20)); // [20, 10, 30, 40] | ||
| assertEquals(20, list.getHeadValue()); | ||
|
|
||
| assertTrue(list.search(40)); // [40, 20, 10, 30] | ||
| assertEquals(40, list.getHeadValue()); | ||
| assertEquals(4, list.getSize()); | ||
| } | ||
|
|
||
| @Test | ||
| void testMoveLastElementToFrontPreservesFullListStructure() { | ||
| list.insert(10); | ||
| list.insert(20); | ||
| list.insert(30); | ||
|
|
||
| // Search tail element '30' | ||
| assertTrue(list.search(30)); // Order becomes [30, 10, 20] | ||
|
|
||
| assertEquals(30, list.getHeadValue()); | ||
| assertEquals(3, list.getSize()); | ||
|
|
||
| // Verify remaining chain order [10, 20] | ||
| assertTrue(list.search(20)); // [20, 30, 10] | ||
| assertEquals(20, list.getHeadValue()); | ||
|
|
||
| assertTrue(list.search(10)); // [10, 20, 30] | ||
| assertEquals(10, list.getHeadValue()); | ||
| assertEquals(3, list.getSize()); | ||
| } | ||
|
|
||
| @Test | ||
| void testSearchNonExistentElementPreservesStructureAndSize() { | ||
| list.insert(10); | ||
| list.insert(20); | ||
| list.insert(30); | ||
|
|
||
| assertFalse(list.search(99)); | ||
| assertEquals(3, list.getSize()); | ||
| assertEquals(10, list.getHeadValue()); | ||
| } | ||
|
|
||
| @Test | ||
| void testDuplicateValuesMovesFirstMatchedToFront() { | ||
| list.insert(10); | ||
| list.insert(20); | ||
| list.insert(10); // Duplicate '10' at tail | ||
| list.insert(30); | ||
|
|
||
| // Initial list state: [10, 20, 10, 30] | ||
| // Searching '10' hits the head immediately -> no re-linking | ||
| assertTrue(list.search(10)); | ||
| assertEquals(10, list.getHeadValue()); | ||
| assertEquals(4, list.getSize()); | ||
|
|
||
| // Searching '20' moves middle element to head: [20, 10, 10, 30] | ||
| assertTrue(list.search(20)); | ||
| assertEquals(20, list.getHeadValue()); | ||
|
|
||
| // Searching '10' moves the FIRST instance of '10' (index 1) to head: [10, 20, 10, 30] | ||
| assertTrue(list.search(10)); | ||
| assertEquals(10, list.getHeadValue()); | ||
| assertEquals(4, list.getSize()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.