Merge pull request #1288 from MohamedBechir/Development
Add singly linked list
This commit is contained in:
commit
a3c111b5f5
102
src/main/java/com/dataStructures/SinglyLinkedList.java
Normal file
102
src/main/java/com/dataStructures/SinglyLinkedList.java
Normal file
@ -0,0 +1,102 @@
|
||||
package com.dataStructures;
|
||||
|
||||
|
||||
|
||||
|
||||
public class SinglyLinkedList<T> {
|
||||
|
||||
/**
|
||||
* Define a node in the singly linked list
|
||||
*/
|
||||
private static class Node<T> {
|
||||
T data;
|
||||
Node<T> next;
|
||||
|
||||
Node(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reference to the first node of the singly linked list
|
||||
*/
|
||||
private Node<T> head;
|
||||
|
||||
/**
|
||||
* Method to return the size of the singly linked list
|
||||
*/
|
||||
public int size() {
|
||||
int size = 0;
|
||||
Node<T> node = head;
|
||||
while (node != null) {
|
||||
++size;
|
||||
node = node.next;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to add a node at the end of the singly linked list
|
||||
*/
|
||||
public void add(T data) {
|
||||
Node<T> newNode = new Node<>(data);
|
||||
if (head == null) {
|
||||
head = newNode;
|
||||
} else {
|
||||
Node<T> last = head;
|
||||
while (last.next != null) {
|
||||
last = last.next;
|
||||
}
|
||||
last.next = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to add a node at the beginning of the singly linked list
|
||||
*/
|
||||
public void insertAtStart (T data) {
|
||||
Node<T> node = new Node<>(data);
|
||||
node.data = data;
|
||||
node.next = null;
|
||||
node.next = head;
|
||||
head = node;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to remove a node at any position in the singly linked list
|
||||
*/
|
||||
public void remove(int position) {
|
||||
Node<T> prev = null, node = head;
|
||||
while (position > 0 && node != null) {
|
||||
--position;
|
||||
prev = node;
|
||||
node = node.next;
|
||||
}
|
||||
if (node != null) {
|
||||
if (prev == null) {
|
||||
head = node.next;
|
||||
} else {
|
||||
prev.next = node.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to print the content of the singly linked list for better
|
||||
* understanding
|
||||
*/
|
||||
public void printContent() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Node<T> node = head;
|
||||
while (node != null) {
|
||||
sb.append(node.data).append('-').append('>');
|
||||
node = node.next;
|
||||
}
|
||||
int lastComma = sb.lastIndexOf(",");
|
||||
if (lastComma != -1) {
|
||||
sb.deleteCharAt(lastComma);
|
||||
}
|
||||
System.out.println(sb);
|
||||
}
|
||||
}
|
60
src/test/java/com/dataStructures/SinglyLinkedListTest.java
Normal file
60
src/test/java/com/dataStructures/SinglyLinkedListTest.java
Normal file
@ -0,0 +1,60 @@
|
||||
package com.dataStructures;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
||||
|
||||
public class SinglyLinkedListTest {
|
||||
|
||||
private SinglyLinkedList<Integer> MySinglyLinkedList = new SinglyLinkedList<>();
|
||||
|
||||
@Test
|
||||
public void emptyTest() {
|
||||
Assertions.assertEquals(0, MySinglyLinkedList.size());
|
||||
MySinglyLinkedList.printContent();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addTest() {
|
||||
MySinglyLinkedList.add(1);
|
||||
Assertions.assertEquals(1, MySinglyLinkedList.size());
|
||||
MySinglyLinkedList.printContent();
|
||||
|
||||
MySinglyLinkedList.add(42);
|
||||
Assertions.assertEquals(2, MySinglyLinkedList.size());
|
||||
MySinglyLinkedList.printContent();
|
||||
|
||||
MySinglyLinkedList.add(2);
|
||||
MySinglyLinkedList.add(3);
|
||||
Assertions.assertEquals(4, MySinglyLinkedList.size());
|
||||
MySinglyLinkedList.printContent();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeTest() {
|
||||
MySinglyLinkedList.add(1);
|
||||
MySinglyLinkedList.add(2);
|
||||
MySinglyLinkedList.add(3);
|
||||
MySinglyLinkedList.add(4);
|
||||
MySinglyLinkedList.printContent();
|
||||
|
||||
MySinglyLinkedList.remove(3);
|
||||
Assertions.assertEquals(3, MySinglyLinkedList.size());
|
||||
MySinglyLinkedList.printContent();
|
||||
|
||||
MySinglyLinkedList.remove(0);
|
||||
Assertions.assertEquals(2, MySinglyLinkedList.size());
|
||||
MySinglyLinkedList.printContent();
|
||||
|
||||
MySinglyLinkedList.remove(0);
|
||||
Assertions.assertEquals(1, MySinglyLinkedList.size());
|
||||
MySinglyLinkedList.printContent();
|
||||
|
||||
MySinglyLinkedList.remove(0);
|
||||
Assertions.assertEquals(0, MySinglyLinkedList.size());
|
||||
MySinglyLinkedList.printContent();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user