Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

How can I implement a LinkedList iterator in Java?

Asked by Anil Jha Oct 6, 2022 1.2K views 1 answer
Share

About this question

I am a newbie to Java. I will be glad to hear your opinion with regards to this LinkedList implementation. The Iterators are self implemented by my decision:


public interface Iterator {
    public boolean hasNext();
    public Object Next();
}


public class LinkedList {


    private Node mBase;


    private class ListIterator implements Iterator {


        private Node iterator;


        ListIterator(){

            iterator = mBase;

        }

        @Override   

        public boolean hasNext(){           

            return (iterator.getNext() != null);            

        }

        @Override   

        public Object Next() {          

            Object userData = iterator.getData();

            iterator = iterator.getNext();          

            return userData;

        }

    }


    static private class Node {


        private Node next;

        private Object data;


        Node(Object data, Node next) {

            this.data = data;

            this.next = next;

        }   


        Object getData(){

            return data;

        }


        Node getNext(){

            return next;

        }


        boolean isNext(){

            if (next == null) return (false);


            return (true);

        }

    }


    public void push(Object data){      

        Node newNode = new Node(data, mBase);       

        mBase = newNode;

    }


    public Object pop(){


        Node baseNode = mBase;

        Object userdata = null;


        if (isEmpty())

        {           

            return (null);

        }


        mBase = mBase.getNext();

        baseNode.next = null;

        userdata = baseNode.getData();      

        baseNode = null;            


        return (userdata);

    }


    public long getSize(){


        Node current = mBase;

        int counter = 0;


        if (isEmpty()){         

            return (0);

        }


        while (current.isNext()){

            current = current.getNext();

            ++counter;

        }


        ++counter;


        return (counter);


    };


    public ListIterator getItr(){

        return new ListIterator();

    };


    public boolean isEmpty(){

        return (mBase == null);     

    }


}


public class Main {


    public static void main(String[] args) {

        // TODO Auto-generated method stub


        LinkedList list = new LinkedList();


        System.out.print("isEmpty. Print true:");

        System.out.println(list.isEmpty());


        list.push(1);

        list.push(2);

        list.push(3);

        list.push("Hello");

        list.push("World");


        System.out.print("isEmpty. Print false:");

        System.out.println(list.isEmpty());


        System.out.print("getSize(). Should be 5: ");

        System.out.println(list.getSize());


        Iterator itr = list.getItr();


        System.out.print("itr.hasNext() (should be true)");

        System.out.println(itr.hasNext());



        while (itr.hasNext()) {

            System.out.print("itr.getNext()");

            System.out.println(itr.Next());

        }


        System.out.print("itr.getNext()");

        System.out.println(itr.Next());


        while(!list.isEmpty()){


            System.out.print("itr.list.pop()");

            System.out.println(list.pop());

        }


        System.out.print("getSize(). Should be 0: ");

        System.out.println(list.getSize());     



    }


}

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.