@@ -54,20 +54,16 @@ public void insert(int data) {
5454 * @param data data to be stored in a new node
5555 * @param position position at which a new node is to be inserted
5656 */
57-
5857 public void insertNth (int data , int position ) {
59- if (position < 0 || position > getSize ()) {
60- throw new IndexOutOfBoundsException ("position less than zero or position more than the count of list" );
61- } else {
62- Node cur = head ;
63- Node node = new Node (data );
64- for (int i = 0 ; i < position ; ++i ) {
65- cur = cur .next ;
66- }
67- node .next = cur .next ;
68- cur .next = node ;
69- size ++;
58+ checkBounds (position , 0 , size );
59+ Node cur = head ;
60+ for (int i = 0 ; i < position ; ++i ) {
61+ cur = cur .next ;
7062 }
63+ Node node = new Node (data );
64+ node .next = cur .next ;
65+ cur .next = node ;
66+ size ++;
7167 }
7268
7369 /**
@@ -90,41 +86,57 @@ public void delete() {
9086 * This method deletes an element at Nth position
9187 */
9288 public void deleteNth (int position ) {
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 {
96- Node cur = head ;
97- for (int i = 0 ; i < position ; ++i ) {
98- cur = cur .next ;
99- }
100-
101- Node destroy = cur .next ;
102- cur .next = cur .next .next ;
103- destroy = null ; // clear to let GC do its work
104-
105- size --;
89+ checkBounds (position , 0 , size - 1 );
90+ Node cur = head ;
91+ for (int i = 0 ; i < position ; ++i ) {
92+ cur = cur .next ;
10693 }
94+
95+ Node destroy = cur .next ;
96+ cur .next = cur .next .next ;
97+ destroy = null ; // clear to let GC do its work
98+
99+ size --;
107100 }
108101
109102 /**
110- * Checks if the list is empty
111- *
112- * @return true is list is empty
103+ * @param position to check position
104+ * @param low low index
105+ * @param high high index
106+ * @throws IndexOutOfBoundsException if {@code position} not in range {@code low} to {@code high}
113107 */
114- public boolean isEmpty () {
115- return size == 0 ;
108+ public void checkBounds (int position , int low , int high ) {
109+ if (position < low || position > high ) {
110+ throw new IndexOutOfBoundsException (position + "" );
111+ }
116112 }
117113
118114 /**
119- * Prints contents of the list
115+ * clear all nodes in list
120116 */
121- public void display () {
122- Node current = head .next ;
123- while (current != null ) {
124- System .out .print (current .value + " " );
125- current = current .next ;
117+ public void clear () {
118+ if (size == 0 ) {
119+ return ;
126120 }
127- System .out .println ();
121+ Node prev = head .next ;
122+ Node cur = prev .next ;
123+ while (cur != null ) {
124+ prev = null ; // clear to let GC do its work
125+ prev = cur ;
126+ cur = cur .next ;
127+ }
128+ prev = null ;
129+ head .next = null ;
130+ size = 0 ;
131+ }
132+
133+ /**
134+ * Checks if the list is empty
135+ *
136+ * @return true is list is empty
137+ */
138+ public boolean isEmpty () {
139+ return size == 0 ;
128140 }
129141
130142 /**
@@ -134,6 +146,20 @@ public int getSize() {
134146 return size ;
135147 }
136148
149+ @ Override
150+ public String toString () {
151+ if (size == 0 ) {
152+ return "" ;
153+ }
154+ StringBuilder builder = new StringBuilder ();
155+ Node cur = head .next ;
156+ while (cur != null ) {
157+ builder .append (cur .value ).append ("->" );
158+ cur = cur .next ;
159+ }
160+ return builder .replace (builder .length () - 2 , builder .length (), "" ).toString ();
161+ }
162+
137163 /**
138164 * Main method
139165 *
@@ -148,19 +174,24 @@ public static void main(String args[]) {
148174 myList .insertHead (7 );
149175 myList .insertHead (10 );
150176
151- myList . display () ; // 10 -> 7 -> 5
177+ System . out . println ( myList ); ; // 10 -> 7 -> 5
152178
153179 myList .deleteHead ();
154180
155- myList . display () ; // 7 -> 5
181+ System . out . println ( myList ); ; // 7 -> 5
156182
157183 myList .insertNth (11 , 2 );
158184
159- myList . display () ; // 7 -> 5 -> 11
185+ System . out . println ( myList ); ; // 7 -> 5 -> 11
160186
161187 myList .deleteNth (1 );
162188
163- myList .display (); // 7-> 11
189+ System .out .println (myList );; // 7-> 11
190+
191+ myList .clear ();
192+ assert myList .isEmpty ();
193+
194+ System .out .println (myList ); // ""
164195
165196 }
166197}
0 commit comments