Skip to content

feat: Adding uniform number counting algorithm #6052

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
merged 62 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
9b5ced4
Sorted Linked List added with Javadoc and tests
mjk22071998 Oct 2, 2024
1863e6b
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 2, 2024
e1382c1
"Added comments to SortedLinkedList.java to describe the implementati…
mjk22071998 Oct 2, 2024
3ec7627
Upgraded test from junit 4 to junit 5
mjk22071998 Oct 2, 2024
6ff74f0
Added space before braces of functions
mjk22071998 Oct 2, 2024
dd91a15
Rename SortedlinkedListTest.java to SortedLinkedListTest.java
mjk22071998 Oct 2, 2024
2c33f89
made to string null safe
mjk22071998 Oct 2, 2024
58390c1
Updated tail
mjk22071998 Oct 2, 2024
028727f
"Added assignment of `this.tail` to `newNode` in `SortedLinkedList` c…
mjk22071998 Oct 2, 2024
3dce7f0
Remove assertions for minValue and maxValue in empty list test
mjk22071998 Oct 2, 2024
9e0cdf3
tried to get link updated
mjk22071998 Oct 2, 2024
1c2bb10
"Fixed whitespace and formatting issues in SortedLinkedList.java and …
mjk22071998 Oct 2, 2024
25be88e
formatting of test file corrected
mjk22071998 Oct 2, 2024
0a5495d
Removed few whitespaces
mjk22071998 Oct 2, 2024
5693923
Merge branch 'master' into master
mjk22071998 Oct 3, 2024
16cdb60
Addressed comments by alxkm
mjk22071998 Oct 3, 2024
90c9782
"Updated toString method to include brackets and removed default Node…
mjk22071998 Oct 3, 2024
ebc8d33
tests updated
mjk22071998 Oct 3, 2024
47a725a
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 3, 2024
073be5a
Digit Separation algorithm added for positive numbers
mjk22071998 Oct 3, 2024
cb824f4
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 3, 2024
f6452c2
Renamed files
mjk22071998 Oct 3, 2024
15faa54
linter errors resolved
mjk22071998 Oct 3, 2024
278b3d0
linter styles corrected
mjk22071998 Oct 3, 2024
44b985e
Support for negative numbers added
mjk22071998 Oct 3, 2024
4f6de8a
Tests corrected
mjk22071998 Oct 3, 2024
b2bf61f
Merge branch 'master' into master
mjk22071998 Oct 3, 2024
bd7aec1
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 3, 2024
b233407
Merge branch 'master' into master
siriak Oct 4, 2024
18a5eff
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 5, 2024
1ff7d73
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 6, 2024
c53d8d8
feat: Added binary addition algorithm
mjk22071998 Oct 6, 2024
5ace3d0
Merge branch 'master' of https://github.com/mjk22071998/Java
mjk22071998 Oct 6, 2024
e9d9d9c
"Updated JavaDoc comment to match class name in BinaryAddition.java"
mjk22071998 Oct 6, 2024
eeb8daf
Update directory
mjk22071998 Oct 6, 2024
77d0b95
Updated test file
mjk22071998 Oct 6, 2024
c65880e
Merge branch 'master' of https://github.com/mjk22071998/Java
mjk22071998 Oct 6, 2024
516e464
clang linted
mjk22071998 Oct 6, 2024
e623c9b
tests clang linted
mjk22071998 Oct 6, 2024
774b527
Merge branch 'master' into master
siriak Oct 7, 2024
6354f22
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 7, 2024
36fa0d6
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 7, 2024
8cadc67
Update directory
mjk22071998 Oct 7, 2024
d97a750
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 9, 2024
98a4473
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 12, 2024
dc0f031
Update directory
mjk22071998 Oct 12, 2024
84d5819
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 22, 2024
980e6e0
Update directory
mjk22071998 Oct 22, 2024
0b63212
Added Uniform Numbers Algorithms
mjk22071998 Oct 28, 2024
84ed591
Update directory
mjk22071998 Oct 28, 2024
076d995
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 28, 2024
ac359ec
Update directory
mjk22071998 Oct 28, 2024
edf1abb
clang formatted
mjk22071998 Oct 28, 2024
a7262fb
tests corrected
mjk22071998 Oct 28, 2024
91308b4
build erros corrected
mjk22071998 Oct 28, 2024
8b9ba95
removed trailing whitespaces
mjk22071998 Oct 28, 2024
a486ac0
Omitted negative numbers
mjk22071998 Oct 28, 2024
2b4b54c
Styles corrected and added private constructor
mjk22071998 Oct 28, 2024
e311003
made class final
mjk22071998 Oct 28, 2024
fc44a87
removed unnecessary paranthesis
mjk22071998 Oct 28, 2024
95e927c
documentation added
mjk22071998 Oct 28, 2024
cd5fba2
Merge branch 'master' into master
siriak Oct 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Sorted Linked List added with Javadoc and tests
  • Loading branch information
