|
| 1 | +package com.thealgorithms.datastructures.lists; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +/** |
| 6 | + * A SortedLinkedList is a data structure that maintains a sorted list of elements. |
| 7 | + * Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation. |
| 8 | + * |
| 9 | + * @author Muhammad Junaid Khalid |
| 10 | + * @param int the type of elements in this list |
| 11 | + */ |
| 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 | + * |
| 25 | + * @param value the value to be inserted |
| 26 | + */ |
| 27 | + public void insert(int value){ |
| 28 | + Node newNode = new Node(value); |
| 29 | + if (head == null) { |
| 30 | + this.head = newNode; |
| 31 | + this.tail = newNode; |
| 32 | + } |
| 33 | + else if (value < head.value) { |
| 34 | + newNode.next = this.head; |
| 35 | + this.head = newNode; |
| 36 | + } |
| 37 | + else if (value > tail.value) { |
| 38 | + this.tail.next = newNode; |
| 39 | + } |
| 40 | + else{ |
| 41 | + Node temp=head; |
| 42 | + while (temp.next != null && temp.next.value < value) { |
| 43 | + temp = temp.next; |
| 44 | + } |
| 45 | + newNode.next = temp.next; |
| 46 | + temp.next = newNode; |
| 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 | + * |
| 60 | + * @param value the value to be deleted |
| 61 | + * @return true if the element is found and deleted, false otherwise |
| 62 | + */ |
| 63 | + public boolean delete(int value){ |
| 64 | + if (this.head == null) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + else if (this.head.value == value) { |
| 68 | + this.head = this.head.next; |
| 69 | + return true; |
| 70 | + } |
| 71 | + else{ |
| 72 | + Node temp = this.head; |
| 73 | + while (temp.next != null) { |
| 74 | + if (temp.next.value == value) { |
| 75 | + temp.next = temp.next.next; |
| 76 | + return true; |
| 77 | + } |
| 78 | + temp = temp.next; |
| 79 | + } |
| 80 | + return false; |
| 81 | + |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Searches for the specified element in the sorted linked list. |
| 87 | + * |
| 88 | + * @param value the value to be searched |
| 89 | + * @return true if the element is found, false otherwise |
| 90 | + */ |
| 91 | + public boolean search(int value){ |
| 92 | + Node temp = this.head; |
| 93 | + while (temp != null) { |
| 94 | + if (temp.value == value) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + temp = temp.next; |
| 98 | + } |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Checks if the sorted linked list is empty. |
| 104 | + * |
| 105 | + * @return true if the list is empty, false otherwise |
| 106 | + */ |
| 107 | + public boolean isEmpty() { |
| 108 | + return head == null; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the minimum value in the sorted linked list. |
| 113 | + * |
| 114 | + * @return the minimum value |
| 115 | + */ |
| 116 | + public int minValue(){ |
| 117 | + return this.head.value; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns the maximum value in the sorted linked list. |
| 122 | + * |
| 123 | + * @return the maximum value |
| 124 | + */ |
| 125 | + public int maxValue(){ |
| 126 | + return this.tail.value; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Returns a string representation of the sorted linked list. |
| 131 | + * |
| 132 | + * @return a string representation of the sorted linked list |
| 133 | + */ |
| 134 | + @Override |
| 135 | + public String toString() { |
| 136 | + ArrayList<String> elements=new ArrayList<>(); |
| 137 | + Node temp = this.head; |
| 138 | + while (temp != null) { |
| 139 | + elements.add(String.valueOf(temp.value)); |
| 140 | + temp = temp.next; |
| 141 | + } |
| 142 | + return String.join(", ", elements); |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + public class Node { |
| 147 | + public int value; |
| 148 | + public Node next; |
| 149 | + |
| 150 | + public Node(){ |
| 151 | + this.value = 0; |
| 152 | + this.next= null; |
| 153 | + } |
| 154 | + |
| 155 | + public Node(int value){ |
| 156 | + this.value = value; |
| 157 | + this.next = null; |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | +} |
0 commit comments