Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#979 from shellhub/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
yanglbme authored Oct 7, 2019
2 parents 9c880b5 + c51e2b6 commit ea7c4ec
Showing 1 changed file with 46 additions and 33 deletions.
79 changes: 46 additions & 33 deletions DataStructures/Lists/SinglyLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,35 @@ public class SinglyLinkedList {
*/
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
*
* @param x Element to be added
*/
public void insertHead(int x) {
Node newNode = new Node(x);
newNode.next = head;
head = newNode;
insertNth(x, 0);
}

/**
* insert an element at the tail of list
*
* @param data Element to be added
*/
public void insert(int data) {
insertNth(data, size);
}

/**
Expand All @@ -37,17 +57,16 @@ public void insertHead(int x) {

public void insertNth(int data, 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)
insertHead(data);
else {
throw new IndexOutOfBoundsException("position less than zero or position more than the count of list");
} else {
Node cur = head;
Node node = new Node(data);
for (int i = 1; i < position; ++i) {
for (int i = 0; i < position; ++i) {
cur = cur.next;
}
node.next = cur.next;
cur.next = node;
size++;
}
}

Expand All @@ -57,29 +76,33 @@ public void insertNth(int data, int position) {
* @return The element deleted
*/
public void deleteHead() {
if (isEmpty()) {
throw new RuntimeException("The list is empty!");
}
deleteNth(0);
}

Node destroy = head;
head = head.next;
destroy = null; // clear to let GC do its work
/**
* This method deletes an element at the tail
*/
public void delete() {
deleteNth(size - 1);
}

/**
* 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 {
if (position < 0 || position > size - 1) {
throw new IndexOutOfBoundsException("position less than zero or position more than the count of list");
} else {
Node cur = head;
for (int i = 1; i < position; ++i) {
for (int i = 0; i < position; ++i) {
cur = cur.next;
}

Node destroy = cur.next;
cur.next = cur.next.next;
destroy = null; // clear to let GC do its work

size--;
}
}

Expand All @@ -89,14 +112,14 @@ public void deleteNth(int position) {
* @return true is list is empty
*/
public boolean isEmpty() {
return getSize() == 0;
return size == 0;
}

/**
* Prints contents of the list
*/
public void display() {
Node current = head;
Node current = head.next;
while (current != null) {
System.out.print(current.value + " ");
current = current.next;
Expand All @@ -108,17 +131,7 @@ public void display() {
* 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;
}
return size;
}

/**
Expand Down

0 comments on commit ea7c4ec

Please sign in to comment.