Skip to content

Commit c51e2b6

Browse files
update
1 parent 9c880b5 commit c51e2b6

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

DataStructures/Lists/SinglyLinkedList.java

+46-33
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,35 @@ public class SinglyLinkedList {
1717
*/
1818
private Node head;
1919

20+
/**
21+
* size of SinglyLinkedList
22+
*/
23+
private int size;
24+
25+
/**
26+
* init SinglyLinkedList
27+
*/
28+
public SinglyLinkedList() {
29+
head = new Node(0);
30+
size = 0;
31+
}
32+
2033
/**
2134
* This method inserts an element at the head
2235
*
2336
* @param x Element to be added
2437
*/
2538
public void insertHead(int x) {
26-
Node newNode = new Node(x);
27-
newNode.next = head;
28-
head = newNode;
39+
insertNth(x, 0);
40+
}
41+
42+
/**
43+
* insert an element at the tail of list
44+
*
45+
* @param data Element to be added
46+
*/
47+
public void insert(int data) {
48+
insertNth(data, size);
2949
}
3050

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

3858
public void insertNth(int data, int position) {
3959
if (position < 0 || position > getSize()) {
40-
throw new RuntimeException("position less than zero or position more than the count of list");
41-
} else if (position == 0)
42-
insertHead(data);
43-
else {
60+
throw new IndexOutOfBoundsException("position less than zero or position more than the count of list");
61+
} else {
4462
Node cur = head;
4563
Node node = new Node(data);
46-
for (int i = 1; i < position; ++i) {
64+
for (int i = 0; i < position; ++i) {
4765
cur = cur.next;
4866
}
4967
node.next = cur.next;
5068
cur.next = node;
69+
size++;
5170
}
5271
}
5372

@@ -57,29 +76,33 @@ public void insertNth(int data, int position) {
5776
* @return The element deleted
5877
*/
5978
public void deleteHead() {
60-
if (isEmpty()) {
61-
throw new RuntimeException("The list is empty!");
62-
}
79+
deleteNth(0);
80+
}
6381

64-
Node destroy = head;
65-
head = head.next;
66-
destroy = null; // clear to let GC do its work
82+
/**
83+
* This method deletes an element at the tail
84+
*/
85+
public void delete() {
86+
deleteNth(size - 1);
6787
}
6888

6989
/**
7090
* This method deletes an element at Nth position
7191
*/
7292
public void deleteNth(int position) {
73-
if (position < 0 || position >= getSize()) {
74-
throw new RuntimeException("position less than zero or position more than the count of list");
75-
} else if (position == 0)
76-
deleteHead();
77-
else {
93+
if (position < 0 || position > size - 1) {
94+
throw new IndexOutOfBoundsException("position less than zero or position more than the count of list");
95+
} else {
7896
Node cur = head;
79-
for (int i = 1; i < position; ++i) {
97+
for (int i = 0; i < position; ++i) {
8098
cur = cur.next;
8199
}
100+
101+
Node destroy = cur.next;
82102
cur.next = cur.next.next;
103+
destroy = null; // clear to let GC do its work
104+
105+
size--;
83106
}
84107
}
85108

@@ -89,14 +112,14 @@ public void deleteNth(int position) {
89112
* @return true is list is empty
90113
*/
91114
public boolean isEmpty() {
92-
return getSize() == 0;
115+
return size == 0;
93116
}
94117

95118
/**
96119
* Prints contents of the list
97120
*/
98121
public void display() {
99-
Node current = head;
122+
Node current = head.next;
100123
while (current != null) {
101124
System.out.print(current.value + " ");
102125
current = current.next;
@@ -108,17 +131,7 @@ public void display() {
108131
* Returns the size of the linked list
109132
*/
110133
public int getSize() {
111-
if (head == null)
112-
return 0;
113-
else {
114-
Node current = head;
115-
int size = 1;
116-
while (current.next != null) {
117-
current = current.next;
118-
size++;
119-
}
120-
return size;
121-
}
134+
return size;
122135
}
123136

124137
/**

0 commit comments

Comments
 (0)