mjk22071998 committed Oct 2, 2024
commit 9b5ced457c6e1e78c5d958a7b0d7ccc678f773c5
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package com.thealgorithms.datastructures.lists;

import java.util.ArrayList;

/**
* A SortedLinkedList is a data structure that maintains a sorted list of elements.
* Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation.
*
* @author Muhammad Junaid Khalid
* @param int the type of elements in this list
*/

public class SortedLinkedList {
private Node head;
private Node tail;

public SortedLinkedList(){
this.head=null;
this.tail=null;
}

/**
* Inserts a new element into the sorted linked list.
*
* @param value the value to be inserted
*/
public void insert(int value){
Node newNode = new Node(value);
if (head == null) {
this.head = newNode;
this.tail = newNode;
}
else if (value < head.value) {
newNode.next = this.head;
this.head = newNode;
}
else if (value > tail.value) {
this.tail.next = newNode;
}
else{
Node temp=head;
while (temp.next != null && temp.next.value < value) {
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
}
}

/**
* Displays the elements of the sorted linked list.
*/
public void display(){
System.out.println(this.toString());
}

/**
* Deletes the first occurrence of the specified element in the sorted linked list.
*
* @param value the value to be deleted
* @return true if the element is found and deleted, false otherwise
*/
public boolean delete(int value){
if (this.head == null) {
return false;
}
else if (this.head.value == value) {
this.head = this.head.next;
return true;
}
else{
Node temp = this.head;
while (temp.next != null) {
if (temp.next.value == value) {
temp.next = temp.next.next;
return true;
}
temp = temp.next;
}
return false;

}
}

/**
* Searches for the specified element in the sorted linked list.
*
* @param value the value to be searched
* @return true if the element is found, false otherwise
*/
public boolean search(int value){
Node temp = this.head;
while (temp != null) {
if (temp.value == value) {
return true;
}
temp = temp.next;
}
return false;
}

/**
* Checks if the sorted linked list is empty.
*
* @return true if the list is empty, false otherwise
*/
public boolean isEmpty() {
return head == null;
}

/**
* Returns the minimum value in the sorted linked list.
*
* @return the minimum value
*/
public int minValue(){
return this.head.value;
}

/**
* Returns the maximum value in the sorted linked list.
*
* @return the maximum value
*/
public int maxValue(){
return this.tail.value;
}

/**
* Returns a string representation of the sorted linked list.
*
* @return a string representation of the sorted linked list
*/
@Override
public String toString() {
ArrayList<String> elements=new ArrayList<>();
Node temp = this.head;
while (temp != null) {
elements.add(String.valueOf(temp.value));
temp = temp.next;
}
return String.join(", ", elements);
}


public class Node {
public int value;
public Node next;

public Node(){
this.value = 0;
this.next= null;
}

public Node(int value){
this.value = value;
this.next = null;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.thealgorithms.datastructures.lists;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class SortedLinkedListTest {

@Test
public void testInsert() {
SortedLinkedList list = new SortedLinkedList();
list.insert(5);
list.insert(3);
list.insert(7);
assertEquals("3, 5, 7", list.toString());
}

@Test
public void testDelete() {
SortedLinkedList list = new SortedLinkedList();
list.insert(5);
list.insert(3);
list.insert(7);
assertTrue(list.delete(5));
assertEquals("3, 7", list.toString());
assertFalse(list.delete(10));
}

@Test
public void testSearch() {
SortedLinkedList list = new SortedLinkedList();
list.insert(5);
list.insert(3);
list.insert(7);
assertTrue(list.search(5));
assertFalse(list.search(10));
}

@Test
public void testMinValue() {
SortedLinkedList list = new SortedLinkedList();
list.insert(5);
list.insert(3);
list.insert(7);
assertEquals(3, list.minValue());
}

@Test
public void testMaxValue() {
SortedLinkedList list = new SortedLinkedList();
list.insert(5);
list.insert(3);
list.insert(7);
assertEquals(7, list.maxValue());
}

@Test
public void testEmptyList() {
SortedLinkedList list = new SortedLinkedList();
assertEquals("", list.toString());
assertFalse(list.delete(5));
assertFalse(list.search(5));
assertEquals(0, list.minValue());
assertEquals(0, list.maxValue());
}
@Test
public void testIsEmpty_onEmptyList() {
SortedLinkedList list = new SortedLinkedList();
assertTrue(list.isEmpty());
}

@Test
public void testIsEmpty_onNonEmptyList() {
SortedLinkedList list = new SortedLinkedList();
list.insert(10);
assertFalse(list.isEmpty());
}

@Test
public void testIsEmpty_afterDeletion() {
SortedLinkedList list = new SortedLinkedList();
list.insert(10);
list.delete(10);
assertTrue(list.isEmpty());
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy