@@ -17,15 +17,35 @@ public class SinglyLinkedList {
17
17
*/
18
18
private Node head ;
19
19
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
+
20
33
/**
21
34
* This method inserts an element at the head
22
35
*
23
36
* @param x Element to be added
24
37
*/
25
38
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 );
29
49
}
30
50
31
51
/**
@@ -37,17 +57,16 @@ public void insertHead(int x) {
37
57
38
58
public void insertNth (int data , int position ) {
39
59
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 {
44
62
Node cur = head ;
45
63
Node node = new Node (data );
46
- for (int i = 1 ; i < position ; ++i ) {
64
+ for (int i = 0 ; i < position ; ++i ) {
47
65
cur = cur .next ;
48
66
}
49
67
node .next = cur .next ;
50
68
cur .next = node ;
69
+ size ++;
51
70
}
52
71
}
53
72
@@ -57,29 +76,33 @@ public void insertNth(int data, int position) {
57
76
* @return The element deleted
58
77
*/
59
78
public void deleteHead () {
60
- if (isEmpty ()) {
61
- throw new RuntimeException ("The list is empty!" );
62
- }
79
+ deleteNth (0 );
80
+ }
63
81
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 );
67
87
}
68
88
69
89
/**
70
90
* This method deletes an element at Nth position
71
91
*/
72
92
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 {
78
96
Node cur = head ;
79
- for (int i = 1 ; i < position ; ++i ) {
97
+ for (int i = 0 ; i < position ; ++i ) {
80
98
cur = cur .next ;
81
99
}
100
+
101
+ Node destroy = cur .next ;
82
102
cur .next = cur .next .next ;
103
+ destroy = null ; // clear to let GC do its work
104
+
105
+ size --;
83
106
}
84
107
}
85
108
@@ -89,14 +112,14 @@ public void deleteNth(int position) {
89
112
* @return true is list is empty
90
113
*/
91
114
public boolean isEmpty () {
92
- return getSize () == 0 ;
115
+ return size == 0 ;
93
116
}
94
117
95
118
/**
96
119
* Prints contents of the list
97
120
*/
98
121
public void display () {
99
- Node current = head ;
122
+ Node current = head . next ;
100
123
while (current != null ) {
101
124
System .out .print (current .value + " " );
102
125
current = current .next ;
@@ -108,17 +131,7 @@ public void display() {
108
131
* Returns the size of the linked list
109
132
*/
110
133
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 ;
122
135
}
123
136
124
137
/**
0 commit comments