mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
Major Updates in SinglyLinkedList.java
- The "count" variable made the code more prone to errors, so I removed and replaced it with another "getSize()" function. - The value of "next" was initialized to null. - Major improvements of code were given to "insertNth()" and "deleteHead()" functions for efficiency. - A new function "deleteNth()" was defined, increasing the usefulness of the program.
This commit is contained in:
@ -17,11 +17,6 @@ class SinglyLinkedList {
|
|||||||
*/
|
*/
|
||||||
private Node head;
|
private Node head;
|
||||||
|
|
||||||
/**
|
|
||||||
* Count of nodes
|
|
||||||
*/
|
|
||||||
private int count;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method inserts an element at the head
|
* This method inserts an element at the head
|
||||||
*
|
*
|
||||||
@ -31,7 +26,6 @@ class SinglyLinkedList {
|
|||||||
Node newNode = new Node(x);
|
Node newNode = new Node(x);
|
||||||
newNode.next = head;
|
newNode.next = head;
|
||||||
head = newNode;
|
head = newNode;
|
||||||
++count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,19 +36,20 @@ class SinglyLinkedList {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public void insertNth(int data, int position) {
|
public void insertNth(int data, int position) {
|
||||||
if (position < 0 || position > count) {
|
if (position < 0 || position > getSize()) {
|
||||||
throw new RuntimeException("position less than zero or position more than the count of list");
|
throw new RuntimeException("position less than zero or position more than the count of list");
|
||||||
}
|
}
|
||||||
Node node = new Node(data);
|
else if (position == 0)
|
||||||
Node dummy = new Node(-1);
|
insertHead(data);
|
||||||
dummy.next = head;
|
else {
|
||||||
Node cur = dummy;
|
Node cur = head;
|
||||||
for (int i = 0; i < position; ++i) {
|
Node node = new Node(data);
|
||||||
cur = cur.next;
|
for (int i = 1; i < position; ++i) {
|
||||||
|
cur = cur.next;
|
||||||
|
}
|
||||||
|
node.next = cur.next;
|
||||||
|
cur.next = node;
|
||||||
}
|
}
|
||||||
node.next = cur.next;
|
|
||||||
cur.next = node;
|
|
||||||
++count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,15 +57,30 @@ class SinglyLinkedList {
|
|||||||
*
|
*
|
||||||
* @return The element deleted
|
* @return The element deleted
|
||||||
*/
|
*/
|
||||||
public Node deleteHead() {
|
public void deleteHead() {
|
||||||
if (isEmpty()) {
|
if (isEmpty()) {
|
||||||
throw new RuntimeException("The list is empty!");
|
throw new RuntimeException("The list is empty!");
|
||||||
}
|
}
|
||||||
|
|
||||||
Node temp = head;
|
|
||||||
head = head.next;
|
head = head.next;
|
||||||
--count;
|
}
|
||||||
return temp;
|
|
||||||
|
/**
|
||||||
|
* This method deletes an element at Nth position
|
||||||
|
*/
|
||||||
|
public void deleteNth(int position) {
|
||||||
|
if (position < 0 || position > getSize()) {
|
||||||
|
throw new RuntimeException("position less than zero or position more than the count of list");
|
||||||
|
}
|
||||||
|
else if (position == 0)
|
||||||
|
deleteHead();
|
||||||
|
else {
|
||||||
|
Node cur = head;
|
||||||
|
for (int i = 1; i < position; ++i) {
|
||||||
|
cur = cur.next;
|
||||||
|
}
|
||||||
|
cur.next = cur.next.next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -79,7 +89,7 @@ class SinglyLinkedList {
|
|||||||
* @return true is list is empty
|
* @return true is list is empty
|
||||||
*/
|
*/
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return count == 0;
|
return getSize() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,6 +104,23 @@ class SinglyLinkedList {
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the size of the linked list
|
||||||
|
*/
|
||||||
|
public int getSize() {
|
||||||
|
if (head == null)
|
||||||
|
return 0;
|
||||||
|
else {
|
||||||
|
Node current = head;
|
||||||
|
int size = 1;
|
||||||
|
while (current.next != null) {
|
||||||
|
current = current.next;
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main method
|
* Main method
|
||||||
*
|
*
|
||||||
@ -117,6 +144,11 @@ class SinglyLinkedList {
|
|||||||
myList.insertNth(11, 2);
|
myList.insertNth(11, 2);
|
||||||
|
|
||||||
myList.display(); // 7 -> 5 -> 11
|
myList.display(); // 7 -> 5 -> 11
|
||||||
|
|
||||||
|
myList.deleteNth(1);
|
||||||
|
|
||||||
|
myList.display(); // 7-> 11
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,5 +177,6 @@ class Node {
|
|||||||
*/
|
*/
|
||||||
Node(int value) {
|
Node(int value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
this.next = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user