java - developing a add to index method in generic linked list class -


i'm having trouble implementing add method generic linked list class adds value @ given index. however, can't doing correctly when running through test class i'm getting following errors:

1) t31addtoemptylist(linkedlisttest$add2test) java.lang.indexoutofboundsexception  2) t32addtothreeitemlist0(linkedlisttest$add2test) java.lang.assertionerror: 4 element list has wrong size. expected:<4> was:<3>  3) t35addtothreeitemlist3(linkedlisttest$add2test) java.lang.indexoutofboundsexception 

this add @ index method:

public void add(int index, e value){     if (index < 0 || index >= this.size) {         throw new indexoutofboundsexception();      //if empty add front     }else if(index == 0) {         front = new listnode(value, front);     // if index last element add front     }else if(index == size){         add(value);     }else {         listnode current = front;         (int = 0; < index - 1; i++){             current = current.next;         }         current.next = new listnode(value, current.next);     }     this.size++;  } 

and full class:

public class linkedlist<e>{      int size;      private class listnode{         e data;         listnode next;          listnode(e data) {             this.data = data;             next = null;         }          listnode(e data, listnode next) {             this.data = data;             this.next = next;         }     }      private listnode front;     private listnode end;      // constructor     // construct empty linkedlist object.     public linkedlist(){         this.size = 0;         this.front = null;     }      // return size (number of items) in linkedlist.     public int size(){         return size;     }      // return true if linkedlist has no items.     // return false if size greater zero.     boolean isempty(){         return size() == 0;     }      //  add given element, value, end of list.     // appends     public void add(e value){         if (front == null) {             front = new listnode(value);         } else {             listnode current = front;             while (current.next != null) {                 current = current.next;             }             current.next = new listnode(value);         }         this.size++;     }      // add given element, value, list @ given index.     // after operation complete, get(index) return value.     // operation valid 0 <= index <= size().     public void add(int index, e value){         if (index < 0 || index >= this.size) {             throw new indexoutofboundsexception();          //if empty add front         }else if(index == 0) {             front = new listnode(value, front);         // if index last element add front         }else if(index == size){             add(value);         }else {             listnode current = front;             (int = 0; < index - 1; i++){                 current = current.next;             }             current.next = new listnode(value, current.next);         }         this.size++;      }      /*     // prepend helper method     //prepend     public void prepend(int data){         if (front == null){             end = new listnode();             front = end;         }         else {             front = new listnode(data,front);         }         size++;     }     */      // return element of list @ given index.     // operation valid 0 <= index < size().     // operation not modify list.     public e get(int index){          // check index out of bounds         if (index < 0 || index >= this.size) {             throw new indexoutofboundsexception();         }         // returns char @ index         listnode curr = front;         (int = 0; < index; i++) {             curr = curr.next;         }         return curr.data;     }  } // end class 

my test class https://gist.github.com/silverfin13/e22d061c2daf0d3a7013

i appreciate help, thank you

if (index < 0 || index >= this.size) {     throw new indexoutofboundsexception(); 

here index >= this.size condition @ top of public void add(int index, e value)is problem guess should index > this.size

why? if want add @ node in last position index equal current size , it's valid.

you have else if(index == size) condition it's mean want allow index equal size @ top throwing new indexoutofboundsexception(); that.


you have problem in size maintaining.

public void add(int index, e value) method increase size this.size++; valid insert. in case of index==size

else if(index == size){         add(value); 

you calling public void add(e value) method increase size @ end of operation in case of index==size this.size increased twice.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -