Endless iterator loop on doubly linked list using Java -


i have created doubly-linked list , implemented own iterator.

however, have done wrong , iterator results in endless loop.

have been struggling find error, feedback appreciated. in advance. apologise wall of code, think error lies within iterator of way have created node class.

my code:

import java.util.iterator; import java.util.nosuchelementexception;  public class mydoubleendedlinkedlist<t extends comparable<t>> implements iterable<t> { // initialising nodes including 2 sentinal nodes  private node<t> head; private node<t> tail; private node<t> current; private int currentsize;  mydoubleendedlinkedlist() {     head = new node<t>();     tail = new node<t>();     head.setnext(tail);     tail.setprevious(head);     current = head;     currentsize = 0;   // methods used loop , iterate through list public boolean isempty() {     return (current == head && current == tail); }  public boolean endlist() {     return (current != tail); }  public void resetcurrent() {      current = head; }  public void nextcurrent() {     current = current.getnext(); }  public t getcurrent() {     return current.getdata(); }  public int size() {     return this.currentsize; }     @override public iterator<t> iterator() {      return new linkedlistiterator<t>(); }  // node class doublylinkedlist  public class node<e> {     private node<e> previous;     private node<e> next;     private e data;  node() {     previous = null;     next = null;     data = null; }  node(node<e> newprevious, node<e> newnext, e newdata) {     previous = newprevious;     next = newnext;     data = newdata; }  // set previous node public void setprevious(node<e> newprevious) {     previous = newprevious; }  // set next node public void setnext(node<e> newnext) {     next = newnext; }  public void setdata(e newdata) {     data = newdata; }  public node<e> getprevious() {     return previous; }  public node<e> getnext() {     return next;  }  public e getdata() {     return data; }  }  class linkedlistiterator<e> implements iterator<t> {     private node<t> current;     private node<t> previous;     private node<t> previous2;      private boolean removecalled;      public linkedlistiterator() {         current = head;         previous = null;         previous2 = null;         removecalled = false;     }      public boolean hasnext() {         return (current != null);     }      public t next() {         if (hasnext()) {          t temp = current.getdata();         previous2 = previous;         previous = current;         current = current.next;         removecalled = false;         return temp;     }     throw new nosuchelementexception(); }      public void remove() {          if (previous == null || removecalled) {         throw new illegalstateexception();         }         if (previous2 == null) {         head = current;         } else {             previous2.setnext(current);             previous = previous2;         }         removecalled = true;          throw new unsupportedoperationexception();    }  }} 

so can't find bug in code here simpler implementation of basic linked list in java. if show me how you're adding elements list easier track down.

import java.util.iterator;  public class mylinkedlist<t> implements iterable<t> {     private node head = null;     private node tail = null;      public static void main(string[] args) {         mylinkedlist<string> li = new mylinkedlist<>();         li.add("1");         li.add("2");         li.add("3");         li.add("4");         li.add("5");          (string s : li) {             system.out.println(s);         }     }      public void add(t data) {         if (head == null) {             head = new node(data, null);             tail = head;         } else {             node n = new node(data, tail);             tail.next = n;             tail = n;         }     }      @override     public iterator<t> iterator() {         return new iterator<t>() {              node current = head;              @override             public boolean hasnext() {                 return current != null;             }              @override             public t next() {                 t data = current.data;                 current = current.next;                 return data;             }              @override             public void remove() {              }         };     }      class node {         final t data;         node prev = null;         node next = null;          node(t data, node prev) {             this.data = data;             this.prev = prev;         }     } } 

Popular posts from this blog