|
| 1 | +package com.thealgorithms.datastructures.lists; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * A SortedLinkedList is a data structure that maintains a sorted list of elements. |
| 8 | + * Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation. |
| 9 | + * This implementation uses a singly linked list to store the elements. |
| 10 | + * Further details can be found on this link |
| 11 | + * https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html |
| 12 | + */ |
| 13 | +public class SortedLinkedList { |
| 14 | + private Node head; |
| 15 | + private Node tail; |
| 16 | + |
| 17 | + public SortedLinkedList() { |
| 18 | + this.head = null; |
| 19 | + this.tail = null; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Inserts a new element into the sorted linked list. |
| 24 | + * @param value the value to be inserted |
| 25 | + */ |
| 26 | + public void insert(int value) { |
| 27 | + Node newNode = new Node(value); |
| 28 | + if (head == null) { |
| 29 | + this.head = newNode; |
| 30 | + this.tail = newNode; |
| 31 | + } else if (value < head.value) { |
| 32 | + newNode.next = this.head; |
| 33 | + this.head = newNode; |
| 34 | + } else if (value > tail.value) { |
| 35 | + this.tail.next = newNode; |
| 36 | + this.tail = newNode; |
| 37 | + } else { |
| 38 | + Node temp = head; |
| 39 | + while (temp.next != null && temp.next.value < value) { |
| 40 | + temp = temp.next; |
| 41 | + } |
| 42 | + newNode.next = temp.next; |
| 43 | + temp.next = newNode; |
| 44 | + if (newNode.next == null) { |
| 45 | + this.tail = newNode; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Displays the elements of the sorted linked list. |
| 52 | + */ |
| 53 | + public void display() { |
| 54 | + System.out.println(this.toString()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Deletes the first occurrence of the specified element in the sorted linked list. |
| 59 | + * @param value the value to be deleted |
| 60 | + * @return true if the element is found and deleted, false otherwise |
| 61 | + */ |
| 62 | + public boolean delete(int value) { |
| 63 | + if (this.head == null) { |
| 64 | + return false; |
| 65 | + } else if (this.head.value == value) { |
| 66 | + if (this.head.next == null) { |
| 67 | + this.head = null; |
| 68 | + this.tail = null; |
| 69 | + } else { |
| 70 | + this.head = this.head.next; |
| 71 | + } |
| 72 | + return true; |
| 73 | + } else { |
| 74 | + Node temp = this.head; |
| 75 | + while (temp.next != null) { |
| 76 | + if (temp.next.value == value) { |
| 77 | + if (temp.next == this.tail) { |
| 78 | + this.tail = temp; |
| 79 | + } |
| 80 | + temp.next = temp.next.next; |
| 81 | + return true; |
| 82 | + } |
| 83 | + temp = temp.next; |
| 84 | + } |
| 85 | + return false; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Searches for the specified element in the sorted linked list. |
| 91 | + * @param value the value to be searched |
| 92 | + * @return true if the element is found, false otherwise |
| 93 | + */ |
| 94 | + public boolean search(int value) { |
| 95 | + Node temp = this.head; |
| 96 | + while (temp != null) { |
| 97 | + if (temp.value == value) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + temp = temp.next; |
| 101 | + } |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Checks if the sorted linked list is empty. |
| 107 | + * @return true if the list is empty, false otherwise |
| 108 | + */ |
| 109 | + public boolean isEmpty() { |
| 110 | + return head == null; |
| 111 | + } |
| 112 | + /** |
| 113 | + * Returns a string representation of the sorted linked list. |
| 114 | + * @return a string representation of the sorted linked list |
| 115 | + */ |
| 116 | + @Override |
| 117 | + public String toString() { |
| 118 | + if (this.head != null) { |
| 119 | + List<String> elements = new ArrayList<>(); |
| 120 | + Node temp = this.head; |
| 121 | + while (temp != null) { |
| 122 | + elements.add(String.valueOf(temp.value)); |
| 123 | + temp = temp.next; |
| 124 | + } |
| 125 | + return "[" + String.join(", ", elements) + "]"; |
| 126 | + |
| 127 | + } else { |
| 128 | + return "[]"; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public final class Node { |
| 133 | + public final int value; |
| 134 | + public Node next; |
| 135 | + |
| 136 | + public Node(int value) { |
| 137 | + this.value = value; |
| 138 | + this.next = null; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments