HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

LinkedList implementation with Iterators in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linkedlistwithjavaimplementationiterators

Problem

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;
}

++counte

Solution

Use (at least parts of) the source, Luke

You mentioned that you decided to implement iterators on your own, but you could at least use the standard Iterator interface. It's almost the same as what you did, having only two methods, hasNext, and next. Not only it's standard so that it should be familiar to all readers, those are better names too, and follow the camelCase convention of the language.

What's in a name?

Using a common vocabulary helps programmers understand each other's code. It's OK to implement a common library class on your own for practice, but to facilitate communication, it's good to stick to the common vocabulary. So in the future I suggest to use the same names for methods used by the JDK, unless there is a good reason to deviate.

Inner classes

When possible, it's good to make inner classes static, so that instances of those classes don't hold a reference to the enclosing class. You could make ListIterator static by passing mBase to its constructor.

Performance

The getSize method had \$O(n)\$ time complexity, because it iterates over the elements every time it's called. (Btw, it's a bit ironic that the implementation doesn't use the iterator, what a missed opportunity!) You could do better, at the expense of an integer field, incremented when you add a new item, decremented when you remove an item. The there time complexity would become \$O(1)\$.

Simplify

This can be written simpler:

boolean isNext(){
        if (next == null) return (false);
        return (true);
    }


Like this:

boolean isNext() {
        return next != null;
    }

Code Snippets

boolean isNext(){
        if (next == null) return (false);
        return (true);
    }
boolean isNext() {
        return next != null;
    }

Context

StackExchange Code Review Q#149007, answer score: 3

Revisions (0)

No revisions yet.