From c51e2b68e7e06f1434f380447588d0250e767a1b Mon Sep 17 00:00:00 2001 From: shellhub Date: Sun, 6 Oct 2019 20:39:09 +0800 Subject: [PATCH] update --- DataStructures/Lists/SinglyLinkedList.java | 79 +++++++++++++--------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/DataStructures/Lists/SinglyLinkedList.java b/DataStructures/Lists/SinglyLinkedList.java index b4dae16f..8747aebb 100644 --- a/DataStructures/Lists/SinglyLinkedList.java +++ b/DataStructures/Lists/SinglyLinkedList.java @@ -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); } /** @@ -37,17 +57,16 @@ public class SinglyLinkedList { 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++; } } @@ -57,29 +76,33 @@ public class SinglyLinkedList { * @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--; } } @@ -89,14 +112,14 @@ public class SinglyLinkedList { * @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; @@ -108,17 +131,7 @@ public class SinglyLinkedList { * 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; } /**