java - Why is my doubly-linked list repeating on insertion? -


my code generating odd answers can't entirely comprehend. example, insert method seems repeat first insert make, disregarding size of list , insert calls make after it.

public class linkedlistimpl implements list_interface {         node sentinel;          private int numelts;   public linkedlistimpl(){         sentinel=new node(0);         numelts = 0;     }  public boolean insert(double elt, int index) {     node temp = new node(elt);     node currentnode;     int x;      if(index > size()) {         return false;     }        else  if(index == 0){         currentnode = sentinel;         if(size() == 0) {             currentnode.prev = temp;             temp.next = sentinel;         }         currentnode.next = temp;         temp.prev = currentnode;          numelts++;         return true;     }     else {         currentnode = sentinel;         for(x = 0; x < index; x++) {             currentnode = currentnode.next;         }         temp = currentnode;         currentnode.prev = temp;         temp.next = currentnode;          numelts++;         return true;     } }  public double get(int index) {     node currentnode;     int x;      if(numelts == 0) {         return double.nan;     }     else {         currentnode = sentinel;         for(x = 0; x < index; x++) {             currentnode = currentnode.next;         }         return currentnode.getdata();     } } } public int size() {         return numelts;     } 

this code here:

linkedlistimpl l= new linkedlistimpl(); l.insert(45.0,  0); l.insert(13.0, 1); l.insert(89, 2); system.out.println("size of list: " + l.size()); system.out.println(l.get(0)); system.out.println(l.get(1)); system.out.println(l.get(2)); system.out.println(l.get(3)); 

returns:

size of list: 3 0.0 45.0 45.0 

i don't understand why 45.0 isn't first entry @ l.get(0)

i don't understand why it's duplicating 45.0 on , on again. if use loop print l.get(x) 0 <= x <= 10 still repeat 45.0 way x = 2 x = 10.

for 1 thing, not setting of references. insert newnode doubly-linked linked between prevnode , nextnode, need change following references:

prevnode.next newnode.prev newnode.next nextnode.prev 

your code sets subset of those.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -