mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 12:11:28 +08:00
@ -17,15 +17,35 @@ public class SinglyLinkedList {
|
|||||||
*/
|
*/
|
||||||
private Node head;
|
private Node head;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* size of SinglyLinkedList
|
||||||
|
*/
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* init SinglyLinkedList
|
||||||
|
*/
|
||||||
|
public SinglyLinkedList() {
|
||||||
|
head = new Node(0);
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method inserts an element at the head
|
* This method inserts an element at the head
|
||||||
*
|
*
|
||||||
* @param x Element to be added
|
* @param x Element to be added
|
||||||
*/
|
*/
|
||||||
public void insertHead(int x) {
|
public void insertHead(int x) {
|
||||||
Node newNode = new Node(x);
|
insertNth(x, 0);
|
||||||
newNode.next = head;
|
}
|
||||||
head = newNode;
|
|
||||||
|
/**
|
||||||
|
* insert an element at the tail of list
|
||||||
|
*
|
||||||
|
* @param data Element to be added
|
||||||
|
*/
|
||||||
|
public void insert(int data) {
|
||||||
|
insertNth(data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,17 +57,16 @@ public class SinglyLinkedList {
|
|||||||
|
|
||||||
public void insertNth(int data, int position) {
|
public void insertNth(int data, int position) {
|
||||||
if (position < 0 || position > getSize()) {
|
if (position < 0 || position > getSize()) {
|
||||||
throw new RuntimeException("position less than zero or position more than the count of list");
|
throw new IndexOutOfBoundsException("position less than zero or position more than the count of list");
|
||||||
} else if (position == 0)
|
} else {
|
||||||
insertHead(data);
|
|
||||||
else {
|
|
||||||
Node cur = head;
|
Node cur = head;
|
||||||
Node node = new Node(data);
|
Node node = new Node(data);
|
||||||
for (int i = 1; i < position; ++i) {
|
for (int i = 0; i < position; ++i) {
|
||||||
cur = cur.next;
|
cur = cur.next;
|
||||||
}
|
}
|
||||||
node.next = cur.next;
|
node.next = cur.next;
|
||||||
cur.next = node;
|
cur.next = node;
|
||||||
|
size++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,29 +76,33 @@ public class SinglyLinkedList {
|
|||||||
* @return The element deleted
|
* @return The element deleted
|
||||||
*/
|
*/
|
||||||
public void deleteHead() {
|
public void deleteHead() {
|
||||||
if (isEmpty()) {
|
deleteNth(0);
|
||||||
throw new RuntimeException("The list is empty!");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Node destroy = head;
|
/**
|
||||||
head = head.next;
|
* This method deletes an element at the tail
|
||||||
destroy = null; // clear to let GC do its work
|
*/
|
||||||
|
public void delete() {
|
||||||
|
deleteNth(size - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method deletes an element at Nth position
|
* This method deletes an element at Nth position
|
||||||
*/
|
*/
|
||||||
public void deleteNth(int position) {
|
public void deleteNth(int position) {
|
||||||
if (position < 0 || position >= getSize()) {
|
if (position < 0 || position > size - 1) {
|
||||||
throw new RuntimeException("position less than zero or position more than the count of list");
|
throw new IndexOutOfBoundsException("position less than zero or position more than the count of list");
|
||||||
} else if (position == 0)
|
} else {
|
||||||
deleteHead();
|
|
||||||
else {
|
|
||||||
Node cur = head;
|
Node cur = head;
|
||||||
for (int i = 1; i < position; ++i) {
|
for (int i = 0; i < position; ++i) {
|
||||||
cur = cur.next;
|
cur = cur.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Node destroy = cur.next;
|
||||||
cur.next = cur.next.next;
|
cur.next = cur.next.next;
|
||||||
|
destroy = null; // clear to let GC do its work
|
||||||
|
|
||||||
|
size--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,14 +112,14 @@ public class SinglyLinkedList {
|
|||||||
* @return true is list is empty
|
* @return true is list is empty
|
||||||
*/
|
*/
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return getSize() == 0;
|
return size == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints contents of the list
|
* Prints contents of the list
|
||||||
*/
|
*/
|
||||||
public void display() {
|
public void display() {
|
||||||
Node current = head;
|
Node current = head.next;
|
||||||
while (current != null) {
|
while (current != null) {
|
||||||
System.out.print(current.value + " ");
|
System.out.print(current.value + " ");
|
||||||
current = current.next;
|
current = current.next;
|
||||||
@ -108,17 +131,7 @@ public class SinglyLinkedList {
|
|||||||
* Returns the size of the linked list
|
* Returns the size of the linked list
|
||||||
*/
|
*/
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
if (head == null)
|
return size;
|
||||||
return 0;
|
|
||||||
else {
|
|
||||||
Node current = head;
|
|
||||||
int size = 1;
|
|
||||||
while (current.next != null) {
|
|
||||||
current = current.next;
|
|
||||||
size++;
|
|
||||||
}
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user